How to use indicatif for progress bars

Cli
Add indicatif to Cargo.toml, create a ProgressBar with a custom style, and update its position in a loop to show progress.

Add indicatif to your Cargo.toml dependencies, import ProgressBar and ProgressStyle, then instantiate a bar, set its length, and update it in a loop.

[dependencies]
indicatif = "0.18.3"
use indicatif::{ProgressBar, ProgressStyle};

fn main() {
    let total = 100;
    let pb = ProgressBar::new(total);
    pb.set_style(
        ProgressStyle::with_template("{spinner:.green} [{elapsed_precise}] [{bar:40.cyan/blue}] {pos}/{len}")
            .unwrap()
            .progress_chars("##-"),
    );

    for i in 0..total {
        // Simulate work
        std::thread::sleep(std::time::Duration::from_millis(50));
        pb.set_position(i + 1);
    }

    pb.finish_with_message("Done!");
}