diff --git a/src/utils.cpp b/src/utils.cpp index 1b3be38..8d0d073 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -34,6 +34,10 @@ void term::progress(int prefixlen, double cur, double total) const { + progress(prefixlen, prefix, cur / total); +} + +void term::progress(int prefixlen, const std::string& prefix, double fraction) const { if (!_isatty) return; // use unicode to make nice bar @@ -41,12 +45,12 @@ void term::progress(int prefixlen, u8"\u258F", u8"\u258E", u8"\u258D", u8"\u258C", u8"\u258B", u8"\u258A", u8"\u2589", u8"\u2588" }; - if (cur > total) cur = total; + if (fraction > 1) fraction = 1.0; 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); + int steps = round(fraction * maxsteps); + int perc = round(100 * fraction); int fill_len = barwidth - steps / 8; *_out << '\r' << std::setw(prefixlen) << prefix.substr(0, prefixlen) << ' ' << io::grayf(8); for (int i = 0; i < steps / 8; ++i) { diff --git a/src/utils.hpp b/src/utils.hpp index e4c6467..f43a589 100644 --- a/src/utils.hpp +++ b/src/utils.hpp @@ -25,6 +25,10 @@ namespace sc { double cur, double total) const; + void progress(int prefixlen, + const std::string& prefix, + double fraction) const; + static bool has_truecolor(); };