Make the progress bar throttle effective after an initial delay.

This commit is contained in:
jcamiel 2024-04-22 11:48:52 +02:00
parent fa46a56430
commit cedd86768c
No known key found for this signature in database
GPG Key ID: 07FF11CFD55356CC

View File

@ -48,6 +48,8 @@ pub enum Mode {
/// The minimum duration between two progress bar redraw (to avoid flickering). /// The minimum duration between two progress bar redraw (to avoid flickering).
const UPDATE_INTERVAL: Duration = Duration::from_millis(100); const UPDATE_INTERVAL: Duration = Duration::from_millis(100);
/// The minimum duration for the progress bar to be throttle (some delay to let the UI stabilize)
const FIRST_THROTTLE: Duration = Duration::from_millis(16);
impl ParProgress { impl ParProgress {
/// Creates a new instance. /// Creates a new instance.
@ -56,7 +58,7 @@ impl ParProgress {
max_running_displayed, max_running_displayed,
mode, mode,
color, color,
throttle: Throttle::new(UPDATE_INTERVAL), throttle: Throttle::new(UPDATE_INTERVAL, FIRST_THROTTLE),
} }
} }
@ -155,18 +157,24 @@ impl Mode {
/// just putting stuff onto the terminal. We also want to avoid flickering by not drawing anything /// just putting stuff onto the terminal. We also want to avoid flickering by not drawing anything
/// that goes away too quickly. /// that goes away too quickly.
struct Throttle { struct Throttle {
/// Creation time of the progress.
start: Instant,
/// Last time the progress bar has be refreshed on the terminal. /// Last time the progress bar has be refreshed on the terminal.
last_update: Option<Instant>, last_update: Option<Instant>,
/// Refresh interval /// Refresh interval
interval: Duration, interval: Duration,
/// First interval of non throttle to let the UI initialize
first_throttle: Duration,
} }
impl Throttle { impl Throttle {
/// Creates a new instances. /// Creates a new instances.
fn new(interval: Duration) -> Self { fn new(interval: Duration, first_throttle: Duration) -> Self {
Throttle { Throttle {
start: Instant::now(),
last_update: None, last_update: None,
interval, interval,
first_throttle,
} }
} }
@ -179,6 +187,9 @@ impl Throttle {
} }
fn update(&mut self) { fn update(&mut self) {
if self.start.elapsed() < self.first_throttle {
return;
}
self.last_update = Some(Instant::now()); self.last_update = Some(Instant::now());
} }