Skip to main content

ansiq_core/
style.rs

1#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
2pub enum Color {
3    #[default]
4    Reset,
5    Black,
6    DarkGrey,
7    Grey,
8    White,
9    Blue,
10    Cyan,
11    Green,
12    Yellow,
13    Magenta,
14    Red,
15    Indexed(u8),
16    Rgb(u8, u8, u8),
17}
18
19#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
20pub struct Style {
21    pub fg: Color,
22    pub bg: Color,
23    pub bold: bool,
24    pub reversed: bool,
25}
26
27impl Default for Style {
28    fn default() -> Self {
29        Self {
30            fg: Color::Reset,
31            bg: Color::Reset,
32            bold: false,
33            reversed: false,
34        }
35    }
36}
37
38impl Style {
39    pub const fn fg(mut self, fg: Color) -> Self {
40        self.fg = fg;
41        self
42    }
43
44    pub const fn bg(mut self, bg: Color) -> Self {
45        self.bg = bg;
46        self
47    }
48
49    pub const fn bold(mut self, bold: bool) -> Self {
50        self.bold = bold;
51        self
52    }
53
54    pub const fn reversed(mut self, reversed: bool) -> Self {
55        self.reversed = reversed;
56        self
57    }
58}
59
60impl From<Color> for Style {
61    fn from(value: Color) -> Self {
62        Style::default().fg(value)
63    }
64}
65
66pub fn patch_style(base: Style, patch: Style) -> Style {
67    let fg = if patch.fg == Color::Reset {
68        base.fg
69    } else {
70        patch.fg
71    };
72    let bg = if patch.bg == Color::Reset {
73        base.bg
74    } else {
75        patch.bg
76    };
77
78    Style {
79        fg,
80        bg,
81        bold: base.bold || patch.bold,
82        reversed: base.reversed || patch.reversed,
83    }
84}