warren-cli 0.1.6

Install any CLI tool unlimited times. Every instance is its own world.
use indicatif::{ProgressBar, ProgressDrawTarget, ProgressStyle};
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::Duration;

static ENABLED: AtomicBool = AtomicBool::new(true);

/// Enable or disable progress rendering globally (e.g. when not a TTY
/// or when `ui.progress` is false in the config).
pub fn set_enabled(enabled: bool) {
    ENABLED.store(enabled, Ordering::Relaxed);
}

fn enabled() -> bool {
    ENABLED.load(Ordering::Relaxed)
}

fn draw_target() -> ProgressDrawTarget {
    if enabled() {
        ProgressDrawTarget::stderr()
    } else {
        ProgressDrawTarget::hidden()
    }
}

pub fn download_bar(len: u64) -> ProgressBar {
    let pb = ProgressBar::new(len);
    pb.set_draw_target(draw_target());
    pb.set_style(
        ProgressStyle::with_template("     [{bar:44.cyan/dim}] {bytes}/{total_bytes} {msg}")
            .unwrap()
            .progress_chars("━━╌"),
    );
    pb.enable_steady_tick(Duration::from_millis(100));
    pb
}

pub fn spinner(msg: &str) -> ProgressBar {
    let pb = ProgressBar::new_spinner();
    pb.set_draw_target(draw_target());
    pb.set_style(
        ProgressStyle::with_template("  {spinner:.cyan}  {msg}")
            .unwrap()
            .tick_strings(&["", "", "", "", "", "", "", "", "", "", ""]),
    );
    pb.set_message(msg.to_string());
    pb.enable_steady_tick(Duration::from_millis(80));
    pb
}