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
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Color {
Black,
Red,
Green,
Yellow,
Blue,
Magenta,
Cyan,
White,
Grey,
Reset,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub struct Style {
foreground_color: Option<Color>,
background_color: Option<Color>,
is_bold: bool,
is_italic: bool,
is_underline: bool,
}
impl Default for Style {
fn default() -> Self {
Self {
foreground_color: None,
background_color: None,
is_bold: false,
is_italic: false,
is_underline: false,
}
}
}
impl Style {
pub fn set_foreground(&self, color: Color) -> Style {
Style {
foreground_color: Some(color),
..*self
}
}
pub fn foreground(&self) -> Option<Color> {
self.foreground_color
}
pub fn set_background(&self, color: Color) -> Style {
Style {
background_color: Some(color),
..*self
}
}
pub fn background(&self) -> Option<Color> {
self.background_color
}
pub fn set_bold(&self, is_bold: bool) -> Style {
Style { is_bold, ..*self }
}
pub fn is_bold(&self) -> bool {
self.is_bold
}
pub fn set_italic(&self, is_italic: bool) -> Style {
Style { is_italic, ..*self }
}
pub fn is_italic(&self) -> bool {
self.is_italic
}
pub fn set_underline(&self, is_underline: bool) -> Style {
Style {
is_underline,
..*self
}
}
pub fn is_underlined(&self) -> bool {
self.is_underline
}
}
#[cfg(test)]
mod tests {
use crate::{Color, Style};
#[test]
fn style_foreground() {
let mut style = Style::default();
assert_eq!(None, style.foreground());
style = style.set_foreground(Color::Blue);
assert_eq!(Some(Color::Blue), style.foreground());
style = style.set_foreground(Color::Red);
assert_eq!(Some(Color::Red), style.foreground());
}
#[test]
fn style_background() {
let mut style = Style::default();
assert_eq!(None, style.background());
style = style.set_background(Color::Yellow);
assert_eq!(Some(Color::Yellow), style.background());
style = style.set_background(Color::Magenta);
assert_eq!(Some(Color::Magenta), style.background());
}
#[test]
fn style_bold() {
let mut style = Style::default();
assert_eq!(false, style.is_bold());
style = style.set_bold(true);
assert_eq!(true, style.is_bold());
}
#[test]
fn style_italic() {
let mut style = Style::default();
assert_eq!(false, style.is_italic());
style = style.set_italic(true);
assert_eq!(true, style.is_italic());
}
#[test]
fn style_underline() {
let mut style = Style::default();
assert_eq!(false, style.is_underlined());
style = style.set_underline(true);
assert_eq!(true, style.is_underlined());
}
}