console-rs / indicatif

A command line progress reporting library for Rust
MIT License
4.21k stars 238 forks source link

per_sec values not retained when iteration exits early with ProgressFinish::Abandon #601

Closed DanGonite57 closed 6 months ago

DanGonite57 commented 8 months ago

When progression of a progress bar with .with_finish(ProgressFinish::Abandon) ends early, the position of the bar is retained at the point that it stopped. However, the per_sec keys (per_sec, bytes_per_sec, binary_bytes_per_sec) are all updated as if the rest of the progress had been completed instantly. I would suggest that these values should also be kept as they were when progression was abandoned.

env: rustc 1.73.0 (cc66ad468 2023-10-03)

cargo.toml: indicatif = "0.17.7"

Arbitrary example:

use indicatif::{ProgressBar, ProgressFinish, ProgressIterator, ProgressStyle};
use std::{array, thread::sleep, time::Duration};

fn main() {
    let arr: [usize; 10] = array::from_fn(|x| x);

    let progress_bar = ProgressBar::new(10).with_finish(ProgressFinish::Abandon);
    progress_bar.set_style(
        ProgressStyle::default_bar()
            .template(
                "[{elapsed_precise}] {bar:60} {pos:>7}/{len:7} {per_sec:>8} {bytes_per_sec:>8} {binary_bytes_per_sec:>8}",
            )
            .unwrap(),
    );
    arr.iter()
        .map(|x| {
            sleep(Duration::new(1, 0));
            x
        })
        .progress_with(progress_bar)
        .find(|&&x| x == 9);

    let progress_bar = ProgressBar::new(10).with_finish(ProgressFinish::Abandon);
    progress_bar.set_style(
        ProgressStyle::default_bar()
            .template(
                "[{elapsed_precise}] {bar:60} {pos:>7}/{len:7} {per_sec:>8} {bytes_per_sec:>8} {binary_bytes_per_sec:>8}",
            )
            .unwrap(),
    );
    arr.iter()
        .map(|x| {
            sleep(Duration::new(1, 0));
            x
        })
        .progress_with(progress_bar)
        .find(|&&x| x == 4);
}

Output:

[00:00:10] ████████████████████████████████████████████████████████████      10/10      0.9925/s    0 B/s    0 B/s
[00:00:05] ██████████████████████████████░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░       5/10      1.9868/s    1 B/s    1 B/s
djc commented 7 months ago

Your suggestion makes sense but I probably won't have time to look at an implementation soon. However, if you're able to dig in and submit a PR I'd be happy to answer any questions you have and review your code.

DanGonite57 commented 7 months ago

Hi, thanks for your reply. I appear to have found a simple solution by using the current position of the progress bar rather than the total length, which corrects the behaviour of Abandoned progress bars, but maintains the current behaviour for AndLeave since their pos is updated on completion.