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
use bitflags::bitflags;

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Color {
    Reset,
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    Gray,
    DarkGray,
    LightRed,
    LightGreen,
    LightYellow,
    LightBlue,
    LightMagenta,
    LightCyan,
    White,
    Rgb(u8, u8, u8),
    Indexed(u8),
}

impl Color {
    /// Returns a short code associated with the color, used for debug purpose
    /// only
    pub(crate) fn code(self) -> &'static str {
        match self {
            Color::Reset => "X",
            Color::Black => "b",
            Color::Red => "r",
            Color::Green => "c",
            Color::Yellow => "y",
            Color::Blue => "b",
            Color::Magenta => "m",
            Color::Cyan => "c",
            Color::Gray => "w",
            Color::DarkGray => "B",
            Color::LightRed => "R",
            Color::LightGreen => "G",
            Color::LightYellow => "Y",
            Color::LightBlue => "B",
            Color::LightMagenta => "M",
            Color::LightCyan => "C",
            Color::White => "W",
            Color::Indexed(_) => "i",
            Color::Rgb(_, _, _) => "o",
        }
    }
}

bitflags! {
    pub struct Modifier: u16 {
        const BOLD              = 0b0000_0000_0001;
        const DIM               = 0b0000_0000_0010;
        const ITALIC            = 0b0000_0000_0100;
        const UNDERLINED        = 0b0000_0000_1000;
        const SLOW_BLINK        = 0b0000_0001_0000;
        const RAPID_BLINK       = 0b0000_0010_0000;
        const REVERSED          = 0b0000_0100_0000;
        const HIDDEN            = 0b0000_1000_0000;
        const CROSSED_OUT       = 0b0001_0000_0000;
    }
}

impl Modifier {
    /// Returns a short code associated with the color, used for debug purpose
    /// only
    pub(crate) fn code(self) -> String {
        use std::fmt::Write;

        let mut result = String::new();

        if self.contains(Modifier::BOLD) {
            write!(result, "BO").unwrap();
        }
        if self.contains(Modifier::DIM) {
            write!(result, "DI").unwrap();
        }
        if self.contains(Modifier::ITALIC) {
            write!(result, "IT").unwrap();
        }
        if self.contains(Modifier::UNDERLINED) {
            write!(result, "UN").unwrap();
        }
        if self.contains(Modifier::SLOW_BLINK) {
            write!(result, "SL").unwrap();
        }
        if self.contains(Modifier::RAPID_BLINK) {
            write!(result, "RA").unwrap();
        }
        if self.contains(Modifier::REVERSED) {
            write!(result, "RE").unwrap();
        }
        if self.contains(Modifier::HIDDEN) {
            write!(result, "HI").unwrap();
        }
        if self.contains(Modifier::CROSSED_OUT) {
            write!(result, "CR").unwrap();
        }

        result
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Style {
    pub fg: Color,
    pub bg: Color,
    pub modifier: Modifier,
}

impl Default for Style {
    fn default() -> Style {
        Style {
            fg: Color::Reset,
            bg: Color::Reset,
            modifier: Modifier::empty(),
        }
    }
}

impl Style {
    pub fn reset(&mut self) {
        self.fg = Color::Reset;
        self.bg = Color::Reset;
        self.modifier = Modifier::empty();
    }

    pub fn fg(mut self, color: Color) -> Style {
        self.fg = color;
        self
    }
    pub fn bg(mut self, color: Color) -> Style {
        self.bg = color;
        self
    }
    pub fn modifier(mut self, modifier: Modifier) -> Style {
        self.modifier = modifier;
        self
    }
}