Skip to content

Commit

Permalink
Use presence of non-empty NO_COLOR environment variable to decide if …
Browse files Browse the repository at this point in the history
…terminal suports ANSI.
  • Loading branch information
aaronriekenberg committed Mar 3, 2024
1 parent cda4fe0 commit 10d6019
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<ProgressBar>,
}
Expand All @@ -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);
Expand Down

0 comments on commit 10d6019

Please sign in to comment.