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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
use crate::StyleList;
use super::super::ColorList;
/// The `CuteText` struct contains a string of text, a reference to a `ColorList`
/// prev_len that is length of text before run add_text, and color_before_text_length is the length of the color before the text.
pub struct CuteText {
pub text: String,
pub color_list: ColorList,
pub prev_len: usize,
pub color_before_text_length: usize,
pub style_list: StyleList,
}
impl CuteText {
/// Adds the color passed by parameter to `self.text` and updates its text.
///
/// Equivalent to calling `add_style_or_color(color)`.
///
/// # Arguments
///
/// * `color: &str` - The color that will be added to the `self.text`.
///
/// # Returns
///
/// A mutable reference to the updated `self.text`.
fn add_color(&mut self, color: &str) -> &mut Self {
self.add_style_or_color(color)
}
/// Adds the style passed by parameter to `self.text` and updates its text.
///
/// Equivalent to calling `add_style_or_color(style)`.
///
/// # Arguments
///
/// * `style: &str` - The style that will be added to the `self.text`.
///
/// # Returns
///
/// A mutable reference to the updated `self.text`.
fn add_style(&mut self, style: &str) -> &mut Self {
self.add_style_or_color(style)
}
/// Adds the style or color or 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 style or color or color to the text that was added when using the `add_text` function.
///
/// # Arguments
///
/// * `style or color: &str` - The style or color or color that will be added to the `self.text`.
///
/// # Returns
///
/// A mutable reference to the updated `self.text`.
fn add_style_or_color(&mut self, style_or_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() + style_or_color + after_text;
self.text = modified_text;
}
if self.prev_len == 0 {
self.color_before_text_length += style_or_color.len();
self.text = style_or_color.to_string() + &self.text;
}
self.text = self.text.to_owned() + self.color_list.reset;
self
}
}
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,
color_before_text_length: 0,
style_list: StyleList::new(),
}
}
/// 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 sets the bold text
pub fn bold(&mut self) -> &mut Self {
self.add_style(self.style_list.bold);
self
}
/// This function sets the dim text
pub fn dim(&mut self) -> &mut Self {
self.add_style(self.style_list.dim);
self
}
/// This function sets the underline text
pub fn underline(&mut self) -> &mut Self {
self.add_style(self.style_list.underline);
self
}
/// This function sets the blink text
pub fn blink(&mut self) -> &mut Self {
self.add_style(self.style_list.blink);
self
}
/// This function sets the reverse text
pub fn reverse(&mut self) -> &mut Self {
self.add_style(self.style_list.reverse);
self
}
/// This function sets the hidden text
pub fn hidden(&mut self) -> &mut Self {
self.add_style(self.style_list.hidden);
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 a new text at the beginning of the current text applying its colors.
pub fn add_text_at_the_beginning(&mut self, text_to_add: &str) -> &mut Self {
let text: &str = &self.text[self.color_before_text_length..self.text.len()];
let color: &str = &self.text[0..self.color_before_text_length];
let new_text: String = color.to_owned() + text_to_add + text;
self.text = new_text;
self
}
}