console-rs / indicatif

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

Progress Bars over 80 characters move up the screen when stdout is redirected #609

Closed jparris closed 6 months ago

jparris commented 7 months ago

Hi There,

I've encountered a case where if the progress bar length is over 80 characters and stdout is redirected the progress bar will move up the screen overwriting previous output. As can bee seen in the screen recording. Below is screen recording and code snippet.

Reproduction

https://github.com/console-rs/indicatif/assets/125183/cc523dc7-842c-4fc5-a5bf-b42c4b0163ea

Code

Note: The progress line only hit's 81 characters and displays the behavior when we 100 iterations.

use indicatif::{MultiProgress, ProgressBar, ProgressStyle};
use std::thread;
use std::time::Duration;

fn main() {
    let num_jobs: usize = 500;

    let display_manager = MultiProgress::new();
    let pb = display_manager.add(ProgressBar::new(num_jobs as u64));
    pb.set_style(
        ProgressStyle::with_template(
            &format!("{{spinner}} [{{elapsed_precise}}] {{bar:.cyan}} {{pos}}/{{len}} This is a long comment in the progress "),
        )
        .expect("Could not create progress bar style"),
    );
    for _ in 0..num_jobs {
        pb.inc(1);
        thread::sleep(Duration::from_millis(10));
    }

    let _ = display_manager.println("Progress Bar Completed.");
}
djc commented 7 months ago

I think this might be the same as #606?

jparris commented 7 months ago

Hi @djc ,

I just ran a quick experiment using the branch for PR https://github.com/console-rs/indicatif/pull/608 which fixes issue #606 and was able to reproduce the issue.

Cargo.toml for the code snippet above.

name = "indicatif_issue"
version = "0.1.0"
edition = "2021"

[dependencies]
indicatif = {git = "https://github.com/tgolsson/indicatif", branch = "ts/real-len" }
tgolsson commented 7 months ago

Might be related to https://github.com/console-rs/console/issues/185?

jparris commented 7 months ago

Might be related to console-rs/console#185?

@tgolsson great suggestionm, when I used your PR branch it resolved the issue 🎉 . I'm going to close this issue.