Skip to main content

minus/prompt/
style.rs

1use super::RESET_STYLE;
2use crossterm::style::{Attribute, Color};
3use std::fmt::Write;
4
5#[derive(Clone, Copy, Debug, Eq, PartialEq)]
6pub enum PromptColor {
7    Reset,
8    Black,
9    DarkGrey,
10    Red,
11    DarkRed,
12    Green,
13    DarkGreen,
14    Yellow,
15    DarkYellow,
16    Blue,
17    DarkBlue,
18    Magenta,
19    DarkMagenta,
20    Cyan,
21    DarkCyan,
22    White,
23    Grey,
24    Rgb { r: u8, g: u8, b: u8 },
25    AnsiValue(u8),
26}
27
28#[derive(Clone, Copy, Debug, Eq, PartialEq)]
29pub enum PromptAttribute {
30    Bold,
31    Dim,
32    Italic,
33    Underlined,
34    Reverse,
35    Hidden,
36    CrossedOut,
37}
38
39impl PromptAttribute {
40    const ALL: [Self; 7] = [
41        Self::Bold,
42        Self::Dim,
43        Self::Italic,
44        Self::Underlined,
45        Self::Reverse,
46        Self::Hidden,
47        Self::CrossedOut,
48    ];
49
50    const fn bit(self) -> u8 {
51        1 << self as u8
52    }
53}
54
55#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
56pub struct PromptStyle {
57    pub(super) foreground: Option<PromptColor>,
58    pub(super) background: Option<PromptColor>,
59    pub(super) attributes: u8,
60}
61
62impl PromptStyle {
63    #[must_use]
64    pub const fn foreground(mut self, color: PromptColor) -> Self {
65        self.foreground = Some(color);
66        self
67    }
68
69    #[must_use]
70    pub const fn background(mut self, color: PromptColor) -> Self {
71        self.background = Some(color);
72        self
73    }
74
75    #[must_use]
76    pub const fn attribute(mut self, attribute: PromptAttribute) -> Self {
77        self.attributes |= attribute.bit();
78        self
79    }
80
81    pub(super) fn write_styled(self, output: &mut String, text: &str) {
82        let mut styled = self.foreground.is_some() || self.background.is_some();
83        if let Some(color) = self.foreground {
84            write_prompt_color(output, color, false);
85        }
86        if let Some(color) = self.background {
87            write_prompt_color(output, color, true);
88        }
89        for attribute in PromptAttribute::ALL {
90            if self.attributes & attribute.bit() != 0 {
91                let code = match attribute {
92                    PromptAttribute::Bold => 1,
93                    PromptAttribute::Dim => 2,
94                    PromptAttribute::Italic => 3,
95                    PromptAttribute::Underlined => 4,
96                    PromptAttribute::Reverse => 7,
97                    PromptAttribute::Hidden => 8,
98                    PromptAttribute::CrossedOut => 9,
99                };
100                let _ = write!(output, "\x1b[{code}m");
101                styled = true;
102            }
103        }
104        output.push_str(text);
105        if styled {
106            output.push_str(RESET_STYLE);
107        }
108    }
109}
110
111pub fn write_prompt_color(output: &mut String, color: PromptColor, background: bool) {
112    let offset = if background { 10 } else { 0 };
113    match color {
114        PromptColor::Rgb { r, g, b } => {
115            let base = if background { 48 } else { 38 };
116            let _ = write!(output, "\x1b[{base};2;{r};{g};{b}m");
117        }
118        PromptColor::AnsiValue(value) => {
119            let base = if background { 48 } else { 38 };
120            let _ = write!(output, "\x1b[{base};5;{value}m");
121        }
122        named => {
123            let foreground = match named {
124                PromptColor::Reset => 39,
125                PromptColor::Black => 30,
126                PromptColor::DarkRed => 31,
127                PromptColor::DarkGreen => 32,
128                PromptColor::DarkYellow => 33,
129                PromptColor::DarkBlue => 34,
130                PromptColor::DarkMagenta => 35,
131                PromptColor::DarkCyan => 36,
132                PromptColor::Grey => 37,
133                PromptColor::DarkGrey => 90,
134                PromptColor::Red => 91,
135                PromptColor::Green => 92,
136                PromptColor::Yellow => 93,
137                PromptColor::Blue => 94,
138                PromptColor::Magenta => 95,
139                PromptColor::Cyan => 96,
140                PromptColor::White => 97,
141                PromptColor::Rgb { .. } | PromptColor::AnsiValue(_) => unreachable!(),
142            };
143            let _ = write!(output, "\x1b[{}m", foreground + offset);
144        }
145    }
146}
147
148impl From<PromptColor> for Color {
149    fn from(color: PromptColor) -> Self {
150        match color {
151            PromptColor::Reset => Self::Reset,
152            PromptColor::Black => Self::Black,
153            PromptColor::DarkGrey => Self::DarkGrey,
154            PromptColor::Red => Self::Red,
155            PromptColor::DarkRed => Self::DarkRed,
156            PromptColor::Green => Self::Green,
157            PromptColor::DarkGreen => Self::DarkGreen,
158            PromptColor::Yellow => Self::Yellow,
159            PromptColor::DarkYellow => Self::DarkYellow,
160            PromptColor::Blue => Self::Blue,
161            PromptColor::DarkBlue => Self::DarkBlue,
162            PromptColor::Magenta => Self::Magenta,
163            PromptColor::DarkMagenta => Self::DarkMagenta,
164            PromptColor::Cyan => Self::Cyan,
165            PromptColor::DarkCyan => Self::DarkCyan,
166            PromptColor::White => Self::White,
167            PromptColor::Grey => Self::Grey,
168            PromptColor::Rgb { r, g, b } => Self::Rgb { r, g, b },
169            PromptColor::AnsiValue(value) => Self::AnsiValue(value),
170        }
171    }
172}
173
174impl From<PromptAttribute> for Attribute {
175    fn from(attribute: PromptAttribute) -> Self {
176        match attribute {
177            PromptAttribute::Bold => Self::Bold,
178            PromptAttribute::Dim => Self::Dim,
179            PromptAttribute::Italic => Self::Italic,
180            PromptAttribute::Underlined => Self::Underlined,
181            PromptAttribute::Reverse => Self::Reverse,
182            PromptAttribute::Hidden => Self::Hidden,
183            PromptAttribute::CrossedOut => Self::CrossedOut,
184        }
185    }
186}