use clap::ColorChoice;
use std::ffi::OsString;
pub trait ColorChoiceExt {
fn to_bool(&self) -> bool {
use std::{
env,
io::{stdout, IsTerminal},
};
match self.as_color_choice() {
ColorChoice::Always => true,
ColorChoice::Never => false,
ColorChoice::Auto => {
stdout().is_terminal()
&& !env::var_os("NO_COLOR").is_some_and(|value| !value.is_empty())
}
}
}
fn as_color_choice(&self) -> &ColorChoice;
}
impl ColorChoiceExt for ColorChoice {
fn as_color_choice(&self) -> &ColorChoice {
self
}
}
pub fn color_choice(args: &[OsString]) -> ColorChoice {
let mut choice = ColorChoice::Auto;
let mut args = args.iter().filter_map(|arg| arg.to_str());
while let Some(arg) = args.next() {
let value = if arg == "--color" {
args.next()
} else {
arg.strip_prefix("--color=")
};
if let Some(value) = value {
choice = value.parse().unwrap_or(choice);
}
}
choice
}