Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add the indication of the remaining time #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions progress_bar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,8 @@ void ProgressBar::ShowProgress(uint64_t progress) const {
os << std::put_time(std::localtime(&time), "[%F %T.")
<< std::setfill('0') << std::setw(3) << ms.count() << "]\t"
<< get_progress_summary(progress_ratio)
<< ", " + std::to_string(progress) + "/" + std::to_string(total_) + '\n';
<< ", " + std::to_string(progress) + "/" + std::to_string(total_)
<< ", " + BeautifyDuration(RemainingExecutionTime(progress_ratio)) + " remaining" + '\n';
*out << os.str() << std::flush;
return;
}
Expand All @@ -167,7 +168,8 @@ void ProgressBar::ShowProgress(uint64_t progress) const {
+ std::string(size_t(bar_size * progress_ratio), unit_bar_)
+ std::string(bar_size - size_t(bar_size * progress_ratio), unit_space_)
+ "] " + get_progress_summary(progress_ratio)
+ ", " + std::to_string(progress) + "/" + std::to_string(total_) + '\r';
+ ", " + std::to_string(progress) + "/" + std::to_string(total_)
+ ", " + BeautifyDuration(RemainingExecutionTime(progress_ratio)) + " remaining" + '\r';

*out << buffer_ << std::flush;

Expand All @@ -183,6 +185,9 @@ ProgressBar& ProgressBar::operator++() {
}

ProgressBar& ProgressBar::operator+=(uint64_t delta) {
if (progress_.load() == 0)
start_time_.store(std::chrono::system_clock::now());

if (silent_ || !delta)
return *this;

Expand All @@ -202,3 +207,48 @@ ProgressBar& ProgressBar::operator+=(uint64_t delta) {

return *this;
}

std::chrono::duration<double> ProgressBar::RemainingExecutionTime(double progress_ratio) const {
// epsilon to avoid division by zero
if (progress_ratio == 0)
progress_ratio = 1e-2;

auto now = std::chrono::system_clock::now();
std::chrono::duration<double> diff = now - start_time_.load();
double total_s = 1 / progress_ratio * diff.count();
return std::chrono::duration<double>(total_s - progress_ratio * total_s);
}

// from https://stackoverflow.com/questions/22590821/convert-stdduration-to-human-readable-time
std::string ProgressBar::BeautifyDuration(std::chrono::duration<double> input_seconds) const {
using namespace std::chrono;
typedef duration<int, std::ratio<86400>> days;
auto d = duration_cast<days>(input_seconds);
input_seconds -= d;
auto h = duration_cast<hours>(input_seconds);
input_seconds -= h;
auto m = duration_cast<minutes>(input_seconds);
input_seconds -= m;

auto dc = d.count();
auto hc = h.count();
auto mc = m.count();

std::stringstream ss;
ss.fill('0');
if (dc) {
ss << d.count() << "d";
}
if (dc || hc) {
if (dc) { ss << std::setw(2); } //pad if second set of numbers
ss << h.count() << "h";
}
if (dc || hc || mc) {
if (dc || hc) { ss << std::setw(2); }
ss << m.count() << "m";
}
if (dc || hc || mc) { ss << std::setw(2); }
ss << input_seconds.count() << 's';

return ss.str();
}
3 changes: 3 additions & 0 deletions progress_bar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,16 @@ class ProgressBar {
void ShowProgress(uint64_t progress) const;
int GetConsoleWidth() const;
int GetBarLength() const;
std::chrono::duration<double> RemainingExecutionTime(double progress_ratio) const;
std::string BeautifyDuration(std::chrono::duration<double> input_seconds) const;

bool silent_;
bool logging_mode_;
uint64_t total_;
std::atomic<uint64_t> progress_ = {0};
uint64_t frequency_update;
std::ostream *out;
std::atomic<std::chrono::time_point<std::chrono::system_clock>> start_time_;
mutable std::mutex mu_;
mutable std::string buffer_;

Expand Down