use clap::ValueEnum;
use std::ffi::{OsStr, OsString};
use std::io::IsTerminal;
#[derive(Copy, Clone, Debug, Default, PartialEq, Eq, ValueEnum)]
pub enum ColorChoice {
#[default]
Auto,
Always,
Never,
}
pub(crate) fn resolve_color(
choice: ColorChoice,
stderr_tty: bool,
no_color: Option<&OsStr>,
) -> bool {
match choice {
ColorChoice::Always => true,
ColorChoice::Never => false,
ColorChoice::Auto => stderr_tty && no_color.is_none(),
}
}
pub(crate) fn color_enabled_from(
choice: ColorChoice,
stderr_tty: bool,
lookup: &dyn Fn(&str) -> Option<OsString>,
) -> bool {
resolve_color(choice, stderr_tty, lookup("NO_COLOR").as_deref())
}
pub(crate) fn color_enabled(choice: ColorChoice) -> bool {
color_enabled_from(choice, std::io::stderr().is_terminal(), &|key| {
std::env::var_os(key)
})
}
fn parse_boolish(raw: &str) -> Option<bool> {
match raw.to_lowercase().as_str() {
"y" | "yes" | "t" | "true" | "on" | "1" => Some(true),
"n" | "no" | "f" | "false" | "off" | "0" => Some(false),
_ => None,
}
}
pub(crate) const QUIET_FLAG: &str = "--quiet";
pub(crate) const QUIET_SHORT: &str = "-q";
pub(crate) const QUIET_ENV: &str = "ASSAY_QUIET";
pub(crate) const COLOR_FLAG: &str = "--color";
pub(crate) const COLOR_ENV: &str = "ASSAY_COLOR";
pub(crate) const NON_INTERACTIVE_FLAG: &str = "--non-interactive";
pub(crate) const NON_INTERACTIVE_ENV: &str = "ASSAY_NON_INTERACTIVE";
pub(crate) fn parse_quiet_env(raw: Option<&OsStr>) -> Result<bool, String> {
let Some(raw) = raw else {
return Ok(false);
};
if raw.is_empty() {
return Ok(false);
}
let text = raw.to_str().ok_or_else(|| {
format!(
"invalid {QUIET_ENV} value {}: expected a boolean (1/0, true/false, yes/no, on/off)",
raw.to_string_lossy()
)
})?;
parse_boolish(text).ok_or_else(|| {
format!(
"invalid {QUIET_ENV} value {text:?}: \
expected a boolean (1/0, true/false, yes/no, on/off)"
)
})
}
pub(crate) fn parse_color_env(raw: Option<&OsStr>) -> Result<Option<ColorChoice>, String> {
let Some(raw) = raw else {
return Ok(None);
};
if raw.is_empty() {
return Ok(None);
}
let text = raw.to_str().ok_or_else(|| {
format!(
"invalid {COLOR_ENV} value {}: expected auto|always|never",
raw.to_string_lossy()
)
})?;
match text {
"auto" => Ok(Some(ColorChoice::Auto)),
"always" => Ok(Some(ColorChoice::Always)),
"never" => Ok(Some(ColorChoice::Never)),
_ => Err(format!(
"invalid {COLOR_ENV} value {text:?}: expected auto|always|never"
)),
}
}
pub(crate) fn parse_non_interactive_env(raw: Option<&OsStr>) -> Result<bool, String> {
let Some(raw) = raw else {
return Ok(false);
};
if raw.is_empty() {
return Ok(false);
}
let text = raw.to_str().ok_or_else(|| {
format!(
"invalid {NON_INTERACTIVE_ENV} value {}: expected a boolean (1/0, true/false, yes/no, on/off)",
raw.to_string_lossy()
)
})?;
parse_boolish(text).ok_or_else(|| {
format!(
"invalid {NON_INTERACTIVE_ENV} value {text:?}: \
expected a boolean (1/0, true/false, yes/no, on/off)"
)
})
}
pub(crate) fn resolve_posture_with(
top_quiet: bool,
top_color: Option<ColorChoice>,
top_non_interactive: bool,
quiet_env: Option<&OsStr>,
color_env: Option<&OsStr>,
non_interactive_env: Option<&OsStr>,
) -> Result<(bool, ColorChoice, bool), String> {
let quiet = top_quiet || parse_quiet_env(quiet_env)?;
let color = top_color
.or(parse_color_env(color_env)?)
.unwrap_or_default();
let non_interactive = top_non_interactive || parse_non_interactive_env(non_interactive_env)?;
Ok((quiet, color, non_interactive))
}