From 6417492caa546a91ca6f69881d33141ddaf192c2 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Skutnik Date: Wed, 11 Dec 2024 15:34:02 +0300 Subject: [PATCH] Fix double prints (#671) --- src/draw_target.rs | 21 +++++++++++++-------- tests/render.rs | 25 +++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/draw_target.rs b/src/draw_target.rs index 7d020c1f..6c107d51 100644 --- a/src/draw_target.rs +++ b/src/draw_target.rs @@ -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; } } } diff --git a/tests/render.rs b/tests/render.rs index b05af145..5a8546ef 100644 --- a/tests/render.rs +++ b/tests/render.rs @@ -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);