Michael Matz schrieb:
Hi,
On Tue, 11 May 2004, Bastian Schern wrote:
Ich habe das jetzt so gemacht:
// void progress( float factor ) {{{ void progress( float factor ) { static struct winsize winsz; ioctl( 0, TIOCGWINSZ, &winsz ); int MAX_CHARS = winsz.ws_col;
Schon recht huebsch. Natuerlich fehlt Fehlerbehandlung. Wenn z.B. stdin gar kein Terminal ist (sondern z.B. ne pipe) geht obiges natuerlich schief. Also "if (ioctl (...) == 0) { MAX_CHARS = ... }" . Und siehe auch isatty(3) .
Vielen Dank für den Hinweis. Die Funktion sieht jetzt so aus: // void progress( float factor ) {{{ void progress( float factor ) { static struct winsize winsz; static int MAX_CHARS = 72; if( 0 == ioctl( 0, TIOCGWINSZ, &winsz ) ) { MAX_CHARS = winsz.ws_col; if( MAX_CHARS > 8 ) MAX_CHARS -= 8; } cout << setw( static_cast<int>( factor * MAX_CHARS ) ) << setfill( '#' ) << ""; cout << setw( MAX_CHARS - static_cast<int>( factor * MAX_CHARS ) ) << setfill( '·' ) << ""; cout << " " << setw( 6 ) << setfill( ' ' ) << setprecision( 2 ) << fixed << right << static_cast<float>( factor * 100 ) << "%"; cout << flush; cout << "\r"; } // }}} isatty( ... ) benötige ich dann nicht mehr, oder? Viele Grüße Bastian