From 2b24eab3703bedb2aa4f1329fc3c919c869b6e85 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 21 Sep 2006 07:13:43 +0000 Subject: The percent view now takes the terminal's width into account, pretty cool! --- src/viewerpercent.cpp | 76 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 31 deletions(-) (limited to 'src/viewerpercent.cpp') diff --git a/src/viewerpercent.cpp b/src/viewerpercent.cpp index 03dfe23..31ce52a 100644 --- a/src/viewerpercent.cpp +++ b/src/viewerpercent.cpp @@ -2,6 +2,10 @@ #include "perform.h" #include "plugger.h" +#include +#include +#include + PluginInterface2( percent, ViewerPercent, Viewer, "Mike Buland", 0, 1 ); ViewerPercent::ViewerPercent() : @@ -27,6 +31,12 @@ void ViewerPercent::beginCommand( Action::eAction nAct, const std::string &sTarg { this->sTarget = sTarget; nCurCommand++; + + struct winsize scrn; + + ioctl( fileno( stdout ), TIOCGWINSZ, &scrn ); + + nTermWidth = scrn.ws_col; } void ViewerPercent::endCommand() @@ -43,27 +53,9 @@ void ViewerPercent::beginPerforms( int nCount ) void ViewerPercent::endPerforms() { - int nLen = printf("\r[%d/%d] %s [", - nCurCommand, nTotalCommands, - sTarget.c_str() ); - for( int j = 0; j < nWidth; j++ ) - { - fputc('#', stdout ); - } - nLen += nWidth; - nLen += printf("] 100%%"); + nCurPerform = nTotalPerforms; - if( nLastLen > nLen ) - { - int jmax = nLastLen-nLen; - for( int j = 0; j < jmax; j++ ) - { - fputc(' ', stdout ); - } - } - nLastLen = 0; - - fflush( stdout ); + printPercent(""); } void ViewerPercent::beginRequiresCheck( bool bCached, const std::string &sName ) @@ -77,33 +69,55 @@ void ViewerPercent::endRequiresCheck() void ViewerPercent::beginPerform( Perform *pPerform ) { nCurPerform++; - int nLen = printf("\r[%d/%d] %s [", + printPercent( pPerform->getTarget() ); +} + +void ViewerPercent::printPercent( const std::string &sCur ) +{ + char buf[2048]; + char *bi = buf; + int nLen = sprintf( buf, "\r[%d/%d] %s [", nCurCommand, nTotalCommands, sTarget.c_str() ); + bi += nLen; int jmax = nCurPerform*nWidth/nTotalPerforms; for( int j = 0; j < jmax; j++ ) { - fputc('#', stdout ); + *bi = '#'; + bi++; } jmax = nWidth-jmax; for( int j = 0; j < jmax; j++ ) { - fputc(' ', stdout ); + *bi = ' '; + bi++; } nLen += nWidth; - nLen += printf("] %-2d%% %s", + nLen += sprintf( bi, "] %-2d%% %s", nCurPerform*100/nTotalPerforms, - pPerform->getTarget().c_str() ); - - if( nLastLen > nLen ) + sCur.c_str() ); + bi = buf + nLen; + if( (int)(bi - buf) >= nTermWidth ) { - jmax = nLastLen-nLen; - for( int j = 0; j < jmax; j++ ) + nLastLen = nLen = nTermWidth; + strcpy( buf+nTermWidth-3, "..."); + } + else + { + if( nLastLen > nLen ) { - fputc(' ', stdout ); + jmax = nLastLen-nLen; + for( int j = 0; j < jmax; j++ ) + { + *bi = ' '; + bi++; + } } + nLastLen = nLen; + *bi = '\0'; } - nLastLen = nLen; + + fputs( buf, stdout ); fflush( stdout ); } -- cgit v1.2.3