diff --git a/src/progress.rs b/src/progress.rs index 2b2c365..c51bb68 100644 --- a/src/progress.rs +++ b/src/progress.rs @@ -11,6 +11,9 @@ use crate::command_line_args::CommandLineArgs; const PROGRESS_STYLE: &str = "{spinner} [{elapsed_precise}] Commands Done/Total: {pos:>2}/{len:2} {wide_bar} ETA {eta_precise}"; +const PROGRESS_STYLE_NO_SPINNER: &str = + "[{elapsed_precise}] Commands Done/Total: {pos:>2}/{len:2} {wide_bar} ETA {eta_precise}"; + pub struct Progress { progress_bar: Option, } @@ -20,10 +23,23 @@ impl Progress { let progress_bar = if !command_line_args.progress_bar { None } else { + // Terminal supports ANSI formatting if NO_COLOR is unset or empty. + // Idea from https://github.com/tokio-rs/tracing/pull/2647 + + let terminal_supports_ansi = std::env::var("NO_COLOR").map_or(true, |v| v.is_empty()); + let progress_bar = ProgressBar::new(0); - progress_bar.enable_steady_tick(Duration::from_millis(100)); + if terminal_supports_ansi { + progress_bar.enable_steady_tick(Duration::from_millis(100)); + } + + let style = if terminal_supports_ansi { + PROGRESS_STYLE + } else { + PROGRESS_STYLE_NO_SPINNER + }; - let style = ProgressStyle::with_template(PROGRESS_STYLE) + let style = ProgressStyle::with_template(style) .context("ProgressStyle::with_template error")?; progress_bar.set_style(style);