Get progressbar::last back, do not print multiple 100% lines (fixes #104)

pull/126/head
Vitaliy Filippov 2016-09-30 12:33:11 +03:00
parent 67b5b05e17
commit 68e0e5afe5
2 changed files with 32 additions and 26 deletions

View File

@ -8,7 +8,7 @@
namespace gr namespace gr
{ {
ProgressBar::ProgressBar(): showProgressBar(false) ProgressBar::ProgressBar(): showProgressBar(false), last(1000)
{ {
} }
@ -48,33 +48,38 @@ void ProgressBar::reportProgress(u64_t total, u64_t processed)
double fraction = (double)processed/total; double fraction = (double)processed/total;
int point = round(fraction*1000); int point = round(fraction*1000);
if (this->last < 1000 || point != this->last)
{
// only print progress after >= 0.1% change
this->last = point;
// 10 for prefix of percent and 22 for suffix of file size // 10 for prefix of percent and 22 for suffix of file size
int availableSize = determineTerminalSize() - 32; int availableSize = determineTerminalSize() - 32;
int totalDots; int totalDots;
if (availableSize > 100) if (availableSize > 100)
totalDots = 100; totalDots = 100;
else if (availableSize < 0) else if (availableSize < 0)
totalDots = 10; totalDots = 10;
else else
totalDots = availableSize; totalDots = availableSize;
int dotz = round(fraction * totalDots); int dotz = round(fraction * totalDots);
int count = 0; int count = 0;
// delete previous output line // delete previous output line
printf("\r\33[2K [%3.0f%%] [", fraction * 100); printf("\r\33[2K [%3.0f%%] [", fraction * 100);
for (; count < dotz - 1; count++) for (; count < dotz - 1; count++)
putchar('='); putchar('=');
putchar('>'); putchar('>');
for (; count < totalDots - 1; count++) for (; count < totalDots - 1; count++)
putchar(' '); putchar(' ');
printf("] "); printf("] ");
printBytes(processed); printBytes(processed);
putchar('/'); putchar('/');
printBytes(total); printBytes(total);
if (point == 1000) if (point == 1000)
putchar('\n'); putchar('\n');
fflush(stdout); fflush(stdout);
}
} }
} }

View File

@ -18,6 +18,7 @@ private:
static unsigned short int determineTerminalSize(); static unsigned short int determineTerminalSize();
bool showProgressBar; bool showProgressBar;
int last;
}; };
} }