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