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