console-rs / indicatif

A command line progress reporting library for Rust
MIT License
4.23k stars 240 forks source link

Custom `pos` & `len` calculation #466

Open zohnannor opened 1 year ago

zohnannor commented 1 year ago

I am using progress bar like so:

const COUNT: usize = 200;

let count = /* ... */;
for offset in (0..count).step_by(COUNT).progress_with_style(
    ProgressStyle::with_template(
        "[FOR {elapsed} / ETA {eta}] {wide_bar} {pos}/{len} ({percent}%) {msg}",
    )
    .unwrap(),
) {
    send_request(..., offset, ...);
}

I am sending requests to an API with pagination mechanic with offset and count, so that I can retrieve COUNT items each response. (0..count).step_by(COUNT) is helping with that. But this way progress bar displays numbers COUNT times smaller. It would be very helpful if I could set custom pos & len calculation for ProgressStyle. Maybe like so:

    ProgressStyle::with_template(
        "[FOR {elapsed} / ETA {eta}] {wide_bar} {pos}/{len} ({percent}%) {msg}",
    )
    .unwrap()
    .with_pos(|pos| pos * 200)
    .with_len(|len| len * 200),
djc commented 1 year ago

So this seems pretty specific to the iterator-based API, since the underlying ProgressBar API makes it pretty easy to initialize len and use inc() to increase the position in larger steps.

Do you want to submit a PR for this? I'm afraid this is a bit too niche for me to spend much time on.

zohnannor commented 1 year ago

Sure, if you're fine with API I proposed.

djc commented 1 year ago

Seems okay.