paris/formatter/
custom.rs

1use crate::formatter::keys::Key;
2
3pub struct CustomStyle<'a> {
4    key: String,
5    colors: Vec<Key<'a>>,
6}
7
8impl<'a> CustomStyle<'a> {
9    pub fn new(key: &str, colors: Vec<&'a str>) -> Self {
10        Self {
11            key: format!("<{}>", key),
12            colors: colors.iter().map(|s| Key::new(s)).collect(),
13        }
14    }
15
16    pub fn key(&self) -> &str {
17        &self.key
18    }
19
20    pub fn expand(&self) -> String {
21        let mut colors: Vec<String> = Vec::with_capacity(2);
22
23        // Turn it into the ansi values it should be
24        for color in self.colors.iter() {
25            colors.push(color.to_ansi());
26        }
27
28        colors.join("")
29    }
30}
31
32#[cfg(test)]
33mod tests {
34    use crate::formatter::custom::CustomStyle;
35    use crate::formatter::keys::Key;
36
37    #[test]
38    fn ansi_expansion() {
39        let style = CustomStyle::new("lol", vec!["blue"]);
40        let color = Key::new("blue");
41
42        assert_eq!(style.expand(), color.to_ansi());
43    }
44
45    #[test]
46    fn ansi_expansion_multiple() {
47        let style = CustomStyle::new("lol", vec!["blue", "bold", "on-green"]);
48        let colors = vec![Key::new("blue"), Key::new("bold"), Key::new("on-green")];
49
50        let generated: String = colors.iter().map(|k| k.to_ansi()).collect();
51
52        assert_eq!(style.expand(), generated);
53    }
54}