nixfleet_cli/
color.rs

1//! Tiny ANSI helper. No `colored`/`owo-colors` dep - these are 4 codes.
2
3#[derive(Copy, Clone, Debug)]
4pub enum Style {
5    Green,
6    Yellow,
7    Red,
8    Dim,
9}
10
11impl Style {
12    fn code(self) -> &'static str {
13        match self {
14            Self::Green => "32",
15            Self::Yellow => "33",
16            Self::Red => "31",
17            Self::Dim => "2",
18        }
19    }
20}
21
22#[derive(Copy, Clone, Debug)]
23pub struct Stylizer {
24    pub enabled: bool,
25}
26
27impl Stylizer {
28    pub fn paint(self, style: Style, s: &str) -> String {
29        if self.enabled {
30            format!("\x1b[{}m{}\x1b[0m", style.code(), s)
31        } else {
32            s.to_string()
33        }
34    }
35}
36
37/// Honour NO_COLOR (<https://no-color.org>) and tty detection.
38pub fn detect(force_off: bool) -> bool {
39    if force_off {
40        return false;
41    }
42    if std::env::var_os("NO_COLOR").is_some() {
43        return false;
44    }
45    is_terminal::IsTerminal::is_terminal(&std::io::stdout())
46}