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

Only tick if the ticker is disabled #458

Merged
merged 2 commits into from Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 9 additions & 4 deletions src/progress_bar.rs
Expand Up @@ -191,9 +191,13 @@ 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)
}
}

Expand All @@ -202,7 +206,7 @@ impl ProgressBar {
self.pos.inc(delta);
let now = Instant::now();
if self.pos.allow(now) {
self.state().tick(now);
self.tick_inner(now);
}
}

Expand Down Expand Up @@ -233,15 +237,16 @@ impl ProgressBar {

/// Update the `ProgressBar`'s inner [`ProgressState`]
pub fn update(&self, f: impl FnOnce(&mut ProgressState)) {
self.state().update(Instant::now(), f)
self.state()
.update(Instant::now(), f, self.ticker.lock().unwrap().is_none())
}

/// Sets the position of the progress bar
pub fn set_position(&self, pos: u64) {
self.pos.set(pos);
let now = Instant::now();
if self.pos.allow(now) {
self.state().tick(now);
self.tick_inner(now);
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/state.rs
Expand Up @@ -84,9 +84,11 @@ impl BarState {
}
}

pub(crate) fn update(&mut self, now: Instant, f: impl FnOnce(&mut ProgressState)) {
pub(crate) fn update(&mut self, now: Instant, f: impl FnOnce(&mut ProgressState), tick: bool) {
f(&mut self.state);
self.tick(now);
if tick {
self.tick(now);
}
}

pub(crate) fn set_length(&mut self, now: Instant, len: u64) {
Expand Down
2 changes: 1 addition & 1 deletion tests/render.rs
Expand Up @@ -339,7 +339,7 @@ fn ticker_drop() {
.with_finish(ProgressFinish::AndLeave)
.with_message(format!("doing stuff {}", i)),
);
new_spinner.enable_steady_tick(Duration::from_millis(50));
new_spinner.enable_steady_tick(Duration::from_millis(100));
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There was a test failure on Windows only, so I'm guessing that's just a timing artefact?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so

spinner.replace(new_spinner);
}

Expand Down