From de9862bd2e35e2ca9eee375db27cc393a2fd0b2f Mon Sep 17 00:00:00 2001 From: Lussac Zheng Date: Mon, 6 Jun 2022 16:00:40 +0800 Subject: [PATCH] fix: first style of wide elements was ignored ref: #431, 5038ba8019bad296b5a0314770db5d05c050a7dd --- src/style.rs | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) diff --git a/src/style.rs b/src/style.rs index ec0768d1..e8cd0860 100644 --- a/src/style.rs +++ b/src/style.rs @@ -321,12 +321,6 @@ impl ProgressStyle { } }; - if buf == "\x00" { - // Don't expand for wide elements - cur.push_str(&buf); - continue; - } - match width { Some(width) => { let padded = PaddedStringDisplay { @@ -759,4 +753,37 @@ mod tests { style.format_state(&state, &mut buf, WIDTH); assert_eq!(&buf[0], "fghijklmno"); } + + #[test] + fn wide_element_style() { + const CHARS: &str = "=>-"; + const WIDTH: u16 = 8; + let pos = Arc::new(AtomicPosition::new()); + // half finished + pos.set(2); + let state = ProgressState::new(Some(4), pos); + let mut buf = Vec::new(); + + let style = ProgressStyle::with_template("{wide_bar}") + .unwrap() + .progress_chars(CHARS); + style.format_state(&state, &mut buf, WIDTH); + assert_eq!(&buf[0], "====>---"); + + buf.clear(); + let style = ProgressStyle::with_template("{wide_bar:.red.on_blue/green.on_cyan}") + .unwrap() + .progress_chars(CHARS); + style.format_state(&state, &mut buf, WIDTH); + assert_eq!( + &buf[0], + "\u{1b}[31m\u{1b}[44m====>\u{1b}[32m\u{1b}[46m---\u{1b}[0m\u{1b}[0m" + ); + + buf.clear(); + let mut style = ProgressStyle::with_template("{wide_msg:^.red.on_blue}").unwrap(); + style.message = "foobar".into(); + style.format_state(&state, &mut buf, WIDTH); + assert_eq!(&buf[0], "\u{1b}[31m\u{1b}[44m foobar \u{1b}[0m"); + } }