color_output/task/impl.rs
1use crate::*;
2
3/// Default implementation for Task with empty text list.
4impl<'a> Default for Task<'a> {
5 #[inline(always)]
6 fn default() -> Self {
7 Self { text_list: vec![] }
8 }
9}
10
11/// Implementation of task operations.
12impl<'a> Task<'a> {
13 /// Adds a text configuration to the task list.
14 ///
15 /// # Arguments
16 ///
17 /// - `Text` - The text configuration to add
18 ///
19 /// # Returns
20 ///
21 /// - `&mut Self` - The task for method chaining
22 #[inline(always)]
23 pub fn add(&mut self, new_text: Text<'a>) -> &mut Self {
24 self.text_list.push(new_text);
25 self
26 }
27
28 /// Clears all text configurations from the task list.
29 ///
30 /// # Returns
31 ///
32 /// - `&mut Self` - The cleared task for method chaining
33 #[inline(always)]
34 pub(crate) fn clear(&mut self) -> &mut Self {
35 self.text_list.clear();
36 self
37 }
38
39 /// Runs all tasks in the list.
40 ///
41 /// # Arguments
42 ///
43 /// - `&mut Self` - The mutable task instance.
44 ///
45 /// # Returns
46 ///
47 /// - `&mut Self` - The task instance after execution.
48 pub fn run_all(&mut self) -> &mut Self {
49 let text_list: Vec<Text<'_>> = self.text_list.clone();
50 self.clear();
51 let mut output_str: String = String::with_capacity(text_list.len());
52 for text in text_list.iter() {
53 output_str.push_str(&Text::new_from(text).get_display_str_cow());
54 }
55 print!("{output_str}");
56 std::io::stdout().flush().unwrap();
57 self
58 }
59}