Skip to content

Commit

Permalink
[feature request] adding {percent_precise} style key
Browse files Browse the repository at this point in the history
The existing `{percent}` is rendered as an integer, which means it is not very useful for large data sets, as it rarely changes.

I suggest adding a new `{percent_precise}` (a non-breaking change?) that would render it with additional precision. For example, 3 fraction digits, as demonstrated by the test added here.

An earlier proposal to add precision to variables was rejected (#552), and AFAIK, there is no way to customize this without taking over rendering.

I think it would work well with existing variables like `elapsed_precise`, `eta_precise`.

Thanks for considering!
  • Loading branch information
OmarTawfik authored and djc committed Feb 8, 2024
1 parent 44618a8 commit f8d33f9
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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

0 comments on commit f8d33f9

Please sign in to comment.