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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
use std::fmt::{Debug, Display};

/// We use `ColChar` to say exactly what each pixel should look like and what colour it should be. That is, the [`View`](super::View)'s canvas is just a vector of `ColChar`s under the hood. `ColChar` has the [`fill_char`](ColChar::fill_char) and [`modifier`](ColChar::modifier) properties. [`fill_char`](ColChar::fill_char) is the single ascii character used as the "pixel" when the [`View`](super::View) is rendered, whereas [`modifier`](ColChar::modifier) can give that pixel a colour or make it bold/italic
#[derive(Debug, Copy, PartialEq)]
pub struct ColChar {
    pub fill_char: char,
    pub modifier: Modifier,
}

impl ColChar {
    pub const SOLID: Self = Self {
        fill_char: '█',
        modifier: Modifier::None,
    };
    pub const BACKGROUND: Self = Self {
        fill_char: '░',
        modifier: Modifier::None,
    };
    pub const EMPTY: Self = Self {
        fill_char: ' ',
        modifier: Modifier::None,
    };

    pub fn new(fill_char: char, modifier: Modifier) -> Self {
        Self {
            fill_char,
            modifier,
        }
    }

    // Return the rendered ColChar
    #[deprecated = "Please use `ColChar`'s implementation of `std::fmt::Display` instead"]
    pub fn render(&self) -> String {
        self.to_string()
    }

    /// Return a ColChar with the same `modifier` and new `fill_char`
    pub fn with_char(&self, fill_char: char) -> Self {
        Self {
            fill_char: fill_char,
            modifier: self.modifier,
        }
    }

    /// Return a ColChar with the same `fill_char` and new `modifier`
    pub fn with_mod(&self, modifier: Modifier) -> Self {
        Self {
            fill_char: self.fill_char,
            modifier: modifier,
        }
    }

    /// Return a ColChar with the same `fill_char` and new `modifier` of the `Modifier::Colour` enum variant from an RGB value
    pub fn with_rgb(&self, r: u8, g: u8, b: u8) -> Self {
        Self {
            fill_char: self.fill_char,
            modifier: Modifier::Colour { r, g, b },
        }
    }

    /// Return a ColChar with the same `fill_char` and new `modifier` of the `Modifier::Colour` enum variant from an HSV value
    pub fn with_hsv(&self, h: u8, s: u8, v: u8) -> Self {
        Self {
            fill_char: self.fill_char,
            modifier: Modifier::from_hsv(h, s, v),
        }
    }
}

impl Clone for ColChar {
    fn clone(&self) -> Self {
        Self {
            fill_char: self.fill_char,
            modifier: self.modifier,
        }
    }
}

impl Display for ColChar {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.modifier {
            Modifier::None => write!(f, "{}", self.fill_char),
            _ => write!(f, "{}{}{}", self.modifier, self.fill_char, Modifier::END),
        }
    }
}

/// The `Modifier` enum is used for adding modifications to text such as colour, bold/italic/underline and others. It's essentially a wrapper for `\x1b[{x}m`, where {x} is a code or rgb value of some sort. `Modifier` is primarily used by [`ColChar`] as one of its properties
#[derive(Debug, Copy, PartialEq)]
pub enum Modifier {
    Coded(u8),
    Colour { r: u8, g: u8, b: u8 },
    None,
}

impl Modifier {
    pub const END: Self = Self::Coded(0);
    pub const BOLD: Self = Self::Coded(1);
    pub const LIGHT: Self = Self::Coded(2);
    pub const ITALIC: Self = Self::Coded(3);
    pub const UNDERLINE: Self = Self::Coded(4);
    pub const INVERTED: Self = Self::Coded(7);
    pub const CROSSED: Self = Self::Coded(9);
    pub const RED: Self = Self::Coded(31);
    pub const GREEN: Self = Self::Coded(32);
    pub const YELLOW: Self = Self::Coded(33);
    pub const BLUE: Self = Self::Coded(34);
    pub const PURPLE: Self = Self::Coded(35);
    pub const CYAN: Self = Self::Coded(36);

    pub fn from_rgb(r: u8, g: u8, b: u8) -> Self {
        Self::Colour { r, g, b }
    }

    pub fn from_hsv(h: u8, s: u8, v: u8) -> Self {
        let h = h as f32 / 255.0;
        let s = s as f32 / 255.0;
        let v = v as f32 / 255.0;

        let i = (h * 6.0).floor();
        let f = h * 6.0 - i;
        let p = v * (1.0 - f * s);
        let q = v * (1.0 - f * s);
        let t = v * (1.0 - (1.0 - f) * s);

        let (r, g, b) = [
            (v, t, p),
            (q, v, p),
            (p, v, t),
            (p, q, v),
            (t, p, v),
            (v, p, q),
        ][(i % 6.0).floor() as usize];

        Self::Colour {
            r: (r * 255.0) as u8,
            g: (g * 255.0) as u8,
            b: (b * 255.0) as u8,
        }
    }
}

impl Clone for Modifier {
    fn clone(&self) -> Self {
        match self {
            Self::Coded(code) => Self::Coded(*code),
            Self::Colour { r, g, b } => Self::Colour {
                r: *r,
                g: *g,
                b: *b,
            },
            Self::None => Self::None,
        }
    }
}

impl Display for Modifier {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Coded(code) => write!(f, "\x1b[{}m", code),
            Self::Colour { r, g, b } => write!(f, "\x1b[38;2;{};{};{}m", r, g, b),
            Self::None => Ok(()),
        }
    }
}