color_output/task/
impl.rs

1use super::r#type::*;
2use crate::*;
3use std::{borrow::Cow, io::Write};
4use text::r#type::*;
5
6impl<'a> Default for Task<'a> {
7    #[inline]
8    fn default() -> Self {
9        Self { text_list: vec![] }
10    }
11}
12
13impl<'a> Task<'a> {
14    /// Adds a text structure to the task list.
15    ///
16    /// # Parameters
17    /// - `&mut self`: A mutable reference to the current task instance.
18    /// - `new_text`: The text structure to be added.
19    ///
20    /// # Returns
21    /// - `&mut Self`: A mutable reference to the current task instance, allowing method chaining.
22    ///
23    /// If the `new_text` is empty, no text is added and the method returns the current task instance unchanged.
24    #[inline]
25    pub(crate) fn add(&mut self, new_text: Text<'a>) -> &mut Self {
26        if new_text.text.is_empty() {
27            return self;
28        }
29        self.text_list.push(new_text);
30        self
31    }
32
33    /// Removes a task at the specified index.
34    ///
35    /// # Parameters
36    /// - `&mut self`: The current task instance
37    /// - `idx`: The index of the task to be removed
38    ///
39    /// # Returns
40    /// - `TaskResult`: The result of the removal operation
41    #[inline]
42    pub(crate) fn remove(&mut self, idx: usize) -> TaskResult {
43        if idx >= self.text_list.len() {
44            return TaskResult::Fail;
45        }
46        self.text_list.remove(idx);
47        TaskResult::SuccessDefault
48    }
49
50    /// Queries a task at the specified index.
51    ///
52    /// # Parameters
53    /// - `&mut self`: The current task instance
54    /// - `idx`: The index of the task to be queried
55    ///
56    /// # Returns
57    /// - `TaskResult`: The result of the query
58    #[inline]
59    pub(crate) fn query_idx(&mut self, idx: usize) -> TaskResult {
60        if idx >= self.text_list.len() {
61            return TaskResult::Fail;
62        }
63        let text: Text<'a> = self.text_list[idx].clone();
64        let remove_res: TaskResult<'_> = self.remove(idx);
65        match remove_res {
66            TaskResult::SuccessDefault => {
67                let output_str: Cow<'_, str> = Text::new_from(&text).get_display_str_cow();
68                return TaskResult::SuccessText(text);
69            }
70            _ => remove_res,
71        }
72    }
73
74    /// Queries a task at the specified index and formats it into a string.
75    ///
76    /// # Parameters
77    /// - `&mut self`: The current task instance
78    /// - `idx`: The index of the task to be queried
79    ///
80    /// # Returns
81    /// - `TaskResult`: The result of the query in string format
82    #[inline]
83    pub(crate) fn query_idx_format_str(&mut self, idx: usize) -> TaskResult {
84        if idx >= self.text_list.len() {
85            return TaskResult::Fail;
86        }
87        let text: Text<'a> = self.text_list[idx].clone();
88        self.text_list.remove(idx);
89        let output_str: String = Text::new_from(&text).get_display_str_cow().into_owned();
90        TaskResult::SuccessStr(output_str)
91    }
92
93    /// Runs the task at the specified index.
94    ///
95    /// # Parameters
96    /// - `&mut self`: The current task instance
97    /// - `idx`: The index of the task to be run
98    ///
99    /// # Returns
100    /// - `TaskResult`: The result of the task run
101    #[inline]
102    pub(crate) fn run_idx(&mut self, idx: usize) -> TaskResult {
103        let result: TaskResult<'_> = self.query_idx(idx);
104        if result == TaskResult::Fail {
105            return TaskResult::Fail;
106        }
107        match result {
108            TaskResult::SuccessText(success_text) => {
109                let output_str: Cow<'_, str> = Text::new_from(&success_text).get_display_str_cow();
110                print!("{}", output_str);
111                TaskResult::SuccessText(success_text)
112            }
113            _ => result,
114        }
115    }
116
117    /// Clears all tasks from the task list.
118    ///
119    /// # Parameters
120    /// - `&mut self`: A mutable reference to the current task instance.
121    ///
122    /// # Returns
123    /// - `&mut Self`: A mutable reference to the current task instance, allowing method chaining.
124    ///
125    /// This method removes all tasks from the task list.
126    #[inline]
127    pub(crate) fn clear(&mut self) -> &mut Self {
128        self.text_list.clear();
129        self
130    }
131
132    /// Runs all tasks and outputs the result as a string.
133    ///
134    /// # Parameters
135    /// - `&mut self`: A mutable reference to the current task instance.
136    ///
137    /// # Returns
138    /// - `&mut Self`: A mutable reference to the current task instance, allowing method chaining.
139    ///
140    /// The method clones the task list, clears the original list, and then processes each task by
141    /// converting its output to a string and printing the result.
142    #[inline]
143    pub(crate) fn run_all(&mut self) -> &mut Self {
144        let copy_task_list: Vec<Text<'a>> = self.text_list.clone();
145        self.clear();
146        let mut output_str: String = String::new();
147        for text in copy_task_list {
148            let colored_time: &Cow<'_, str> = &Text::new_from(&text).get_display_str_cow();
149            output_str.push_str(colored_time);
150        }
151        print!("{}", output_str);
152        std::io::stdout().flush().unwrap();
153        self
154    }
155}