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

Fix Ticker issue #397 #407

Merged
merged 3 commits into from
Mar 21, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/in_memory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ impl InMemoryTerm {
.into_iter()
.rev()
.skip_while(|line| line.is_empty())
.map(|line| line.trim_end().to_string())
.collect();

// Un-reverse the rows and join them up with newlines
Expand Down
1 change: 1 addition & 0 deletions src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ impl Ticker {
self.interval = interval;
state.draw(false, Instant::now()).ok();
drop(state); // Don't forget to drop the lock before sleeping
drop(arc); // Also need to drop Arc otherwise BarState won't be dropped
thread::sleep(self.interval);
}
}
Expand Down
26 changes: 26 additions & 0 deletions tests/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use indicatif::{
InMemoryTerm, MultiProgress, ProgressBar, ProgressDrawTarget, ProgressFinish, ProgressStyle,
TermLike,
};
use std::time::Duration;

#[test]
fn basic_progress_bar() {
Expand Down Expand Up @@ -242,3 +243,28 @@ Another line printed"#
.trim()
);
}

#[test]
fn ticker_drop() {
let in_mem = InMemoryTerm::new(10, 80);
let mp =
MultiProgress::with_draw_target(ProgressDrawTarget::term_like(Box::new(in_mem.clone())));

let mut spinner: Option<ProgressBar> = None;

for i in 0..5 {
let new_spinner = mp.add(
ProgressBar::new_spinner()
.with_finish(ProgressFinish::AndLeave)
.with_message(format!("doing stuff {}", i)),
);
new_spinner.enable_steady_tick(Duration::from_millis(50));
spinner.replace(new_spinner);
}

drop(spinner);
assert_eq!(
in_mem.contents(),
" doing stuff 0\n doing stuff 1\n doing stuff 2\n doing stuff 3\n doing stuff 4"
);
}