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
124
125
126
127
#[derive(Copy, Clone, Debug)]
#[allow(dead_code)]
pub struct Palette {
    pub(crate) info: styled::Style,
    pub(crate) warn: styled::Style,
    pub(crate) error: styled::Style,
    pub(crate) hint: styled::Style,
    pub(crate) expected: styled::Style,
    pub(crate) actual: styled::Style,
}

impl Palette {
    #[cfg(feature = "color")]
    pub fn always() -> Self {
        Self {
            info: styled::Style(yansi::Style::new(yansi::Color::Green)),
            warn: styled::Style(yansi::Style::new(yansi::Color::Yellow)),
            error: styled::Style(yansi::Style::new(yansi::Color::Red)),
            hint: styled::Style(yansi::Style::new(yansi::Color::Unset).dimmed()),
            expected: styled::Style(yansi::Style::new(yansi::Color::Green).underline()),
            actual: styled::Style(yansi::Style::new(yansi::Color::Red).underline()),
        }
    }

    #[cfg(not(feature = "color"))]
    pub fn always() -> Self {
        Self::never()
    }

    pub fn never() -> Self {
        Self {
            info: Default::default(),
            warn: Default::default(),
            error: Default::default(),
            hint: Default::default(),
            expected: Default::default(),
            actual: Default::default(),
        }
    }

    pub fn auto() -> Self {
        if is_colored() {
            Self::always()
        } else {
            Self::never()
        }
    }

    pub fn info<D: std::fmt::Display>(self, item: D) -> Styled<D> {
        self.info.paint(item)
    }

    pub fn warn<D: std::fmt::Display>(self, item: D) -> Styled<D> {
        self.warn.paint(item)
    }

    pub fn error<D: std::fmt::Display>(self, item: D) -> Styled<D> {
        self.error.paint(item)
    }

    pub fn hint<D: std::fmt::Display>(self, item: D) -> Styled<D> {
        self.hint.paint(item)
    }

    pub fn expected<D: std::fmt::Display>(self, item: D) -> Styled<D> {
        self.expected.paint(item)
    }

    pub fn actual<D: std::fmt::Display>(self, item: D) -> Styled<D> {
        self.actual.paint(item)
    }
}

fn is_colored() -> bool {
    #[cfg(feature = "color")]
    {
        concolor::get(concolor::Stream::Either).ansi_color()
    }

    #[cfg(not(feature = "color"))]
    {
        false
    }
}

pub(crate) use styled::Style;
pub use styled::Styled;

#[cfg(feature = "color")]
mod styled {
    #[derive(Copy, Clone, Debug, Default)]
    pub(crate) struct Style(pub(crate) yansi::Style);

    impl Style {
        pub(crate) fn paint<T: std::fmt::Display>(self, item: T) -> Styled<T> {
            Styled(self.0.paint(item))
        }
    }

    pub struct Styled<D: std::fmt::Display>(yansi::Paint<D>);

    impl<D: std::fmt::Display> std::fmt::Display for Styled<D> {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            self.0.fmt(f)
        }
    }
}

#[cfg(not(feature = "color"))]
mod styled {
    #[derive(Copy, Clone, Debug, Default)]
    pub(crate) struct Style;

    impl Style {
        pub(crate) fn paint<T: std::fmt::Display>(self, item: T) -> Styled<T> {
            Styled(item)
        }
    }

    pub struct Styled<D: std::fmt::Display>(D);

    impl<D: std::fmt::Display> std::fmt::Display for Styled<D> {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            self.0.fmt(f)
        }
    }
}