1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
use ansi_term::{Colour, Style};
use serde::Deserialize;
use std::env;
#[derive(Clone, Copy, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum Color {
Always,
Never,
Auto,
}
impl Color {
pub fn bold_fg(self, text: &str, colour: Colour) -> String {
if self.should_color() {
Style::new().bold().fg(colour).paint(text).to_string()
} else {
text.to_owned()
}
}
pub fn bold(self, text: &str) -> String {
if self.should_color() {
Style::new().bold().paint(text).to_string()
} else {
text.to_owned()
}
}
fn should_color(self) -> bool {
match self {
Self::Always => true,
Self::Never => false,
Self::Auto => match env::var_os("TERM") {
None => false,
Some(k) if k == "dumb" => false,
Some(_) if env::var_os("NO_COLOR").is_some() => false,
Some(_) => true,
},
}
}
}