cute_print/cute/
cute_text.rs

1use crate::StyleList;
2
3use super::super::ColorList;
4
5/// The `CuteText` struct contains a string of text, a reference to a `ColorList`
6/// 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.
7pub struct CuteText {
8    pub text: String,
9    pub color_list: ColorList,
10    pub prev_len: usize,
11    pub color_before_text_length: usize,
12    pub style_list: StyleList,
13}
14
15impl CuteText {
16    /// Adds the color passed by parameter to `self.text` and updates its text.
17    ///
18    /// Equivalent to calling `add_style_or_color(color)`.
19    ///
20    /// # Arguments
21    ///
22    /// * `color: &str` - The color that will be added to the `self.text`.
23    ///
24    /// # Returns
25    ///
26    /// A mutable reference to the updated `self.text`.
27    fn add_color(&mut self, color: &str) -> &mut Self {
28        self.add_style_or_color(color)
29    }
30
31    /// Adds the style passed by parameter to `self.text` and updates its text.
32    ///
33    /// Equivalent to calling `add_style_or_color(style)`.
34    ///
35    /// # Arguments
36    ///
37    /// * `style: &str` - The style that will be added to the `self.text`.
38    ///
39    /// # Returns
40    ///
41    /// A mutable reference to the updated `self.text`.
42    fn add_style(&mut self, style: &str) -> &mut Self {
43        self.add_style_or_color(style)
44    }
45
46    /// Adds the style or color or color passed by parameter to `self.text` and updates its text.
47    ///
48    /// If `self.prev_len` is greater than 0 it extracts the text that was there before using the `add_text` function 
49    /// and adds the style or color or color to the text that was added when using the `add_text` function.
50    ///
51    /// # Arguments
52    ///
53    /// * `style or color: &str` - The style or color or color that will be added to the `self.text`.
54    ///
55    /// # Returns
56    ///
57    /// A mutable reference to the updated `self.text`.
58    fn add_style_or_color(&mut self, style_or_color: &str) -> &mut Self {
59        if self.prev_len > 0 {
60            let before_text: &str = &self.text[0..self.prev_len];
61            let after_text: &str = &self.text[self.prev_len..self.text.len()];
62            let modified_text: String = before_text.to_owned() + style_or_color + after_text;
63            self.text = modified_text;
64        }
65
66        if self.prev_len == 0 {
67            self.color_before_text_length += style_or_color.len();
68            self.text = style_or_color.to_string() + &self.text;
69        }
70        self.text = self.text.to_owned() + self.color_list.reset;
71        self
72    }
73}
74
75impl CuteText {
76    /// This function creates a new instance of the `CuteText` struct.
77    pub fn new() -> Self {
78        CuteText {
79            text: String::new(),
80            color_list: ColorList::new(),
81            prev_len: 0,
82            color_before_text_length: 0,
83            style_list: StyleList::new(),
84        }
85    }
86
87    /// This function sets the text color to black.
88    pub fn black(&mut self) -> &mut Self {
89        self.add_color(self.color_list.black_fg);
90        self
91    }
92
93    /// This function sets the text color to red.
94    pub fn red(&mut self) -> &mut Self {
95        self.add_color(self.color_list.red_fg);
96        self
97    }
98
99    /// This function sets the text color to green.
100    pub fn green(&mut self) -> &mut Self {
101        self.add_color(self.color_list.green_fg);
102        self
103    }
104
105    /// This function sets the text color to yellow.
106    pub fn yellow(&mut self) -> &mut Self {
107        self.add_color(self.color_list.yellow_fg);
108        self
109    }
110
111    /// This function sets the text color to blue.
112    pub fn blue(&mut self) -> &mut Self {
113        self.add_color(self.color_list.blue_fg);
114        self
115    }
116
117    /// This function sets the text color to magenta.
118    pub fn magenta(&mut self) -> &mut Self {
119        self.add_color(self.color_list.magenta_fg);
120        self
121    }
122
123    /// This function sets the text color to cyan.
124    pub fn cyan(&mut self) -> &mut Self {
125        self.add_color(self.color_list.cyan_fg);
126        self
127    }
128
129    /// This function sets the text color to white.
130    pub fn white(&mut self) -> &mut Self {
131        self.add_color(self.color_list.white_fg);
132        self
133    }
134
135    /// This function sets the text color to bright black.
136    pub fn bright_black(&mut self) -> &mut Self {
137        self.add_color(self.color_list.bright_black_fg);
138        self
139    }
140
141    /// This function sets the text color to bright red.
142    pub fn bright_red(&mut self) -> &mut Self {
143        self.add_color(self.color_list.bright_red_fg);
144        self
145    }
146
147    /// This function sets the text color to bright green.
148    pub fn bright_green(&mut self) -> &mut Self {
149        self.add_color(self.color_list.bright_green_fg);
150        self
151    }
152
153    /// This function sets the text color to bright yellow.
154    pub fn bright_yellow(&mut self) -> &mut Self {
155        self.add_color(self.color_list.bright_yellow_fg);
156        self
157    }
158
159    /// This function sets the text color to bright blue.
160    pub fn bright_blue(&mut self) -> &mut Self {
161        self.add_color(self.color_list.bright_blue_fg);
162        self
163    }
164
165    /// This function sets the text color to bright magenta.
166    pub fn bright_magenta(&mut self) -> &mut Self {
167        self.add_color(self.color_list.bright_magenta_fg);
168        self
169    }
170
171    /// This function sets the text color to bright cyan.
172    pub fn bright_cyan(&mut self) -> &mut Self {
173        self.add_color(self.color_list.bright_cyan_fg);
174        self
175    }
176
177    /// This function sets the text color to bright white.
178    pub fn bright_white(&mut self) -> &mut Self {
179        self.add_color(self.color_list.bright_white_fg);
180        self
181    }
182
183    /// This function sets the background color to on_black.
184    pub fn on_black(&mut self) -> &mut Self {
185        self.add_color(self.color_list.black_bg);
186        self
187    }
188
189    /// This function sets the background color to red.
190    pub fn on_red(&mut self) -> &mut Self {
191        self.add_color(self.color_list.red_bg);
192        self
193    }
194
195    /// This function sets the background color to green.
196    pub fn on_green(&mut self) -> &mut Self {
197        self.add_color(self.color_list.green_bg);
198        self
199    }
200
201    /// This function sets the background color to yellow.
202    pub fn on_yellow(&mut self) -> &mut Self {
203        self.add_color(self.color_list.yellow_bg);
204        self
205    }
206
207    /// This function sets the background color to blue.
208    pub fn on_blue(&mut self) -> &mut Self {
209        self.add_color(self.color_list.blue_bg);
210        self
211    }
212
213    /// This function sets the background color to magenta.
214    pub fn on_magenta(&mut self) -> &mut Self {
215        self.add_color(self.color_list.magenta_bg);
216        self
217    }
218
219    /// This function sets the background color to cyan.
220    pub fn on_cyan(&mut self) -> &mut Self {
221        self.add_color(self.color_list.cyan_bg);
222        self
223    }
224
225    /// This function sets the background color to white.
226    pub fn on_white(&mut self) -> &mut Self {
227        self.add_color(self.color_list.white_bg);
228        self
229    }
230
231    /// This function sets the background color bright black.
232    pub fn on_bright_black(&mut self) -> &mut Self {
233        self.add_color(self.color_list.bright_black_bg);
234        self
235    }
236
237    /// This function sets the background color to bright red.
238    pub fn on_bright_red(&mut self) -> &mut Self {
239        self.add_color(self.color_list.bright_red_bg);
240        self
241    }
242
243    /// This function sets the background color to bright green.
244    pub fn on_bright_green(&mut self) -> &mut Self {
245        self.add_color(self.color_list.bright_green_bg);
246        self
247    }
248
249    /// This function sets the background color to bright yellow.
250    pub fn on_bright_yellow(&mut self) -> &mut Self {
251        self.add_color(self.color_list.bright_yellow_bg);
252        self
253    }
254
255    /// This function sets the background color to bright blue.
256    pub fn on_bright_blue(&mut self) -> &mut Self {
257        self.add_color(self.color_list.bright_blue_bg);
258        self
259    }
260
261    /// This function sets the background color to bright magenta.
262    pub fn on_bright_magenta(&mut self) -> &mut Self {
263        self.add_color(self.color_list.bright_magenta_bg);
264        self
265    }
266
267    /// This function sets the background color to bright cyan.
268    pub fn on_bright_cyan(&mut self) -> &mut Self {
269        self.add_color(self.color_list.bright_cyan_bg);
270        self
271    }
272
273    /// This function sets the background color to bright white.
274    pub fn on_bright_white(&mut self) -> &mut Self {
275        self.add_color(self.color_list.bright_white_bg);
276        self
277    }
278
279    /// This function sets the bold text
280    pub fn bold(&mut self) -> &mut Self {
281        self.add_style(self.style_list.bold);
282        self
283    }
284
285    /// This function sets the dim text
286    pub fn dim(&mut self) -> &mut Self {
287        self.add_style(self.style_list.dim);
288        self
289    }
290
291    /// This function sets the underline text
292    pub fn underline(&mut self) -> &mut Self {
293        self.add_style(self.style_list.underline);
294        self
295    }
296
297    /// This function sets the blink text
298    pub fn blink(&mut self) -> &mut Self {
299        self.add_style(self.style_list.blink);
300        self
301    }
302
303    /// This function sets the reverse text
304    pub fn reverse(&mut self) -> &mut Self {
305        self.add_style(self.style_list.reverse);
306        self
307    }
308
309    /// This function sets the hidden text
310    pub fn hidden(&mut self) -> &mut Self {
311        self.add_style(self.style_list.hidden);
312        self
313    }
314    /// This function fills the variable self.prev_len with the length of the text before the new text is added
315    /// and then adds the text passed as a parameter to self.text.
316    ///
317    /// # Arguments
318    ///
319    /// `text: &str` - The text that will be added to the self.text.
320    ///
321    /// # Returns
322    ///
323    /// `&mut Self ` - A mutable reference to the updated
324    pub fn add_text(&mut self, text: &str) -> &mut Self {
325        self.prev_len = self.text.len();
326        self.text = self.text.to_string() + text;
327        self
328    }
329
330    /// Adds a new text at the beginning of the current text applying its colors.
331    pub fn add_text_at_the_beginning(&mut self, text_to_add: &str) -> &mut Self {
332        let text: &str = &self.text[self.color_before_text_length..self.text.len()];
333        let color: &str = &self.text[0..self.color_before_text_length];
334        let new_text: String = color.to_owned() + text_to_add + text;
335        self.text = new_text;
336        self
337    }
338}