Crate cli_progress

Source
Expand description

Dynamic progress and process display library

To use this, construct a CLIDisplayManager, whose output can be changed with the CLIDisplayManager::modify function, and drop it on completion.

While the CLIDisplayManager is in use, no other CLIDisplayManager should be active, however stdout can still be used through the erasing_println macro during modify calls and it will appear in front of the displayed progress/process.

Currently there are three types of displays:

Example with progress bars: cargo run --example progress

use std::{
    array,
    borrow::Cow,
    sync::{
        Arc,
        atomic::{AtomicU8, Ordering},
    },
    thread::sleep,
    time::Duration,
};

use cli_progress::{CLIDisplayManager, CLIDisplayNodeType, erasing_println};
use rand::{Rng, rng};

fn main() {
    // Create CLIDisplayManager at the beginning
    let mut clidm = CLIDisplayManager::new(
        CLIDisplayNodeType::SpinningMessage(Cow::Borrowed("Progress bar example")),
        10,
    );

    // The progress bars are stores as Arc<AtomicU8>
    let bars: [Arc<AtomicU8>; 3] = array::from_fn(|_| Arc::new(AtomicU8::new(0)));

    // The progress bars are initialized in the CLIDisplayManager
    clidm.modify(|modify| {
        for bar in &bars {
            modify.push(CLIDisplayNodeType::ProgressBar(bar.clone()));
        }
    });

    let mut rng = rng();

    // This randomly advances the bars until they all finish
    while !bars.iter().all(|bar| bar.load(Ordering::Relaxed) == 100) {
        let mut rand = 0;

        if rng.random_bool(0.3) || bars[rand].load(Ordering::Relaxed) == 100 {
            rand += 1;
            if rng.random_bool(0.3) || bars[rand].load(Ordering::Relaxed) == 100 {
                rand += 1;
            }
        }

        if bars[rand].fetch_add(1, Ordering::Relaxed) == 99 {
            clidm.modify(|modify| {
                // Notice that this is possible
                erasing_println!(modify, "Bar {} completed!", rand);
            });
        }

        sleep(Duration::from_millis(9));
    }
}

Macros§

erasing_println
This macro can be used in modify calls to add lines to stdout without interrupting the CLIDisplayManager

Structs§

CLIDisplayManager
This is the core struct of the library. Everything is managed here. Create this with the initial root item and a refresh rate and drop it when done.
CLIModificationElement
This is the struct through which the output of a CLIDisplayManager can be changed.

Enums§

CLIDisplayNodeType
All possible display node types.