Skip to content

Commit

Permalink
Fix double prints (#671)
Browse files Browse the repository at this point in the history
  • Loading branch information
spoutn1k authored Dec 11, 2024
1 parent d8a0666 commit 6417492
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
21 changes: 13 additions & 8 deletions src/draw_target.rs
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,19 @@ impl std::ops::DerefMut for DrawStateWrapper<'_> {

impl Drop for DrawStateWrapper<'_> {
fn drop(&mut self) {
if let Some(orphaned) = &mut self.orphan_lines {
orphaned.extend(
self.state
.lines
.iter()
.filter(|l| matches!(l, LineType::Text(_) | LineType::Empty))
.cloned(),
);
if let Some(text_lines) = &mut self.orphan_lines {
// Filter out the lines that do not contain progress information
// Store the filtered out lines in orphaned
let mut lines = Vec::new();

for line in self.state.lines.drain(..) {
match &line {
LineType::Text(_) | LineType::Empty => text_lines.push(line),
_ => lines.push(line),
}
}

self.state.lines = lines;
}
}
}
Expand Down
25 changes: 25 additions & 0 deletions tests/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,31 @@ Flush
);
}

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

let pb = mp.add(ProgressBar::with_draw_target(
Some(10),
ProgressDrawTarget::term_like(Box::new(in_mem.clone())),
));

assert_eq!(in_mem.contents(), "");

pb.println("message printed :)");
pb.inc(2);
assert_eq!(
in_mem.contents(),
r#"
message printed :)
███████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 2/10
"#
.trim()
);
}

#[test]
fn ticker_drop() {
let in_mem = InMemoryTerm::new(10, 80);
Expand Down

0 comments on commit 6417492

Please # to comment.