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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
use super::super::ColorList;
/// The `CuteText` struct contains a string of text, a reference to a `ColorList`
/// and prev_len that is length of text before run add_text.
pub struct CuteText {
pub text: String,
pub color_list: ColorList,
pub prev_len: usize,
}
impl CuteText {
/// This function creates a new instance of the `CuteText` struct.
pub fn new() -> Self {
CuteText {
text: String::new(),
color_list: ColorList::new(),
prev_len: 0,
}
}
/// This function sets the text color to black.
pub fn black(&mut self) -> &mut Self {
self.add_color(self.color_list.black_fg);
self
}
/// This function sets the text color to red.
pub fn red(&mut self) -> &mut Self {
self.add_color(self.color_list.red_fg);
self
}
/// This function sets the text color to green.
pub fn green(&mut self) -> &mut Self {
self.add_color(self.color_list.green_fg);
self
}
/// This function sets the text color to yellow.
pub fn yellow(&mut self) -> &mut Self {
self.add_color(self.color_list.yellow_fg);
self
}
/// This function sets the text color to blue.
pub fn blue(&mut self) -> &mut Self {
self.add_color(self.color_list.blue_fg);
self
}
/// This function sets the text color to magenta.
pub fn magenta(&mut self) -> &mut Self {
self.add_color(self.color_list.magenta_fg);
self
}
/// This function sets the text color to cyan.
pub fn cyan(&mut self) -> &mut Self {
self.add_color(self.color_list.cyan_fg);
self
}
/// This function sets the text color to white.
pub fn white(&mut self) -> &mut Self {
self.add_color(self.color_list.white_fg);
self
}
/// This function sets the text color to bright black.
pub fn bright_black(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_black_fg);
self
}
/// This function sets the text color to bright red.
pub fn bright_red(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_red_fg);
self
}
/// This function sets the text color to bright green.
pub fn bright_green(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_green_fg);
self
}
/// This function sets the text color to bright yellow.
pub fn bright_yellow(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_yellow_fg);
self
}
/// This function sets the text color to bright blue.
pub fn bright_blue(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_blue_fg);
self
}
/// This function sets the text color to bright magenta.
pub fn bright_magenta(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_magenta_fg);
self
}
/// This function sets the text color to bright cyan.
pub fn bright_cyan(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_cyan_fg);
self
}
/// This function sets the text color to bright white.
pub fn bright_white(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_white_fg);
self
}
/// This function sets the background color to on_black.
pub fn on_black(&mut self) -> &mut Self {
self.add_color(self.color_list.black_bg);
self
}
/// This function sets the background color to red.
pub fn on_red(&mut self) -> &mut Self {
self.add_color(self.color_list.red_bg);
self
}
/// This function sets the background color to green.
pub fn on_green(&mut self) -> &mut Self {
self.add_color(self.color_list.green_bg);
self
}
/// This function sets the background color to yellow.
pub fn on_yellow(&mut self) -> &mut Self {
self.add_color(self.color_list.yellow_bg);
self
}
/// This function sets the background color to blue.
pub fn on_blue(&mut self) -> &mut Self {
self.add_color(self.color_list.blue_bg);
self
}
/// This function sets the background color to magenta.
pub fn on_magenta(&mut self) -> &mut Self {
self.add_color(self.color_list.magenta_bg);
self
}
/// This function sets the background color to cyan.
pub fn on_cyan(&mut self) -> &mut Self {
self.add_color(self.color_list.cyan_bg);
self
}
/// This function sets the background color to white.
pub fn on_white(&mut self) -> &mut Self {
self.add_color(self.color_list.white_bg);
self
}
/// This function sets the background color bright black.
pub fn on_bright_black(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_black_bg);
self
}
/// This function sets the background color to bright red.
pub fn on_bright_red(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_red_bg);
self
}
/// This function sets the background color to bright green.
pub fn on_bright_green(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_green_bg);
self
}
/// This function sets the background color to bright yellow.
pub fn on_bright_yellow(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_yellow_bg);
self
}
/// This function sets the background color to bright blue.
pub fn on_bright_blue(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_blue_bg);
self
}
/// This function sets the background color to bright magenta.
pub fn on_bright_magenta(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_magenta_bg);
self
}
/// This function sets the background color to bright cyan.
pub fn on_bright_cyan(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_cyan_bg);
self
}
/// This function sets the background color to bright white.
pub fn on_bright_white(&mut self) -> &mut Self {
self.add_color(self.color_list.bright_white_bg);
self
}
/// This function fills the variable self.prev_len with the length of the text before the new text is added
/// and then adds the text passed as a parameter to self.text.
///
/// # Arguments
///
/// `text: &str` - The text that will be added to the self.text.
///
/// # Returns
///
/// `&mut Self ` - A mutable reference to the updated
pub fn add_text(&mut self, text: &str) -> &mut Self {
self.prev_len = self.text.len();
self.text = self.text.to_string() + text;
self
}
/// Adds the color passed by parameter to `self.text` and updates its text.
/// If `self.prev_len` is greater than 0 it extracts the text that was there before using the `add_text` function and adds the color to the text that was added when using the `add_text` function.
///
/// # Arguments
///
/// `color: &str` - The color that will be added to the `self.text`.
///
/// # Returns
///
/// `&mut Self` - A mutable reference to the updated `self.text`.
fn add_color(&mut self, color: &str) -> &mut Self {
if self.prev_len > 0 {
let before_text: &str = &self.text[0..self.prev_len];
let after_text: &str = &self.text[self.prev_len..self.text.len()];
let modified_text: String = before_text.to_owned() + color + after_text;
self.text = modified_text;
}
if self.prev_len == 0 {
self.text = color.to_string() + &self.text;
}
self.text = self.text.to_owned() + self.color_list.reset;
self
}
}