Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Long progress bar results in new-lines #144

Closed
ceranco opened this issue Dec 27, 2019 · 5 comments
Closed

Long progress bar results in new-lines #144

ceranco opened this issue Dec 27, 2019 · 5 comments

Comments

@ceranco
Copy link

ceranco commented Dec 27, 2019

Description

  • OS: Windows 10
  • Indicateif version: 0.13.0
  • Shells: CMD, Git Bash and Powershell

When using a ProgressBar that is longer than the width of the terminal, the progress bar doesn't clear itself, but instead prints a new line for each update.

CMD

cmd

Git Bash

git-bash

Powershell

powershell

The above gifs show the following code:

use std::cmp::min;
use std::thread;
use std::time::Duration;

use indicatif::{ProgressBar, ProgressStyle};

fn main() {
    let mut downloaded = 0;
    let total_size = 231231231;

    let pb = ProgressBar::new(total_size);
    pb.set_style(ProgressStyle::default_bar()
        .template("{msg:>12.cyan.bold} {spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {bytes}/{total_bytes} ({bytes_per_sec}, {eta})")
        .progress_chars("#>-"));

    pb.set_message("Downloading");
    while downloaded < total_size {
        let new = min(downloaded + 223211, total_size);
        downloaded = new;
        pb.set_position(new);
        thread::sleep(Duration::from_millis(1));
    }

    pb.set_style(
        ProgressStyle::default_bar().template("{msg:>12.green.bold} downloading {total_bytes:.green} in {elapsed:.green}"),
    );
    pb.finish_with_message("Finished");
}

P.S. - This library is awesome! Thanks for making it 😄

@mibac138
Copy link
Contributor

You can use wide_bar instead of bar to make it automatically adjust it's size to the terminal size so that the line doesn't overflow. FYI there are currently two wide elements:

  • wide_bar which adjusts the bar's width to avoid line overflows
  • wide_msg which truncates the message

Keep in mind that there can only be one wide element per ProgressBar.

@ceranco
Copy link
Author

ceranco commented Dec 28, 2019

Thanks for your reply @mibac138.

Unfortunately, even when using wide_bar the line overflows, as the line is longer than the width of the console even when the bar is zero-width:
powershell-wide

Do you have any suggestions?

@mibac138
Copy link
Contributor

Unfortunately, I think your best bet is to just shorten the style of the progress bar or expand your terminal. Alternatively you could also somewhat abuse the system and use two progressbars instead of one. Code:

use std::cmp::min;
use std::thread;
use std::time::Duration;

use indicatif::{ProgressBar, ProgressStyle, MultiProgress};

fn main() {
    let mut downloaded = 0;
    let total_size = 231231231;

    let mpb = MultiProgress::new();
    let pb = mpb.add(ProgressBar::new(total_size));
    pb.set_style(ProgressStyle::default_bar()
        .template("{msg:>12.cyan.bold} {spinner:.green} [{elapsed_precise}] [{wide_bar:.cyan/blue}]")
        .progress_chars("#>-"));
    let pb2 = mpb.add(ProgressBar::new(total_size));
    pb2.set_style(ProgressStyle::default_bar()
        .template("{_:>15}{bytes}/{total_bytes} ({bytes_per_sec}, {eta})"));

    let handle = std::thread::spawn(move || {
        mpb.join().unwrap();
    });

    pb.set_message("Downloading");
    while downloaded < total_size {
        let new = min(downloaded + 223211, total_size);
        downloaded = new;
        pb.set_position(new);
        pb2.set_position(new);
        thread::sleep(Duration::from_millis(1));
    }

    pb.set_style(
        ProgressStyle::default_bar().template("{msg:>12.green.bold} downloading {total_bytes:.green} in {elapsed:.green}"),
    );
    pb.finish_with_message("Finished");
    pb2.finish_and_clear();
    handle.join().unwrap();
}

Output:
2019-12-28 18-59-20

@ceranco
Copy link
Author

ceranco commented Dec 29, 2019

I think that I'll use a shorter template if the console isn't wide enough.
Thanks for your help!

@ceranco ceranco closed this as completed Dec 29, 2019
@sourcefrog
Copy link
Contributor

I just hit this too. In my case wide_message seems to have fixed it(?)

It doesn't seem like anyone would actually want the behavior of lines scrolling down the screen if the message is too long for the terminal. Shouldn't truncation be the default?

It would also be really nice to avoid apps using this library needing to reason about terminal width themselves, which can otherwise be entirely delegated to indicatif.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants