Install indicatif
mainindicatif to your Cargo.toml to use progress bars and spinners in your Rust command line applications.repository·main·Indexed 26 days ago
https://github.com/console-rs/indicatifA progress bar and CLI reporting library for Rust (v0.18.6) providing progress indicators, spinners, and human-readable formatting for durations, bytes, and counts. It includes support for wrapping iterators via ProgressIterator, managing multiple bars with MultiProgress, and integration with the log and tracing crates. Optional features include rayon for parallel iterators, improved unicode support, and WASM compatibility.
indicatif to your Cargo.toml to use progress bars and spinners in your Rust command line applications.If you enable the rayon feature in your Cargo.toml, you can use ParallelProgressIterator to track progress across parallel iterators.
Setup:
[dependencies]
indicatif = {version = "*", features = ["rayon"]}Usage:
use indicatif::{ProgressBar, ParallelProgressIterator, ProgressStyle};
use rayon::iter::{ParallelIterator, IntoParallelRefIterator};
let v: Vec<_> = (0..100000).collect();
// Basic parallel progress
let v2 = v.par_iter().progress_count(v.len() as u64).map(|i| i + 1).collect::<Vec<_>>();
// Custom style parallel progress
let style = ProgressStyle::default_bar();
let v3 = v.par_iter().progress_with_style(style).map(|i| i + 1).collect::<Vec<_>>();The following feature flags are available:
rayon: Adds support for parallel iterators via the rayon crate.improved_unicode: Adds improved unicode support (graphemes, better width calculation).in_memory: Enables InMemoryTerm for testing or in-memory rendering.wasmbind: Required when compiling for wasm32 targets.indicatif-log-bridge crate. This allows you to integrate indicatif with the standard log crate ecosystem.indicatif with the tracing ecosystem, use the tracing-indicatif crate. This provides automatic progress bar management for active tracing spans and ensures that tracing log events do not disrupt active progress bars.You can associate a progress bar with any iterator by using the ProgressIterator trait. This allows you to track progress as you iterate over a collection.
use indicatif::ProgressIterator;
for _ in (0..1000).progress() {
// ...
}Use HumanCount or HumanFloatCount to format numbers with thousands separators (commas).
HumanCount: Formats u64 integers (e.g., 1,234,567).HumanFloatCount: Formats f64 floats with comma separators and optional precision (e.g., 1,234.56). It respects standard Rust formatting precision (e.g., {:.2}).Use these wrappers to format u64 byte counts into human-readable strings with appropriate units.
HumanBytes: Uses binary prefixes (KiB, MiB, etc.).DecimalBytes: Uses SI decimal prefixes (kB, MB, etc.).BinaryBytes: Uses ISO/IEC binary prefixes (KiB, MiB, etc.).The ProgressDrawTarget determines where ProgressBar or MultiProgress objects paint their output. It is a stateful wrapper that optimizes drawing frequency to the output device.
Common ways to initialize a draw target include:
Term object.TermLike trait (useful for custom implementations).For tasks that don't update frequently, you can spawn a background thread to regularly refresh the progress bar (e.g., to keep a spinner spinning or update the ETA).
enable_steady_tick(interval): Starts a background thread that ticks the bar at the specified Duration.disable_steady_tick(): Stops the background ticker.use indicatif::ProgressBar;
use std::time::Duration;
let pb = ProgressBar::new_spinner();
// Tick every 100ms
pb.enable_steady_tick(Duration::from_millis(100));The ProgressIterator trait allows you to wrap any standard Rust iterator to automatically update a progress bar as you consume its elements.
Depending on the iterator type, you can use different methods:
progress(): For ExactSizeIterator, uses the iterator's length to initialize the bar.try_progress(): For general iterators, uses size_hint() to attempt to determine length (returns None if length is unknown).progress_count(len): Manually specify the total number of elements.progress_with(progress_bar): Use a pre-configured ProgressBar instance.progress_with_style(style): For ExactSizeIterator, wraps the iterator with a progress bar using a specific ProgressStyle.If you need to print log messages while a progress bar is active, use ProgressBar::suspend or ProgressBar::println.
println(msg): Prints a line above the progress bar. If the bar is part of a MultiProgress, it prints above all bars.suspend(|| { ... }): Temporarily hides the progress bar, executes the provided closure (e.g., to run a standard println!), and then redraws the bar. This is the safest way to ensure logs don't corrupt the terminal UI.