Skip to content

Commit

Permalink
Factor out a bit of duplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Aug 3, 2022
1 parent 516c0dc commit 626ebdb
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/progress_bar.rs
Expand Up @@ -191,18 +191,22 @@ impl ProgressBar {
///
/// This automatically happens on any other change to a progress bar.
pub fn tick(&self) {
self.tick_inner(Instant::now());
}

fn tick_inner(&self, now: Instant) {
// Only tick if a `Ticker` isn't installed
if self.ticker.lock().unwrap().is_none() {
self.state().tick(Instant::now())
self.state().tick(now)
}
}

/// Advances the position of the progress bar by `delta`
pub fn inc(&self, delta: u64) {
self.pos.inc(delta);
let now = Instant::now();
if self.pos.allow(now) && self.ticker.lock().unwrap().is_none() {
self.state().tick(now);
if self.pos.allow(now) {
self.tick_inner(now);
}
}

Expand Down Expand Up @@ -241,8 +245,8 @@ impl ProgressBar {
pub fn set_position(&self, pos: u64) {
self.pos.set(pos);
let now = Instant::now();
if self.pos.allow(now) && self.ticker.lock().unwrap().is_none() {
self.state().tick(now);
if self.pos.allow(now) {
self.tick_inner(now);
}
}

Expand Down

0 comments on commit 626ebdb

Please sign in to comment.