Index: src/OFStdIOStream.h ================================================================== --- src/OFStdIOStream.h +++ src/OFStdIOStream.h @@ -32,10 +32,26 @@ @interface OFStdIOStream: OFStream { int _fd; bool _atEndOfStream; } + +/*! + * @brief Query the underlying terminal for the number of columns. + * + * @return The number of columns, or -1 if there is no underlying terminal or + * the number of columns could not be queried + */ +- (int)columns; + +/*! + * @brief Query the underlying terminal for the number of rows. + * + * @return The number of rows, or -1 if there is no underlying terminal or the + * number of rows could not be queried + */ +- (int)rows; @end #ifdef __cplusplus extern "C" { #endif Index: src/OFStdIOStream.m ================================================================== --- src/OFStdIOStream.m +++ src/OFStdIOStream.m @@ -22,10 +22,17 @@ # undef __USE_XOPEN #endif #include #include + +#ifdef HAVE_SYS_IOCTL_H +# include +#endif +#ifdef HAVE_SYS_TTYCOM_H +# include +#endif #import "OFStdIOStream.h" #import "OFStdIOStream+Private.h" #import "OFDate.h" #import "OFApplication.h" @@ -202,6 +209,34 @@ - (void)dealloc { OF_DEALLOC_UNSUPPORTED } + +- (int)columns +{ +#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) + struct winsize ws; + + if (ioctl(_fd, TIOCGWINSZ, &ws) != 0) + return -1; + + return ws.ws_col; +#else + return -1; +#endif +} + +- (int)rows +{ +#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) + struct winsize ws; + + if (ioctl(_fd, TIOCGWINSZ, &ws) != 0) + return -1; + + return ws.ws_row; +#else + return -1; +#endif +} @end Index: utils/ofhttp/ProgressBar.m ================================================================== --- utils/ofhttp/ProgressBar.m +++ utils/ofhttp/ProgressBar.m @@ -16,19 +16,10 @@ #include "config.h" #include -#include - -#ifdef HAVE_SYS_IOCTL_H -# include -#endif -#ifdef HAVE_SYS_TTYCOM_H -# include -#endif - #import "OFDate.h" #import "OFStdIOStream.h" #import "OFTimer.h" #import "ProgressBar.h" @@ -91,21 +82,19 @@ } - (void)_drawProgress { float bars, percent; - unsigned short barWidth; -#if defined(HAVE_SYS_IOCTL_H) && defined(TIOCGWINSZ) - struct winsize ws; - - if (ioctl(0, TIOCGWINSZ, &ws) == 0 && ws.ws_col > 37) - barWidth = ws.ws_col - 37; - else - barWidth = 0; -#else - barWidth = 43; -#endif + int columns, barWidth; + + if ((columns = [of_stdout columns]) >= 0) { + if (columns > 37) + barWidth = columns - 37; + else + barWidth = 0; + } else + barWidth = 43; bars = (float)(_resumedFrom + _received) / (float)(_resumedFrom + _length) * barWidth; percent = (float)(_resumedFrom + _received) / (float)(_resumedFrom + _length) * 100;