clerr/report/
properties.rs

1use crate::{Report, Severity};
2use colored::{ColoredString, Colorize};
3
4impl Report {
5    //! Properties
6
7    /// Generates the `properties` entry.
8    ///
9    /// # Display
10    ///
11    /// ```text
12    ///     first:  value
13    ///     second: another value
14    ///     third:  third value
15    /// ```
16    pub fn properties(mut properties: Vec<(String, String)>) -> Vec<ColoredString> {
17        let max_len: usize = properties.iter().map(|(p, _)| p.len()).max().unwrap_or(0);
18        let mut entry: Vec<ColoredString> = Vec::with_capacity(properties.len() * 6);
19        for (property, value) in properties.drain(..) {
20            let len: usize = property.len();
21            let spaces: usize = max_len - len + 2;
22
23            entry.push("    ".normal());
24            entry.push(property.color(Severity::Info.color()));
25            entry.push(":".color(Severity::Info.color()));
26            entry.push(Self::char_count(' ', spaces).normal());
27            entry.push(value.normal());
28            entry.push("\n".normal());
29        }
30        entry
31    }
32
33    /// Adds the `properties` entry.
34    ///
35    /// See `Report::properties`.
36    pub fn add_properties(&mut self, properties: Vec<(String, String)>) {
37        self.add_entry(Self::properties(properties));
38    }
39
40    /// Adds the `properties` entry.
41    ///
42    /// See `Report::properties`.
43    pub fn with_properties(mut self, properties: Vec<(String, String)>) -> Self {
44        self.add_properties(properties);
45        self
46    }
47}
48
49#[cfg(test)]
50mod tests {
51    use crate::Report;
52    use colored::ColoredString;
53
54    #[test]
55    #[ignore]
56    fn properties() {
57        let info: Vec<ColoredString> = Report::properties(vec![
58            ("one".to_string(), "two".to_string()),
59            ("three".to_string(), "four".to_string()),
60            ("five".to_string(), "six".to_string()),
61        ]);
62
63        for s in info {
64            print!("{s}");
65        }
66    }
67}