cute_print/cute/
cute_text.rs1use crate::StyleList;
2
3use super::super::ColorList;
4
5pub 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 fn add_color(&mut self, color: &str) -> &mut Self {
28 self.add_style_or_color(color)
29 }
30
31 fn add_style(&mut self, style: &str) -> &mut Self {
43 self.add_style_or_color(style)
44 }
45
46 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 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 pub fn black(&mut self) -> &mut Self {
89 self.add_color(self.color_list.black_fg);
90 self
91 }
92
93 pub fn red(&mut self) -> &mut Self {
95 self.add_color(self.color_list.red_fg);
96 self
97 }
98
99 pub fn green(&mut self) -> &mut Self {
101 self.add_color(self.color_list.green_fg);
102 self
103 }
104
105 pub fn yellow(&mut self) -> &mut Self {
107 self.add_color(self.color_list.yellow_fg);
108 self
109 }
110
111 pub fn blue(&mut self) -> &mut Self {
113 self.add_color(self.color_list.blue_fg);
114 self
115 }
116
117 pub fn magenta(&mut self) -> &mut Self {
119 self.add_color(self.color_list.magenta_fg);
120 self
121 }
122
123 pub fn cyan(&mut self) -> &mut Self {
125 self.add_color(self.color_list.cyan_fg);
126 self
127 }
128
129 pub fn white(&mut self) -> &mut Self {
131 self.add_color(self.color_list.white_fg);
132 self
133 }
134
135 pub fn bright_black(&mut self) -> &mut Self {
137 self.add_color(self.color_list.bright_black_fg);
138 self
139 }
140
141 pub fn bright_red(&mut self) -> &mut Self {
143 self.add_color(self.color_list.bright_red_fg);
144 self
145 }
146
147 pub fn bright_green(&mut self) -> &mut Self {
149 self.add_color(self.color_list.bright_green_fg);
150 self
151 }
152
153 pub fn bright_yellow(&mut self) -> &mut Self {
155 self.add_color(self.color_list.bright_yellow_fg);
156 self
157 }
158
159 pub fn bright_blue(&mut self) -> &mut Self {
161 self.add_color(self.color_list.bright_blue_fg);
162 self
163 }
164
165 pub fn bright_magenta(&mut self) -> &mut Self {
167 self.add_color(self.color_list.bright_magenta_fg);
168 self
169 }
170
171 pub fn bright_cyan(&mut self) -> &mut Self {
173 self.add_color(self.color_list.bright_cyan_fg);
174 self
175 }
176
177 pub fn bright_white(&mut self) -> &mut Self {
179 self.add_color(self.color_list.bright_white_fg);
180 self
181 }
182
183 pub fn on_black(&mut self) -> &mut Self {
185 self.add_color(self.color_list.black_bg);
186 self
187 }
188
189 pub fn on_red(&mut self) -> &mut Self {
191 self.add_color(self.color_list.red_bg);
192 self
193 }
194
195 pub fn on_green(&mut self) -> &mut Self {
197 self.add_color(self.color_list.green_bg);
198 self
199 }
200
201 pub fn on_yellow(&mut self) -> &mut Self {
203 self.add_color(self.color_list.yellow_bg);
204 self
205 }
206
207 pub fn on_blue(&mut self) -> &mut Self {
209 self.add_color(self.color_list.blue_bg);
210 self
211 }
212
213 pub fn on_magenta(&mut self) -> &mut Self {
215 self.add_color(self.color_list.magenta_bg);
216 self
217 }
218
219 pub fn on_cyan(&mut self) -> &mut Self {
221 self.add_color(self.color_list.cyan_bg);
222 self
223 }
224
225 pub fn on_white(&mut self) -> &mut Self {
227 self.add_color(self.color_list.white_bg);
228 self
229 }
230
231 pub fn on_bright_black(&mut self) -> &mut Self {
233 self.add_color(self.color_list.bright_black_bg);
234 self
235 }
236
237 pub fn on_bright_red(&mut self) -> &mut Self {
239 self.add_color(self.color_list.bright_red_bg);
240 self
241 }
242
243 pub fn on_bright_green(&mut self) -> &mut Self {
245 self.add_color(self.color_list.bright_green_bg);
246 self
247 }
248
249 pub fn on_bright_yellow(&mut self) -> &mut Self {
251 self.add_color(self.color_list.bright_yellow_bg);
252 self
253 }
254
255 pub fn on_bright_blue(&mut self) -> &mut Self {
257 self.add_color(self.color_list.bright_blue_bg);
258 self
259 }
260
261 pub fn on_bright_magenta(&mut self) -> &mut Self {
263 self.add_color(self.color_list.bright_magenta_bg);
264 self
265 }
266
267 pub fn on_bright_cyan(&mut self) -> &mut Self {
269 self.add_color(self.color_list.bright_cyan_bg);
270 self
271 }
272
273 pub fn on_bright_white(&mut self) -> &mut Self {
275 self.add_color(self.color_list.bright_white_bg);
276 self
277 }
278
279 pub fn bold(&mut self) -> &mut Self {
281 self.add_style(self.style_list.bold);
282 self
283 }
284
285 pub fn dim(&mut self) -> &mut Self {
287 self.add_style(self.style_list.dim);
288 self
289 }
290
291 pub fn underline(&mut self) -> &mut Self {
293 self.add_style(self.style_list.underline);
294 self
295 }
296
297 pub fn blink(&mut self) -> &mut Self {
299 self.add_style(self.style_list.blink);
300 self
301 }
302
303 pub fn reverse(&mut self) -> &mut Self {
305 self.add_style(self.style_list.reverse);
306 self
307 }
308
309 pub fn hidden(&mut self) -> &mut Self {
311 self.add_style(self.style_list.hidden);
312 self
313 }
314 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 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}