prettylogger/
colors.rs

1use std::collections::HashMap;
2use serde::{Serialize, Deserialize};
3use lazy_static::lazy_static;
4use std::fmt::{Display, Formatter};
5
6static BLACK: &str = "\x1b[30m";
7static BLUE: &str = "\x1b[34m";
8static CYAN: &str = "\x1b[36m";
9static GREEN: &str = "\x1b[32m";
10static GRAY: &str = "\x1b[90m";
11static MAGENTA: &str = "\x1b[35m";
12static RED: &str = "\x1b[31m";
13static WHITE: &str = "\x1b[37m";
14static YELLOW: &str = "\x1b[33m";
15pub(crate) static RESET: &str = "\x1b[0m";
16
17#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Default,
18    Serialize, Deserialize)]
19/// Used to set log header colors.
20pub enum Color
21{
22    #[default]
23    None = 0,
24    Black = 1,
25    Blue = 2,
26    Cyan = 3,
27    Green = 4,
28    Gray = 5,
29    Magenta = 6,
30    Red = 7,
31    White = 8,
32    Yellow = 9,
33}
34
35impl Display for Color {
36    fn fmt(&self, f: &mut Formatter) -> std::fmt::Result {
37        let level_str = match *self {
38            Color::None => "None",
39            Color:: Black => "Black",
40            Color::Blue => "Blue",
41            Color::Cyan => "Cyan",
42            Color::Green => "Green",
43            Color::Gray => "Gray",
44            Color::Magenta => "Magenta",
45            Color::Red => "Red",
46            Color::White => "White",
47            Color::Yellow => "Yellow",
48        };
49        write!(f, "{}", level_str)
50    }
51}
52
53impl TryFrom<i32> for Color {
54    type Error = &'static str;
55    fn try_from(value: i32) -> Result<Self, Self::Error> {
56        match value {
57            0 => Ok(Color::None),
58            1 => Ok(Color::Black),
59            2 => Ok(Color::Blue),
60            3 => Ok(Color::Cyan),
61            4 => Ok(Color::Green),
62            5 => Ok(Color::Gray),
63            6 => Ok(Color::Magenta),
64            7 => Ok(Color::Red),
65            8 => Ok(Color::White),
66            9 => Ok(Color::Yellow),
67            _ => Err("Invalid value! Please provide a value in range 0-9."),
68        }
69    }
70}
71
72impl AsRef<str> for Color {
73    fn as_ref(&self) -> &str {
74        match self {
75            Color::None => "None",
76            Color::Black => "Black",
77            Color::Blue => "Blue",
78            Color::Cyan => "Cyan",
79            Color::Green => "Green",
80            Color::Gray => "Gray",
81            Color::Magenta => "Magenta",
82            Color::Red => "Red",
83            Color::White => "White",
84            Color::Yellow => "Yellow",
85        }
86    }
87}
88
89lazy_static! {
90    static ref COLOR_MAP: HashMap<i32, &'static str> =  {
91        let mut m = HashMap::new();
92        m.insert(Color::None as i32, "");
93        m.insert(Color::Black as i32, BLACK);
94        m.insert(Color::Blue as i32, BLUE);
95        m.insert(Color::Cyan as i32, CYAN);
96        m.insert(Color::Green as i32, GREEN);
97        m.insert(Color::Gray as i32, GRAY);
98        m.insert(Color::Magenta as i32, MAGENTA);
99        m.insert(Color::Red as i32, RED);
100        m.insert(Color::White as i32, WHITE);
101        m.insert(Color::Yellow as i32, YELLOW);
102        m.insert(Color::None as i32, "");
103        return m;
104    };
105}
106
107pub(crate) fn get_color_code(color: Color) -> String {
108    let key = color as i32;
109    if COLOR_MAP.contains_key(&key) {
110        return COLOR_MAP[&key].to_string();
111    }
112    else {
113        return RESET.to_string();
114    }
115}