Skip to content
Open
Changes from 2 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
23 changes: 22 additions & 1 deletion src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl ProgressStyle {
buf.write_fmt(format_args!("{}", HumanCount(len))).unwrap();
}
"percent" => buf
.write_fmt(format_args!("{:.*}", 0, state.fraction() * 100f32))
.write_fmt(format_args!("{:.*}", 0, (state.fraction() * 100f32).floor()))
.unwrap(),
"percent_precise" => buf
.write_fmt(format_args!("{:.*}", 3, state.fraction() * 100f32))
Expand Down Expand Up @@ -1055,4 +1055,25 @@ mod tests {
assert_eq!(&buf[2], "bar");
assert_eq!(&buf[3], "baz");
}

#[test]
fn test_pct_precision() {
// seeing something that's not finished as 100% hurts my eyes
const WIDTH: u16 = 80;
let pos = Arc::new(AtomicPosition::new());
pos.set(358);
let state = ProgressState::new(Some(359), pos.clone());

let style = ProgressStyle::default_bar()
.template(
&format!("{{pos}}/{{len}} {{percent}}%"),
).unwrap();
let mut buf = Vec::new();
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "358/359 99%");
buf.clear();
pos.set(359);
style.format_state(&state, &mut buf, WIDTH);
assert_eq!(&buf[0], "359/359 100%");
}
}
Loading