modern_terminal/core/
style.rs

1#[derive(Clone, Debug)]
2pub enum Style {
3    Background(String),
4    Bold,
5    Dim,
6    Foreground(String),
7    None,
8}
9
10impl Style {
11    pub fn ansi_escape_code(
12        &self,
13        storage: crate::core::color::storage::Storage,
14    ) -> String {
15        let sgr = self.ansi_sgr(storage);
16        let sgr: Vec<String> = sgr.iter().map(u8::to_string).collect();
17        if sgr.is_empty() {
18            String::new()
19        }
20        else {
21            format!("\u{1b}[{}m", sgr.join(";"))
22        }
23    }
24
25    pub fn ansi_sgr(
26        &self,
27        storage: crate::core::color::storage::Storage,
28    ) -> Vec<u8> {
29        match self {
30            Style::Background(color) => {
31                if let Ok(color) = crate::core::color::Color::new(color) {
32                    if let Some(color) = color.to_storage(storage) {
33                        if let Ok(sgr) = color.ansi_sgr(false) {
34                            return sgr;
35                        }
36                    }
37                }
38                vec![]
39            },
40            Style::Bold => {
41                vec![1]
42            },
43            Style::Dim => {
44                vec![2]
45            },
46            Style::Foreground(color) => {
47                if let Ok(color) = crate::core::color::Color::new(color) {
48                    if let Some(color) = color.to_storage(storage) {
49                        if let Ok(sgr) = color.ansi_sgr(true) {
50                            return sgr;
51                        }
52                    }
53                }
54                vec![]
55            },
56            Style::None => {
57                vec![0]
58            },
59        }
60    }
61}
62
63#[cfg(test)]
64mod test_style {
65    use super::Style;
66
67    #[test]
68    fn ansi_sgr_none() {
69        assert_eq!(
70            Style::None.ansi_sgr(crate::core::color::storage::Storage::Bits24),
71            [0]
72        );
73    }
74
75    #[test]
76    fn ansi_sgr_background() {
77        assert_eq!(
78            Style::Background("black".to_string())
79                .ansi_sgr(crate::core::color::storage::Storage::Bits24),
80            [40]
81        );
82    }
83
84    #[test]
85    fn ansi_sgr_reset_background() {
86        assert_eq!(
87            Style::Background("default".to_string())
88                .ansi_sgr(crate::core::color::storage::Storage::Bits24),
89            [49]
90        );
91    }
92
93    #[test]
94    fn ansi_sgr_bold() {
95        assert_eq!(
96            Style::Bold.ansi_sgr(crate::core::color::storage::Storage::Bits24),
97            [1]
98        );
99    }
100
101    #[test]
102    fn ansi_sgr_dim() {
103        assert_eq!(
104            Style::Dim.ansi_sgr(crate::core::color::storage::Storage::Bits24),
105            [2]
106        );
107    }
108
109    #[test]
110    fn ansi_sgr_foreground() {
111        assert_eq!(
112            Style::Foreground("black".to_string())
113                .ansi_sgr(crate::core::color::storage::Storage::Bits24),
114            [30]
115        );
116    }
117
118    #[test]
119    fn ansi_sgr_reset_foreground() {
120        assert_eq!(
121            Style::Foreground("default".to_string())
122                .ansi_sgr(crate::core::color::storage::Storage::Bits24),
123            [39]
124        );
125    }
126}