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
#[derive(Copy, Clone, Debug, Default)]
pub(crate) struct Palette {
    description: anstyle::Style,
    var: anstyle::Style,
    expected: anstyle::Style,
}

impl Palette {
    pub(crate) fn new(alternate: bool) -> Self {
        if alternate && cfg!(feature = "color") {
            Self {
                description: anstyle::AnsiColor::Blue.on_default() | anstyle::Effects::BOLD,
                var: anstyle::AnsiColor::Red.on_default() | anstyle::Effects::BOLD,
                expected: anstyle::AnsiColor::Green.on_default() | anstyle::Effects::BOLD,
            }
        } else {
            Self::plain()
        }
    }

    pub(crate) fn plain() -> Self {
        Self {
            description: Default::default(),
            var: Default::default(),
            expected: Default::default(),
        }
    }

    pub(crate) fn description<D: std::fmt::Display>(self, display: D) -> Styled<D> {
        Styled::new(display, self.description)
    }

    pub(crate) fn var<D: std::fmt::Display>(self, display: D) -> Styled<D> {
        Styled::new(display, self.var)
    }

    pub(crate) fn expected<D: std::fmt::Display>(self, display: D) -> Styled<D> {
        Styled::new(display, self.expected)
    }
}

#[derive(Debug)]
pub(crate) struct Styled<D> {
    display: D,
    style: anstyle::Style,
}

impl<D: std::fmt::Display> Styled<D> {
    pub(crate) fn new(display: D, style: anstyle::Style) -> Self {
        Self { display, style }
    }
}

impl<D: std::fmt::Display> std::fmt::Display for Styled<D> {
    #[inline]
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if f.alternate() {
            write!(f, "{}", self.style.render())?;
            self.display.fmt(f)?;
            write!(f, "{}", self.style.render_reset())?;
            Ok(())
        } else {
            self.display.fmt(f)
        }
    }
}