diff --git a/cli/src/progress.rs b/cli/src/progress.rs index 55b4dc0be1..e3abf2ee8b 100644 --- a/cli/src/progress.rs +++ b/cli/src/progress.rs @@ -61,7 +61,15 @@ impl Progress { _ = write!(output, "{}", crossterm::cursor::Hide); self.guard = Some(guard); } - self.next_print = now.min(self.next_print + Duration::from_secs(1) / UPDATE_HZ); + let period = Duration::from_secs(1) / UPDATE_HZ; + // Try to print periodically by setting the new threshold exactly one period + // later + self.next_print = self.next_print + period; + // But if it's been long enough since last update that we'd effectively print + // two updates now, reset the threshold to now. + if now >= self.next_print { + self.next_print = now + period; + } self.buffer.clear(); write!(self.buffer, "\r").unwrap(); @@ -260,14 +268,14 @@ mod tests { // First output is after the initial delay assert_snapshot!(update(INITIAL_DELAY - Duration::from_millis(1), 0.1), @""); assert_snapshot!(update(Duration::from_millis(1), 0.10), @"[?25l\r 10% [█▊ ]"); - // TODO: No updates for the next 30 milliseconds - assert_snapshot!(update(Duration::from_millis(10), 0.11), @" 11% [██ ]"); - assert_snapshot!(update(Duration::from_millis(10), 0.12), @" 12% [██▏ ]"); - assert_snapshot!(update(Duration::from_millis(10), 0.13), @" 13% [██▍ ]"); + // No updates for the next 30 milliseconds + assert_snapshot!(update(Duration::from_millis(10), 0.11), @""); + assert_snapshot!(update(Duration::from_millis(10), 0.12), @""); + assert_snapshot!(update(Duration::from_millis(10), 0.13), @""); // We get an update now that we go over the threshold assert_snapshot!(update(Duration::from_millis(100), 0.30), @" 30% [█████▍ ]"); - // TODO: Even though we went over by quite a bit, the new threshold is relative - // to the previous output, so we don't get an update here - assert_snapshot!(update(Duration::from_millis(30), 0.40), @" 40% [███████▎ ]"); + // Even though we went over by quite a bit, the new threshold is relative to the + // previous output, so we don't get an update here + assert_snapshot!(update(Duration::from_millis(30), 0.40), @""); } }