From 6685477bfed05167df31f5ede62c79753478a0ae Mon Sep 17 00:00:00 2001 From: Cosimo Matteini Date: Sat, 5 Nov 2022 18:46:40 +0100 Subject: [PATCH 1/2] Fix percent initial value when there is no length Read more on issue #489 --- src/state.rs | 2 +- tests/render.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/state.rs b/src/state.rs index d7dfee7f..c4c06a08 100644 --- a/src/state.rs +++ b/src/state.rs @@ -245,7 +245,7 @@ impl ProgressState { pub fn fraction(&self) -> f32 { let pos = self.pos.pos.load(Ordering::Relaxed); let pct = match (pos, self.len) { - (_, None) => 1.0, + (_, None) => 0.0, (_, Some(0)) => 1.0, (0, _) => 0.0, (pos, Some(len)) => pos as f32 / len as f32, diff --git a/tests/render.rs b/tests/render.rs index e3105e17..b55f1867 100644 --- a/tests/render.rs +++ b/tests/render.rs @@ -60,6 +60,39 @@ fn progress_bar_builder_method_order() { ); } +#[test] +fn progress_bar_percent_with_no_length() { + let in_mem = InMemoryTerm::new(10, 80); + let pb = ProgressBar::with_draw_target( + None, + ProgressDrawTarget::term_like(Box::new(in_mem.clone())), + ) + .with_style(ProgressStyle::with_template("{wide_bar} {percent}%").unwrap()); + + assert_eq!(in_mem.contents(), String::new()); + + pb.tick(); + + assert_eq!( + in_mem.contents(), + "░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0%" + ); + + pb.set_length(10); + + pb.inc(1); + assert_eq!( + in_mem.contents(), + "███████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 10%" + ); + + pb.finish(); + assert_eq!( + in_mem.contents(), + "███████████████████████████████████████████████████████████████████████████ 100%" + ); +} + #[test] fn multi_progress_single_bar_and_leave() { let in_mem = InMemoryTerm::new(10, 80); From ce4a29c01eeb638130437b8ef98bed97577ba912 Mon Sep 17 00:00:00 2001 From: Cosimo Matteini Date: Sun, 6 Nov 2022 14:48:19 +0100 Subject: [PATCH 2/2] Fix clippy lints --- src/in_memory.rs | 8 ++++---- src/style.rs | 8 ++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/in_memory.rs b/src/in_memory.rs index 1ea16555..7d3d94e6 100644 --- a/src/in_memory.rs +++ b/src/in_memory.rs @@ -67,7 +67,7 @@ impl TermLike for InMemoryTerm { .state .lock() .unwrap() - .write_str(&*format!("\x1b[{}A", n)), + .write_str(&format!("\x1b[{}A", n)), } } @@ -78,7 +78,7 @@ impl TermLike for InMemoryTerm { .state .lock() .unwrap() - .write_str(&*format!("\x1b[{}B", n)), + .write_str(&format!("\x1b[{}B", n)), } } @@ -89,7 +89,7 @@ impl TermLike for InMemoryTerm { .state .lock() .unwrap() - .write_str(&*format!("\x1b[{}C", n)), + .write_str(&format!("\x1b[{}C", n)), } } @@ -100,7 +100,7 @@ impl TermLike for InMemoryTerm { .state .lock() .unwrap() - .write_str(&*format!("\x1b[{}D", n)), + .write_str(&format!("\x1b[{}D", n)), } } diff --git a/src/style.rs b/src/style.rs index cb24ab1d..892b1f9b 100644 --- a/src/style.rs +++ b/src/style.rs @@ -190,11 +190,7 @@ impl ProgressStyle { let entirely_filled = fill as usize; // 1 if the bar is not entirely empty or full (meaning we need to draw the "current" // character between the filled and "to do" segment), 0 otherwise. - let head = if fill > 0.0 && entirely_filled < width { - 1 - } else { - 0 - }; + let head = usize::from(fill > 0.0 && entirely_filled < width); let cur = if head == 1 { // Number of fine-grained progress entries in progress_chars. @@ -423,7 +419,7 @@ impl<'a> WideElement<'a> { buf: &mut String, width: u16, ) -> String { - let left = (width as usize).saturating_sub(measure_text_width(&*cur.replace('\x00', ""))); + let left = (width as usize).saturating_sub(measure_text_width(&cur.replace('\x00', ""))); match self { Self::Bar { alt_style } => cur.replace( '\x00',