use std::io::IsTerminal;
const RESET: &str = "\x1b[0m";
const BOLD: &str = "\x1b[1m";
const RED: &str = "\x1b[1;31m";
const YELLOW: &str = "\x1b[1;33m";
#[derive(Clone, Copy)]
pub(crate) struct Style {
active: bool,
}
impl Style {
pub(crate) const PLAIN: Style = Style { active: false };
pub(crate) const ACTIVE: Style = Style { active: true };
pub(crate) fn detect() -> Style {
let no_color = std::env::var_os("NO_COLOR").is_some_and(|v| !v.is_empty());
if std::io::stderr().is_terminal() && !no_color {
Style::ACTIVE
} else {
Style::PLAIN
}
}
fn wrap(self, codes: &str, text: &str) -> String {
if self.active {
format!("{codes}{text}{RESET}")
} else {
text.to_string()
}
}
pub(crate) fn reason(self, text: &str) -> String {
self.wrap(BOLD, text)
}
pub(crate) fn enforce(self, text: &str) -> String {
self.wrap(RED, text)
}
pub(crate) fn warn(self, text: &str) -> String {
self.wrap(YELLOW, text)
}
pub(crate) fn error(self, text: &str) -> String {
self.wrap(RED, text)
}
}