Skip to content

Commit

Permalink
Get progressbar::last back, do not print multiple 100% lines (fixes v…
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalif committed Sep 30, 2016
1 parent 67b5b05 commit 68e0e5a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
57 changes: 31 additions & 26 deletions libgrive/src/util/ProgressBar.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
namespace gr
{

ProgressBar::ProgressBar(): showProgressBar(false)
ProgressBar::ProgressBar(): showProgressBar(false), last(1000)
{
}

Expand Down Expand Up @@ -48,33 +48,38 @@ void ProgressBar::reportProgress(u64_t total, u64_t processed)
double fraction = (double)processed/total;

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
int availableSize = determineTerminalSize() - 32;
int totalDots;
if (availableSize > 100)
totalDots = 100;
else if (availableSize < 0)
totalDots = 10;
else
totalDots = availableSize;
// 10 for prefix of percent and 22 for suffix of file size
int availableSize = determineTerminalSize() - 32;
int totalDots;
if (availableSize > 100)
totalDots = 100;
else if (availableSize < 0)
totalDots = 10;
else
totalDots = availableSize;

int dotz = round(fraction * totalDots);
int count = 0;
// delete previous output line
printf("\r\33[2K [%3.0f%%] [", fraction * 100);
for (; count < dotz - 1; count++)
putchar('=');
putchar('>');
for (; count < totalDots - 1; count++)
putchar(' ');
printf("] ");
printBytes(processed);
putchar('/');
printBytes(total);
if (point == 1000)
putchar('\n');
fflush(stdout);
int dotz = round(fraction * totalDots);
int count = 0;
// delete previous output line
printf("\r\33[2K [%3.0f%%] [", fraction * 100);
for (; count < dotz - 1; count++)
putchar('=');
putchar('>');
for (; count < totalDots - 1; count++)
putchar(' ');
printf("] ");
printBytes(processed);
putchar('/');
printBytes(total);
if (point == 1000)
putchar('\n');
fflush(stdout);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions libgrive/src/util/ProgressBar.hh
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ private:
static unsigned short int determineTerminalSize();

bool showProgressBar;
int last;
};

}
Expand Down

0 comments on commit 68e0e5a

Please sign in to comment.