color_output/output_list_builder/
impl.rs

1use crate::*;
2
3impl<'a> Default for OutputListBuilder<'a> {
4    fn default() -> Self {
5        Self::new()
6    }
7}
8
9impl<'a> OutputListBuilder<'a> {
10    /// Creates a new empty OutputListBuilder.
11    ///
12    /// # Returns
13    ///
14    /// - `OutputListBuilder` - New instance with empty output list
15    #[inline]
16    pub fn new() -> Self {
17        Self {
18            output_list: vec![],
19        }
20    }
21
22    /// Creates a new OutputListBuilder from existing outputs.
23    ///
24    /// # Arguments
25    ///
26    /// - `Vec<Output>` - Collection of outputs to initialize with
27    ///
28    /// # Returns
29    ///
30    /// - `OutputListBuilder` - New instance containing the specified outputs
31    #[inline]
32    pub fn new_from(output_list: Vec<Output<'a>>) -> Self {
33        Self { output_list }
34    }
35
36    /// Adds an output to the list.
37    ///
38    /// # Arguments
39    ///
40    /// - `Output` - The output configuration to add
41    ///
42    /// # Returns
43    ///
44    /// - `&mut Self` - The builder for method chaining
45    #[inline]
46    pub fn add(&mut self, output: Output<'a>) -> &mut Self {
47        self.output_list.push(output);
48        self
49    }
50
51    /// Removes an output item from the list at the specified index.
52    ///
53    /// # Parameters
54    /// - `&mut self`: A mutable reference to the current instance of `OutputListBuilder`.
55    /// - `idx`: The index of the output item to be removed.
56    ///
57    /// # Returns
58    /// - `&mut Self`: A mutable reference to the current instance, allowing for method chaining.
59    ///
60    /// If the index is out of bounds, the list remains unchanged.
61    pub fn remove(&mut self, idx: usize) -> &mut Self {
62        if idx >= self.output_list.len() {
63            return self;
64        }
65        self.output_list.remove(idx);
66        self
67    }
68
69    /// Clears all output items from the output list.
70    ///
71    /// # Parameters
72    /// - `&mut self`: A mutable reference to the current instance of `OutputListBuilder`.
73    #[inline]
74    pub fn clear(&mut self) {
75        self.output_list.clear();
76    }
77
78    /// Runs all output items in the list, executing their output logic.
79    ///
80    /// # Parameters
81    /// - `&mut self`: A mutable reference to the current instance of `OutputListBuilder`.
82    ///
83    /// # Returns
84    /// - `&mut Self`: A mutable reference to the current instance, allowing for method chaining.
85    ///
86    /// The method clones the current output list, clears the original list, and executes
87    /// the output for each cloned item.
88    pub fn run(&mut self) -> &mut Self {
89        let output_list_clone: Vec<Output<'a>> = self.output_list.clone();
90        self.clear();
91        output_list(&output_list_clone.to_vec());
92        self
93    }
94
95    /// Queries the output item at the specified index.
96    ///
97    /// # Parameters
98    /// - `&self`: An immutable reference to the current instance of `OutputListBuilder`.
99    /// - `idx`: The index of the output item to query.
100    ///
101    /// # Returns
102    /// - `Output`: The output item at the specified index, or a default output if the index is out of bounds.
103    pub fn query_idx(&'_ self, idx: usize) -> Output<'_> {
104        if idx >= self.output_list.len() {
105            return Output::default();
106        }
107        self.output_list[idx].clone()
108    }
109
110    /// Runs the output item at the specified index.
111    ///
112    /// # Parameters
113    /// - `&mut self`: A mutable reference to the current instance of `OutputListBuilder`.
114    /// - `idx`: The index of the output item to run.
115    ///
116    /// # Returns
117    /// - `&mut Self`: A mutable reference to the current instance, allowing for method chaining.
118    ///
119    /// If the index is out of bounds, the list remains unchanged.
120    pub fn run_idx(&mut self, idx: usize) -> &mut Self {
121        if idx >= self.output_list.len() {
122            return self;
123        }
124        let output: Output<'_> = self.query_idx(idx);
125        output.output();
126        self
127    }
128}