Skip to content

Commit

Permalink
feat: allow forcing or disabling colors (#243)
Browse files Browse the repository at this point in the history
* feat: allow forcing or disabling colors

* fix: console library as well
  • Loading branch information
baszalmstra authored Aug 1, 2023
1 parent af2fe14 commit d297e1a
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
27 changes: 24 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ rustls-tls = ["reqwest/rustls-tls", "rattler_repodata_gateway/rustls-tls", "ratt
slow_integration_tests = []

[dependencies]
atty = "0.2"
chrono = "0.4.26"
clap = { version = "4.3.16", default-features = false, features = ["derive", "usage", "wrap_help", "std", "color", "error-context"] }
clap-verbosity-flag = "2.0.1"
Expand Down
46 changes: 46 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ struct Args {
/// (-v for verbose, -vv for debug, -vvv for trace, -q for quiet)
#[command(flatten)]
verbose: Verbosity,

/// Whether the log needs to be colored.
#[clap(long, default_value = "auto", global = true)]
color: ColorOutput,
}

/// Generates a completion script for a shell.
Expand Down Expand Up @@ -100,6 +104,20 @@ fn completion(args: CompletionCommand) -> miette::Result<()> {

pub async fn execute() -> miette::Result<()> {
let args = Args::parse();
let use_colors = use_color_output(&args);

// Setup the default miette handler based on whether or not we want colors or not.
miette::set_hook(Box::new(move |_| {
Box::new(
miette::MietteHandlerOpts::default()
.color(use_colors)
.build(),
)
}))?;

// Enable disable colors for the colors crate
console::set_colors_enabled(use_colors);
console::set_colors_enabled_stderr(use_colors);

let level_filter = match args.verbose.log_level_filter() {
clap_verbosity_flag::LevelFilter::Off => LevelFilter::OFF,
Expand All @@ -121,6 +139,7 @@ pub async fn execute() -> miette::Result<()> {

// Setup the tracing subscriber
tracing_subscriber::fmt()
.with_ansi(use_colors)
.with_env_filter(env_filter)
.with_writer(IndicatifWriter::new(progress::global_multi_progress()))
.without_time()
Expand Down Expand Up @@ -148,3 +167,30 @@ pub async fn execute_command(command: Command) -> miette::Result<()> {
Command::Upload(cmd) => upload::execute(cmd).await,
}
}

/// Whether to use colored log format.
/// Option `Auto` enables color output only if the logging is done to a terminal and `NO_COLOR`
/// environment variable is not set.
#[derive(clap::ValueEnum, Debug, Clone, Default)]
pub enum ColorOutput {
Always,
Never,

#[default]
Auto,
}

/// Returns true if the output is considered to be a terminal.
fn is_terminal() -> bool {
// Crate `atty` provides a platform-independent way of checking whether the output is a tty.
atty::is(atty::Stream::Stderr)
}

/// Returns true if the log outputs should be colored or not.
fn use_color_output(args: &Args) -> bool {
match args.color {
ColorOutput::Always => true,
ColorOutput::Never => false,
ColorOutput::Auto => std::env::var_os("NO_COLOR").is_none() && is_terminal(),
}
}

0 comments on commit d297e1a

Please sign in to comment.