console-rs / indicatif

A command line progress reporting library for Rust
MIT License
4.45k stars 243 forks source link

per_sec does not respect width specifier #663

Open rlee287 opened 1 month ago

rlee287 commented 1 month ago

Reproducer (using indicatif 0.17.8, default feature set):

use indicatif::{ProgressBar, ProgressStyle};

fn main() {
    let pb_style = ProgressStyle::default_bar()
        .template("{bar:16} | {pos:>6}/{len:6} | {per_sec:>8}")
        .unwrap();

    let pb = ProgressBar::new(100000).with_style(pb_style);

    for _ in 0..1000 {
        std::thread::sleep(std::time::Duration::from_secs_f64(0.01));
        pb.inc(100);
    }
    pb.finish();
}

The progress bar that's left after the program runs:

████████████████ | 100000/100000 | 9,631.6687/s

Despite per_sec specifying a width of 8 characters, the final per_sec display is 12 characters wide. I would have expected the formatter to trim the number of digits after the decimal point to reduce the width instead.

Compressed asciinema recording of the above program running on my machine:

pb_per_sec.cast.gz

djc commented 1 month ago

Have you tried main? IIRC there have been some improvements in this area.

rlee287 commented 1 month ago

The same problem occurs when I specify a Git dependency for indicatif using the default commit (branch main, commit 5396704f8e9fe781d21a5475c8d3af292d0155fe as of time of writing).

djc commented 4 weeks ago

Hmm, I'm a little skeptical that other width specifiers (either in indicatif or in the Rust formatting machinery) ever trim -- I think they mostly only expand.