Skip to content

Commit

Permalink
Fix a divide by zero error in speedometer
Browse files Browse the repository at this point in the history
  • Loading branch information
oir committed Dec 19, 2023
1 parent a826d27 commit 26199a4
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions barkeep/barkeep.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ class AsyncDisplay {
message_(other.message_),
out_(other.out_),
no_tty_(other.no_tty_) {
if (other.displayer_) {
if (other.running()) {
throw std::runtime_error("A running display cannot be copied");
}
}
Expand All @@ -142,7 +142,7 @@ class AsyncDisplay {
complete_(bool(other.complete_)),
out_(other.out_),
no_tty_(other.no_tty_) {
if (other.displayer_) {
if (other.running()) {
throw std::runtime_error("A running display cannot be moved");
}
message_ = std::move(other.message_);
Expand All @@ -153,7 +153,7 @@ class AsyncDisplay {
/// Start the display. This starts writing the display in the output stream,
/// and computing speed if applicable.
virtual void show() {
if (displayer_) {
if (running()) {
return; // noop if already show()n before
}
start();
Expand Down Expand Up @@ -181,7 +181,7 @@ class AsyncDisplay {

/// End the display.
virtual void done() {
if (not displayer_) { return; } // noop if already done() before
if (not running()) { return; } // noop if already done() before
complete_ = true;
completion_.notify_all();
join();
Expand Down Expand Up @@ -398,7 +398,10 @@ class Speedometer {
progress_increment_sum_ =
(1 - discount_) * progress_increment_sum_ + progress_increment;
duration_increment_sum_ = (1 - discount_) * duration_increment_sum_ + dur;
double speed = progress_increment_sum_ / duration_increment_sum_.count();
double speed =
duration_increment_sum_.count() == 0
? 0
: progress_increment_sum_ / duration_increment_sum_.count();

ss << std::fixed << std::setprecision(2) << "(" << speed;
if (speed_unit.empty()) {
Expand Down

0 comments on commit 26199a4

Please sign in to comment.