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
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
pub mod color;
pub mod rgb;

pub use color::*;
pub use rgb::*;

use std::fmt;

#[derive(Debug, Clone)]
pub struct ColoredText {
    foreground_color: Option<Color>,
    background_color: Option<Color>,
    text: String,
}

impl ColoredText {
    pub fn new(text: &str) -> Self {
        Self {
            foreground_color: None,
            background_color: None,
            text: text.to_string(),
        }
    }

    fn with_foreground_color(&self, color: Color) -> Self {
        Self {
            foreground_color: Some(color),
            background_color: self.background_color,
            text: self.text.clone(),
        }
    }

    fn with_background_color(&self, color: Color) -> Self {
        Self {
            foreground_color: self.foreground_color,
            background_color: Some(color),
            text: self.text.clone(),
        }
    }

    pub(crate) fn ansi_color_code(&self) -> String {
        let mut code = String::from("\x1B[");

        if let Some(fgcolor) = self.foreground_color {
            code.push_str(&fgcolor.to_foreground_color_code());
        }

        if let Some(bgcolor) = self.background_color {
            if self.foreground_color.is_some() {
                code.push(';');
            }
            code.push_str(&bgcolor.to_background_color_code());
        }

        code.push('m');
        code
    }
}

impl fmt::Display for ColoredText {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}{}\x1B[0m", self.ansi_color_code(), self.text)
    }
}

pub trait Colorize : fmt::Display {
    fn to_colored(&self) -> ColoredText;
    fn fg(&self, color: Color) -> ColoredText;
    fn bg(&self, color: Color) -> ColoredText;
    fn fg_rgb(&self, r: u8, g: u8, b: u8) -> ColoredText;
    fn bg_rgb(&self, r: u8, g: u8, b: u8) -> ColoredText;
}

impl Colorize for ColoredText {
    fn to_colored(&self) -> ColoredText {
        self.clone()
    }

    fn fg(&self, color: Color) -> ColoredText {
        self.with_foreground_color(color)
    }

    fn bg(&self, color: Color) -> ColoredText {
        self.with_background_color(color)
    }

    fn fg_rgb(&self, r: u8, g: u8, b: u8) -> ColoredText {
        self.with_foreground_color(Color::TrueColor(RGB::new(r, g, b)))
    }

    fn bg_rgb(&self, r: u8, g: u8, b: u8) -> ColoredText {
        self.with_background_color(Color::TrueColor(RGB::new(r, g, b)))
    }
}

impl Colorize for str {
    fn to_colored(&self) -> ColoredText {
        ColoredText::new(self)
    }

    fn fg(&self, color: Color) -> ColoredText {
        ColoredText::new(self)
        .with_foreground_color(color)
    }

    fn bg(&self, color: Color) -> ColoredText {
        ColoredText::new(self)
        .with_background_color(color)
    }

    fn fg_rgb(&self, r: u8, g: u8, b: u8) -> ColoredText {
        ColoredText::new(self)
        .with_foreground_color(Color::TrueColor(RGB::new(r, g, b)))
    }

    fn bg_rgb(&self, r: u8, g: u8, b: u8) -> ColoredText {
        ColoredText::new(self)
        .with_background_color(Color::TrueColor(RGB::new(r, g, b)))
    }
}

impl Colorize for String {
    fn to_colored(&self) -> ColoredText {
        ColoredText::new(self)
    }

    fn fg(&self, color: Color) -> ColoredText {
        ColoredText::new(self)
        .with_foreground_color(color)
    }

    fn bg(&self, color: Color) -> ColoredText {
        ColoredText::new(self)
        .with_background_color(color)
    }

    fn fg_rgb(&self, r: u8, g: u8, b: u8) -> ColoredText {
        ColoredText::new(self)
        .with_foreground_color(Color::TrueColor(RGB::new(r, g, b)))
    }

    fn bg_rgb(&self, r: u8, g: u8, b: u8) -> ColoredText {
        ColoredText::new(self)
        .with_background_color(Color::TrueColor(RGB::new(r, g, b)))
    }
}