indicatif

repository·main·Indexed 26 days ago

https://github.com/console-rs/indicatif

A 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.

Tokens
3.9K
Snippets
4
Records
28
Agent score
86%

What's inside indicatif

  1. Use ParallelProgressIterator with Rayon

    main

    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<_>>();
  2. Feature flags for indicatif

    main

    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.
  3. Integrate indicatif with the log crate

    main
    To prevent progress bars and log messages from interfering with each other in the terminal, use the indicatif-log-bridge crate. This allows you to integrate indicatif with the standard log crate ecosystem.
  4. Integrate indicatif with the tracing crate

    main
    To use 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.
  5. Use ProgressIterator with standard iterators

    main

    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() {
        // ...
    }
  6. Format counts with comma separators

    main

    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}).
  7. Format bytes using different prefix systems

    main

    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.).
  8. Configure the ProgressDrawTarget

    main

    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:

    • Stdout/Stderr: Standard buffered output with a default refresh rate of 20Hz.
    • Custom Refresh Rate: Specify a custom frequency in Hz.
    • Terminal: Draw to a specific Term object.
    • TermLike: Draw to any object implementing the TermLike trait (useful for custom implementations).
    • Hidden: Prevents any rendering, which is useful when piping output to a file or when the terminal is not user-attended.
  9. Enable steady ticks for slow tasks

    main

    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));
  10. Wrap an iterator with progress reporting using `ProgressIterator`

    main

    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.
  11. Print logs without breaking the progress bar

    main

    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.