Added prefix to progress bar, perc right

This commit is contained in:
Bob Polis 2021-12-13 10:32:16 +01:00
parent 5bb2d7268c
commit 35ed58b012
2 changed files with 19 additions and 4 deletions

View File

@ -27,23 +27,34 @@ int term::cols() const {
return _ws.ws_col;
}
void term::progress(double cur, double total) const {
void term::progress(int prefixlen,
const std::string& prefix,
double cur,
double total) const
{
// use unicode to make nice bar
const char* bar[8] = {
u8"\u258F", u8"\u258E", u8"\u258D", u8"\u258C",
u8"\u258B", u8"\u258A", u8"\u2589", u8"\u2588"
};
int barwidth = cols() - 5;
int perclen = 5;
int barwidth = cols() - perclen - prefixlen - 1;
int maxsteps = barwidth * 8;
int steps = round(cur * maxsteps / total);
int perc = round(100 * cur / total);
*_out << '\r' << std::setw(3) << perc << "% ";
int fill_len = barwidth - steps / 8;
*_out << '\r' << std::setw(prefixlen) << prefix << ' ';
grayf(8);
for (int i = 0; i < steps / 8; ++i) {
*_out << bar[7];
}
if (steps % 8) {
*_out << bar[steps % 8 - 1];
fill_len--;
}
reset();
*_out << std::string(fill_len, ' ');
*_out << ' ' << std::setw(3) << perc << '%';
}
void term::hide_cursor() const {

View File

@ -5,6 +5,7 @@
#include <termios.h>
#include <sys/ioctl.h>
#include <iostream>
#include <string>
namespace sc {
@ -21,7 +22,10 @@ namespace sc {
int rows() const;
int cols() const;
void progress(double cur, double total) const;
void progress(int prefixlen,
const std::string& prefix,
double cur,
double total) const;
void hide_cursor() const;
void show_cursor() const;