Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow forcing or disabling colors #243

Merged
merged 2 commits into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(),
}
}