1#[derive(Copy, Clone, Debug, PartialEq, Eq, Ord, PartialOrd, Hash)]
4pub enum Color {
5 Reset,
6 Black,
7 DarkGrey,
8 Red,
9 DarkRed,
10 Green,
11 DarkGreen,
12 Yellow,
13 DarkYellow,
14 Blue,
15 DarkBlue,
16 Magenta,
17 DarkMagenta,
18 Cyan,
19 DarkCyan,
20 White,
21 Grey,
22 Rgb { r: u8, g: u8, b: u8 },
23 AnsiValue(u8),
24}
25
26#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
27pub struct Style {
28 pub fg: Option<Color>,
29 pub bg: Option<Color>,
30 pub bold: bool,
31 pub dim: bool,
32 pub italic: bool,
33 pub underline: bool,
34 pub crossedout: bool,
35 pub reverse: bool,
36}
37
38impl Style {
39 pub const fn new() -> Self {
40 Self {
41 fg: None,
42 bg: None,
43 bold: false,
44 dim: false,
45 italic: false,
46 underline: false,
47 crossedout: false,
48 reverse: false,
49 }
50 }
51
52 pub fn fg(mut self, color: Color) -> Self {
53 self.fg = Some(color);
54 self
55 }
56
57 pub fn bg(mut self, color: Color) -> Self {
58 self.bg = Some(color);
59 self
60 }
61
62 pub fn bold(mut self) -> Self {
63 self.bold = true;
64 self
65 }
66
67 pub fn dim(mut self) -> Self {
68 self.dim = true;
69 self
70 }
71
72 pub fn italic(mut self) -> Self {
73 self.italic = true;
74 self
75 }
76
77 pub fn underline(mut self) -> Self {
78 self.underline = true;
79 self
80 }
81
82 pub fn crossedout(mut self) -> Self {
83 self.crossedout = true;
84 self
85 }
86
87 pub fn reverse(mut self) -> Self {
88 self.reverse = true;
89 self
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96
97 #[test]
98 fn style_builder_methods_set_their_fields() {
99 let s = Style::new()
100 .fg(Color::Red)
101 .bg(Color::Blue)
102 .bold()
103 .dim()
104 .italic()
105 .underline()
106 .crossedout()
107 .reverse();
108 assert_eq!(
109 s,
110 Style {
111 fg: Some(Color::Red),
112 bg: Some(Color::Blue),
113 bold: true,
114 dim: true,
115 italic: true,
116 underline: true,
117 crossedout: true,
118 reverse: true,
119 }
120 );
121 }
122}