use std::ffi::OsString;
use std::io::IsTerminal as _;
use super::Mark;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Style {
colour: bool,
}
impl Style {
pub fn detect() -> Self {
Self::new(
std::io::stdout().is_terminal(),
std::env::var_os("NO_COLOR"),
)
}
pub fn new(terminal: bool, no_color: Option<OsString>) -> Self {
Self {
colour: terminal && no_color.is_none_or(|v| v.is_empty()),
}
}
pub fn mark(self, mark: Mark) -> String {
self.tint(mark, mark.symbol())
}
pub fn tint(self, mark: Mark, text: &str) -> String {
let code = match mark {
Mark::Ok => "32",
Mark::Attention => "33",
Mark::Broken => "31",
Mark::Info | Mark::Absent => "2",
};
self.paint(code, text)
}
pub fn bold(self, text: &str) -> String {
self.paint("1", text)
}
pub fn dim(self, text: &str) -> String {
self.paint("2", text)
}
fn paint(self, code: &str, text: &str) -> String {
if self.colour {
format!("\x1b[{code}m{text}\x1b[0m")
} else {
text.to_owned()
}
}
}