Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature request] adding {percent_precise} style key #628

Merged
merged 1 commit into from Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/lib.rs
Expand Up @@ -190,7 +190,8 @@
//! * `len`: renders the amount of work to be done as an integer
//! * `human_len`: renders the total length of the bar as an integer, with commas as the thousands
//! separator.
//! * `percent`: renders the current position of the bar as a percentage of the total length.
//! * `percent`: renders the current position of the bar as a percentage of the total length (as an integer).
//! * `percent_precise`: renders the current position of the bar as a percentage of the total length (with 3 fraction digits).
//! * `bytes`: renders the current position of the bar as bytes (alias of `binary_bytes`).
//! * `total_bytes`: renders the total length of the bar as bytes (alias of `binary_total_bytes`).
//! * `decimal_bytes`: renders the current position of the bar as bytes using
Expand Down
3 changes: 3 additions & 0 deletions src/style.rs
Expand Up @@ -282,6 +282,9 @@ impl ProgressStyle {
"percent" => buf
.write_fmt(format_args!("{:.*}", 0, state.fraction() * 100f32))
.unwrap(),
"percent_precise" => buf
.write_fmt(format_args!("{:.*}", 3, state.fraction() * 100f32))
.unwrap(),
"bytes" => buf.write_fmt(format_args!("{}", HumanBytes(pos))).unwrap(),
"total_bytes" => {
buf.write_fmt(format_args!("{}", HumanBytes(len))).unwrap();
Expand Down
33 changes: 33 additions & 0 deletions tests/render.rs
Expand Up @@ -94,6 +94,39 @@ fn progress_bar_percent_with_no_length() {
);
}

#[test]
fn progress_bar_percent_precise_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_precise}%").unwrap());

assert_eq!(in_mem.contents(), String::new());

pb.tick();

assert_eq!(
in_mem.contents(),
"░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 0.000%"
);

pb.set_length(10);

pb.inc(1);
assert_eq!(
in_mem.contents(),
"███████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░ 10.000%"
);

pb.finish();
assert_eq!(
in_mem.contents(),
"███████████████████████████████████████████████████████████████████████ 100.000%"
);
}

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