1use crate::font_mode::FontMode;
2use crate::foreground_colors::ForegroundColors;
3use std::fmt;
4use std::ops::Add;
5
6pub enum BackgroundColors {
7 Black(&'static str),
8 Red(&'static str),
9 Green(&'static str),
10 Yellow(&'static str),
11 Blue(&'static str),
12 Magenta(&'static str),
13 Cyan(&'static str),
14 White(&'static str),
15 Default(&'static str),
16 BrightBlack(&'static str),
17 BrightRed(&'static str),
18 BrightGreen(&'static str),
19 BrightYellow(&'static str),
20 BrightBlue(&'static str),
21 BrightMagenta(&'static str),
22 BrightCyan(&'static str),
23 BrightWhite(&'static str),
24}
25
26impl BackgroundColors {
27 pub fn black() -> Self {
28 BackgroundColors::Black("\x1B[40m")
29 }
30
31 pub fn red() -> Self {
32 BackgroundColors::Red("\x1B[41m")
33 }
34
35 pub fn green() -> Self {
36 BackgroundColors::Green("\x1B[42m")
37 }
38
39 pub fn yellow() -> Self {
40 BackgroundColors::Yellow("\x1B[43m")
41 }
42
43 pub fn blue() -> Self {
44 BackgroundColors::Blue("\x1B[44m")
45 }
46
47 pub fn magenta() -> Self {
48 BackgroundColors::Magenta("\x1B[45m")
49 }
50
51 pub fn cyan() -> Self {
52 BackgroundColors::Cyan("\x1B[46m")
53 }
54
55 pub fn white() -> Self {
56 BackgroundColors::White("\x1B[47m")
57 }
58
59 pub fn default() -> Self {
60 BackgroundColors::Default("\x1B[49m")
61 }
62
63 pub fn bright_black() -> Self {
64 BackgroundColors::BrightBlack("\x1B[100m")
65 }
66
67 pub fn bright_red() -> Self {
68 BackgroundColors::BrightRed("\x1B[101m")
69 }
70
71 pub fn bright_green() -> Self {
72 BackgroundColors::BrightGreen("\x1B[102m")
73 }
74
75 pub fn bright_yellow() -> Self {
76 BackgroundColors::BrightYellow("\x1B[103m")
77 }
78
79 pub fn bright_blue() -> Self {
80 BackgroundColors::BrightBlue("\x1B[104m")
81 }
82
83 pub fn bright_magenta() -> Self {
84 BackgroundColors::BrightMagenta("\x1B[105m")
85 }
86
87 pub fn bright_cyan() -> Self {
88 BackgroundColors::BrightCyan("\x1B[106m")
89 }
90
91 pub fn bright_white() -> Self {
92 BackgroundColors::BrightWhite("\x1B[107m")
93 }
94}
95
96impl fmt::Display for BackgroundColors {
97 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98 match self {
99 BackgroundColors::Black(s) => write!(f, "{}", s),
100 BackgroundColors::Red(s) => write!(f, "{}", s),
101 BackgroundColors::Green(s) => write!(f, "{}", s),
102 BackgroundColors::Yellow(s) => write!(f, "{}", s),
103 BackgroundColors::Blue(s) => write!(f, "{}", s),
104 BackgroundColors::Magenta(s) => write!(f, "{}", s),
105 BackgroundColors::Cyan(s) => write!(f, "{}", s),
106 BackgroundColors::White(s) => write!(f, "{}", s),
107 BackgroundColors::Default(s) => write!(f, "{}", s),
108 BackgroundColors::BrightBlack(s) => write!(f, "{}", s),
109 BackgroundColors::BrightRed(s) => write!(f, "{}", s),
110 BackgroundColors::BrightGreen(s) => write!(f, "{}", s),
111 BackgroundColors::BrightYellow(s) => write!(f, "{}", s),
112 BackgroundColors::BrightBlue(s) => write!(f, "{}", s),
113 BackgroundColors::BrightMagenta(s) => write!(f, "{}", s),
114 BackgroundColors::BrightCyan(s) => write!(f, "{}", s),
115 BackgroundColors::BrightWhite(s) => write!(f, "{}", s),
116 }
117 }
118}
119
120impl Add<ForegroundColors> for BackgroundColors {
121 type Output = String;
122
123 fn add(self, rhs: ForegroundColors) -> Self::Output {
124 let background_color = self.to_string().replace("\x1B[", "").replace("m", "");
125 let foreground_color = rhs.to_string().replace("\x1B[", "").replace("m", "");
126
127 format!("\x1B[{};{}m", background_color, foreground_color)
128 }
129}
130
131impl Add<FontMode> for BackgroundColors {
132 type Output = String;
133
134 fn add(self, rhs: FontMode) -> Self::Output {
135 let background_color = self.to_string().replace("\x1B[", "").replace("m", "");
136 let font_mode = rhs.to_string().replace("\x1B[", "").replace("m", "");
137
138 format!("\x1B[{};{}m", background_color, font_mode)
139 }
140}
141
142impl Add<BackgroundColors> for BackgroundColors {
143 type Output = String;
144
145 fn add(self, rhs: BackgroundColors) -> Self::Output {
146 let background_color_lhs = self.to_string().replace("\x1B[", "").replace("m", "");
147 let background_color_rhs = rhs.to_string().replace("\x1B[", "").replace("m", "");
148
149 format!("\x1B[{};{}m", background_color_lhs, background_color_rhs)
150 }
151}
152
153impl Add<String> for BackgroundColors {
154 type Output = String;
155
156 fn add(self, rhs: String) -> Self::Output {
157 let background_color = self.to_string().replace("\x1B[", "").replace("m", "");
158 let string = rhs.to_string().replace("\x1B[", "").replace("m", "");
159
160 format!("\x1B[{};{}m", background_color, string)
161 }
162}
163
164impl Add<BackgroundColors> for String {
165 type Output = String;
166
167 fn add(self, rhs: BackgroundColors) -> Self::Output {
168 let string = self.to_string().replace("\x1B[", "").replace("m", "");
169 let background_color = rhs.to_string().replace("\x1B[", "").replace("m", "");
170
171 format!("\x1B[{};{}m", string, background_color)
172 }
173}
174
175#[cfg(test)]
176mod tests {
177 use super::*;
178
179 #[test]
180 fn test_black() {
181 assert_eq!("\x1B[40m", BackgroundColors::black().to_string());
182 }
183
184 #[test]
185 fn test_red() {
186 assert_eq!("\x1B[41m", BackgroundColors::red().to_string());
187 }
188
189 #[test]
190 fn test_green() {
191 assert_eq!("\x1B[42m", BackgroundColors::green().to_string());
192 }
193
194 #[test]
195 fn test_yellow() {
196 assert_eq!("\x1B[43m", BackgroundColors::yellow().to_string());
197 }
198
199 #[test]
200 fn test_blue() {
201 assert_eq!("\x1B[44m", BackgroundColors::blue().to_string());
202 }
203
204 #[test]
205 fn test_magenta() {
206 assert_eq!("\x1B[45m", BackgroundColors::magenta().to_string());
207 }
208
209 #[test]
210 fn test_cyan() {
211 assert_eq!("\x1B[46m", BackgroundColors::cyan().to_string());
212 }
213
214 #[test]
215 fn test_white() {
216 assert_eq!("\x1B[47m", BackgroundColors::white().to_string());
217 }
218
219 #[test]
220 fn test_default() {
221 assert_eq!("\x1B[49m", BackgroundColors::default().to_string());
222 }
223
224 #[test]
225 fn test_bright_black() {
226 assert_eq!("\x1B[100m", BackgroundColors::bright_black().to_string());
227 }
228
229 #[test]
230 fn test_bright_red() {
231 assert_eq!("\x1B[101m", BackgroundColors::bright_red().to_string());
232 }
233
234 #[test]
235 fn test_bright_green() {
236 assert_eq!("\x1B[102m", BackgroundColors::bright_green().to_string());
237 }
238
239 #[test]
240 fn test_bright_yellow() {
241 assert_eq!("\x1B[103m", BackgroundColors::bright_yellow().to_string());
242 }
243
244 #[test]
245 fn test_bright_blue() {
246 assert_eq!("\x1B[104m", BackgroundColors::bright_blue().to_string());
247 }
248
249 #[test]
250 fn test_bright_magenta() {
251 assert_eq!("\x1B[105m", BackgroundColors::bright_magenta().to_string());
252 }
253
254 #[test]
255 fn test_bright_cyan() {
256 assert_eq!("\x1B[106m", BackgroundColors::bright_cyan().to_string());
257 }
258
259 #[test]
260 fn test_bright_white() {
261 assert_eq!("\x1B[107m", BackgroundColors::bright_white().to_string());
262 }
263
264 #[test]
265 fn test_background_color_add_foreground_color() {
266 let background_color = BackgroundColors::red();
267 let foreground_color = ForegroundColors::bright_yellow();
268
269 assert_eq!("\x1B[41;93m", background_color + foreground_color);
270 }
271
272 #[test]
273 fn test_background_color_add_background_color() {
274 let background_color_one = BackgroundColors::red();
275 let background_color_two = BackgroundColors::bright_yellow();
276
277 assert_eq!("\x1B[41;103m", background_color_one + background_color_two);
278 }
279
280 #[test]
281 fn test_background_color_add_font_mode() {
282 let background_color = BackgroundColors::red();
283 let font_mode = FontMode::italic();
284
285 assert_eq!("\x1B[41;3m", background_color + font_mode);
286 }
287
288 #[test]
289 fn test_background_color_add_string() {
290 let background_color = BackgroundColors::red();
291 let string = FontMode::italic() + ForegroundColors::bright_blue();
292
293 assert_eq!("\x1B[41;3;94m", background_color + string);
294 }
295
296 #[test]
297 fn test_string_add_background_color() {
298 let string = FontMode::italic() + ForegroundColors::bright_blue();
299 let background_color = BackgroundColors::red();
300
301 assert_eq!("\x1B[3;94;41m", string + background_color);
302 }
303}