From 7e1e39e592f8394e39432ffcd6a5fda086611bac Mon Sep 17 00:00:00 2001 From: mrbesen Date: Sun, 23 Oct 2022 12:01:02 +0200 Subject: [PATCH] improve progressbar --- src/dedup.cpp | 1 + src/progressbar.cpp | 20 +++++++++++++------- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/dedup.cpp b/src/dedup.cpp index 3c06019..af490b4 100644 --- a/src/dedup.cpp +++ b/src/dedup.cpp @@ -88,6 +88,7 @@ std::map Dedup::hash(std::multimap + #include "file.h" const uint_fast8_t ProgressBar::BARLENGTH = 50; @@ -18,17 +20,20 @@ std::chrono::duration ProgressBar::getDuration() const { } std::ostream& operator<<(std::ostream& str, const ProgressBar& pb) { - double progressd = pb.currentBytes / (double) pb.maxBytes; - uint64_t progressi = (progressd * 10000.0); + double progress = (pb.currentBytes / (double) pb.maxBytes); auto usedtime = pb.getDuration(); std::chrono::duration> eta = std::chrono::duration_cast((usedtime / (double) pb.currentBytes) * pb.maxBytes); + // speed + double speed = (pb.currentBytes / (double) usedtime.count()); // B/ms + speed /= 1000; // B/s + uint64_t bytespersecond = (int) speed; // floor // generate bar - str << "["; + str << "\r["; for(uint_fast8_t i = 0; i < ProgressBar::BARLENGTH; ++i) { - if( (i / (double) ProgressBar::BARLENGTH) < progressd ) { + if( (i / (double) ProgressBar::BARLENGTH) < progress ) { str << '#'; } else { str << ' '; @@ -36,9 +41,10 @@ std::ostream& operator<<(std::ostream& str, const ProgressBar& pb) { } // print info - str << "] " << (progressi / 100.0f) << "% " << pb.currentFiles << '/' << pb.maxFiles << " Files " - << FileSize(pb.currentBytes) << '/' << FileSize(pb.maxBytes) << " ETA: " << eta.count() - << "s \r" << std::flush; + str << "] " << std::setprecision(2) << std::setfill('0') << std::fixed << std::setw(5) << (progress * 100) << "% " + << std::setprecision(6) << std::defaultfloat + << FileSize(pb.currentBytes) << '/' << FileSize(pb.maxBytes) << " " << pb.currentFiles << '/' << pb.maxFiles << " Files " + << FileSize(bytespersecond) << "/s ETA: " << eta.count() << "s " << std::flush; return str; }