aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/viewerpercent.cpp76
-rw-r--r--src/viewerpercent.h3
2 files changed, 48 insertions, 31 deletions
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 @@
2#include "perform.h" 2#include "perform.h"
3#include "plugger.h" 3#include "plugger.h"
4 4
5#include <sys/ioctl.h>
6#include <stdio.h>
7#include <stdlib.h>
8
5PluginInterface2( percent, ViewerPercent, Viewer, "Mike Buland", 0, 1 ); 9PluginInterface2( percent, ViewerPercent, Viewer, "Mike Buland", 0, 1 );
6 10
7ViewerPercent::ViewerPercent() : 11ViewerPercent::ViewerPercent() :
@@ -27,6 +31,12 @@ void ViewerPercent::beginCommand( Action::eAction nAct, const std::string &sTarg
27{ 31{
28 this->sTarget = sTarget; 32 this->sTarget = sTarget;
29 nCurCommand++; 33 nCurCommand++;
34
35 struct winsize scrn;
36
37 ioctl( fileno( stdout ), TIOCGWINSZ, &scrn );
38
39 nTermWidth = scrn.ws_col;
30} 40}
31 41
32void ViewerPercent::endCommand() 42void ViewerPercent::endCommand()
@@ -43,27 +53,9 @@ void ViewerPercent::beginPerforms( int nCount )
43 53
44void ViewerPercent::endPerforms() 54void ViewerPercent::endPerforms()
45{ 55{
46 int nLen = printf("\r[%d/%d] %s [", 56 nCurPerform = nTotalPerforms;
47 nCurCommand, nTotalCommands,
48 sTarget.c_str() );
49 for( int j = 0; j < nWidth; j++ )
50 {
51 fputc('#', stdout );
52 }
53 nLen += nWidth;
54 nLen += printf("] 100%%");
55 57
56 if( nLastLen > nLen ) 58 printPercent("");
57 {
58 int jmax = nLastLen-nLen;
59 for( int j = 0; j < jmax; j++ )
60 {
61 fputc(' ', stdout );
62 }
63 }
64 nLastLen = 0;
65
66 fflush( stdout );
67} 59}
68 60
69void ViewerPercent::beginRequiresCheck( bool bCached, const std::string &sName ) 61void ViewerPercent::beginRequiresCheck( bool bCached, const std::string &sName )
@@ -77,33 +69,55 @@ void ViewerPercent::endRequiresCheck()
77void ViewerPercent::beginPerform( Perform *pPerform ) 69void ViewerPercent::beginPerform( Perform *pPerform )
78{ 70{
79 nCurPerform++; 71 nCurPerform++;
80 int nLen = printf("\r[%d/%d] %s [", 72 printPercent( pPerform->getTarget() );
73}
74
75void ViewerPercent::printPercent( const std::string &sCur )
76{
77 char buf[2048];
78 char *bi = buf;
79 int nLen = sprintf( buf, "\r[%d/%d] %s [",
81 nCurCommand, nTotalCommands, 80 nCurCommand, nTotalCommands,
82 sTarget.c_str() ); 81 sTarget.c_str() );
82 bi += nLen;
83 int jmax = nCurPerform*nWidth/nTotalPerforms; 83 int jmax = nCurPerform*nWidth/nTotalPerforms;
84 for( int j = 0; j < jmax; j++ ) 84 for( int j = 0; j < jmax; j++ )
85 { 85 {
86 fputc('#', stdout ); 86 *bi = '#';
87 bi++;
87 } 88 }
88 jmax = nWidth-jmax; 89 jmax = nWidth-jmax;
89 for( int j = 0; j < jmax; j++ ) 90 for( int j = 0; j < jmax; j++ )
90 { 91 {
91 fputc(' ', stdout ); 92 *bi = ' ';
93 bi++;
92 } 94 }
93 nLen += nWidth; 95 nLen += nWidth;
94 nLen += printf("] %-2d%% %s", 96 nLen += sprintf( bi, "] %-2d%% %s",
95 nCurPerform*100/nTotalPerforms, 97 nCurPerform*100/nTotalPerforms,
96 pPerform->getTarget().c_str() ); 98 sCur.c_str() );
97 99 bi = buf + nLen;
98 if( nLastLen > nLen ) 100 if( (int)(bi - buf) >= nTermWidth )
99 { 101 {
100 jmax = nLastLen-nLen; 102 nLastLen = nLen = nTermWidth;
101 for( int j = 0; j < jmax; j++ ) 103 strcpy( buf+nTermWidth-3, "...");
104 }
105 else
106 {
107 if( nLastLen > nLen )
102 { 108 {
103 fputc(' ', stdout ); 109 jmax = nLastLen-nLen;
110 for( int j = 0; j < jmax; j++ )
111 {
112 *bi = ' ';
113 bi++;
114 }
104 } 115 }
116 nLastLen = nLen;
117 *bi = '\0';
105 } 118 }
106 nLastLen = nLen; 119
120 fputs( buf, stdout );
107 121
108 fflush( stdout ); 122 fflush( stdout );
109} 123}
diff --git a/src/viewerpercent.h b/src/viewerpercent.h
index 96412cb..d40ca1b 100644
--- a/src/viewerpercent.h
+++ b/src/viewerpercent.h
@@ -23,6 +23,8 @@ public:
23 virtual void beginAction( const std::string &sName, int nCommands ); 23 virtual void beginAction( const std::string &sName, int nCommands );
24 virtual void endAction(); 24 virtual void endAction();
25 25
26 void printPercent( const std::string &sCur );
27
26private: 28private:
27 int nTotalCommands; 29 int nTotalCommands;
28 int nCurCommand; 30 int nCurCommand;
@@ -31,6 +33,7 @@ private:
31 std::string sTarget; 33 std::string sTarget;
32 int nLastLen; 34 int nLastLen;
33 int nWidth; 35 int nWidth;
36 int nTermWidth;
34}; 37};
35 38
36#endif 39#endif