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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
use core::fmt;
use std::ops::{Add, Sub};

use yansi;

#[derive(Clone, Copy)]
enum Color {
    Black,
    Red,
    Green,
    Blue,
    Magenta,
    Yellow,
    Cyan,
    White,
}

#[derive(Clone, Copy)]
enum Effect {
    Bold,
    Dimmed,
    Italic,
    Underline,
    Blink,
    Invert,
    Hidden,
    Strikethrough,
}

const DEFAULT: Style = Style {
    color: None,
    background: None,
    bold: false,
    dim: false,
    italic: false,
    underlined: false,
    blink: false,
    invert: false,
    hidden: false,
    strikethrough: false,
};

#[derive(Clone, Copy)]
pub struct Style {
    color: Option<Color>,
    background: Option<Color>,
    bold: bool,
    dim: bool,
    italic: bool,
    underlined: bool,
    blink: bool,
    invert: bool,
    hidden: bool,
    strikethrough: bool,
}

impl Style {
    pub fn detect_color_support() {
        if supports_color::on(supports_color::Stream::Stdout).is_none() {
            yansi::Paint::disable();
        }
    }

    pub const fn new() -> Style {
        Style { ..DEFAULT }
    }

    #[must_use]
    const fn set_color(mut self, color: Color) -> Self {
        self.color = Some(color);
        self
    }

    #[must_use]
    const fn set_background(mut self, color: Color) -> Self {
        self.background = Some(color);
        self
    }

    #[must_use]
    const fn set_effect(mut self, effect: Effect, enabled: bool) -> Self {
        match effect {
            Effect::Bold => self.bold = enabled,
            Effect::Dimmed => self.dim = enabled,
            Effect::Italic => self.italic = enabled,
            Effect::Underline => self.underlined = enabled,
            Effect::Blink => self.blink = enabled,
            Effect::Invert => self.invert = enabled,
            Effect::Hidden => self.hidden = enabled,
            Effect::Strikethrough => self.strikethrough = enabled,
        }
        self
    }

    #[must_use]
    pub const fn black(&self) -> Style {
        self.set_color(Color::Black)
    }
    #[must_use]
    pub const fn red(&self) -> Style {
        self.set_color(Color::Red)
    }
    #[must_use]
    pub const fn green(&self) -> Style {
        self.set_color(Color::Green)
    }
    #[must_use]
    pub const fn yellow(&self) -> Style {
        self.set_color(Color::Yellow)
    }
    #[must_use]
    pub const fn blue(&self) -> Style {
        self.set_color(Color::Blue)
    }
    #[must_use]
    pub const fn magenta(&self) -> Style {
        self.set_color(Color::Magenta)
    }
    #[must_use]
    pub const fn cyan(&self) -> Style {
        self.set_color(Color::Cyan)
    }
    #[must_use]
    pub const fn white(&self) -> Style {
        self.set_color(Color::White)
    }

    #[must_use]
    pub const fn on_black(&self) -> Style {
        self.set_background(Color::Black)
    }
    #[must_use]
    pub const fn on_red(&self) -> Style {
        self.set_background(Color::Red)
    }
    #[must_use]
    pub const fn on_green(&self) -> Style {
        self.set_background(Color::Green)
    }
    #[must_use]
    pub const fn on_yellow(&self) -> Style {
        self.set_background(Color::Yellow)
    }
    #[must_use]
    pub const fn on_blue(&self) -> Style {
        self.set_background(Color::Blue)
    }
    #[must_use]
    pub const fn on_magenta(&self) -> Style {
        self.set_background(Color::Magenta)
    }
    #[must_use]
    pub const fn on_cyan(&self) -> Style {
        self.set_background(Color::Cyan)
    }
    #[must_use]
    pub const fn on_white(&self) -> Style {
        self.set_background(Color::White)
    }

    #[must_use]
    pub const fn bold(&self) -> Style {
        self.set_effect(Effect::Bold, true)
    }
    #[must_use]
    pub const fn dim(&self) -> Style {
        self.set_effect(Effect::Dimmed, true)
    }
    #[must_use]
    pub const fn italic(&self) -> Style {
        self.set_effect(Effect::Italic, true)
    }
    #[must_use]
    pub const fn underlined(&self) -> Style {
        self.set_effect(Effect::Underline, true)
    }
    #[must_use]
    pub const fn blink(&self) -> Style {
        self.set_effect(Effect::Blink, true)
    }
    #[must_use]
    pub const fn invert(&self) -> Style {
        self.set_effect(Effect::Invert, true)
    }
    #[must_use]
    pub const fn hidden(&self) -> Style {
        self.set_effect(Effect::Hidden, true)
    }
    #[must_use]
    pub const fn strikethrough(&self) -> Style {
        self.set_effect(Effect::Strikethrough, true)
    }

    #[must_use]
    const fn merge(&self, style: Style) -> Style {
        Style {
            color: match style.color {
                Some(color) => Some(color),
                None => self.color,
            },
            background: match style.background {
                Some(color) => Some(color),
                None => self.background,
            },
            bold: style.bold || self.bold,
            dim: style.dim || self.dim,
            italic: style.italic || self.italic,
            underlined: style.underlined || self.underlined,
            blink: style.blink || self.blink,
            invert: style.invert || self.invert,
            hidden: style.hidden || self.hidden,
            strikethrough: style.strikethrough || self.strikethrough,
        }
    }

    #[must_use]
    pub fn apply<D: fmt::Display>(&self, val: D) -> impl fmt::Display {
        let style: yansi::Style = self.into();
        style.paint(val)
    }
}

impl From<&Style> for yansi::Style {
    fn from(style: &Style) -> Self {
        let mut converted = yansi::Style::new(yansi::Color::Unset);

        if let Some(fg) = style.color {
            converted = converted.fg(fg.into());
        };

        if let Some(bg) = style.background {
            converted = converted.fg(bg.into());
        };

        if style.bold {
            converted = converted.bold()
        }
        if style.dim {
            converted = converted.dimmed()
        }
        if style.italic {
            converted = converted.italic()
        }
        if style.underlined {
            converted = converted.underline()
        }
        if style.blink {
            converted = converted.blink()
        }
        if style.invert {
            converted = converted.invert()
        }
        if style.hidden {
            converted = converted.hidden()
        }
        if style.strikethrough {
            converted = converted.strikethrough()
        }

        converted
    }
}

impl From<Color> for Style {
    fn from(color: Color) -> Self {
        Style::new().set_color(color)
    }
}

impl From<Effect> for Style {
    fn from(effect: Effect) -> Self {
        Style::new().set_effect(effect, true)
    }
}

impl From<Color> for yansi::Color {
    fn from(color: Color) -> Self {
        match color {
            Color::Black => yansi::Color::Black,
            Color::Red => yansi::Color::Red,
            Color::Green => yansi::Color::Green,
            Color::Blue => yansi::Color::Blue,
            Color::Magenta => yansi::Color::Magenta,
            Color::Cyan => yansi::Color::Cyan,
            Color::Yellow => yansi::Color::Yellow,
            Color::White => yansi::Color::White,
        }
    }
}

impl Add<Style> for Style {
    type Output = Style;
    fn add(self, rhs: Style) -> Self::Output {
        self.merge(rhs)
    }
}

impl Add<Color> for Style {
    type Output = Style;
    fn add(self, rhs: Color) -> Self::Output {
        self.set_color(rhs)
    }
}

impl Add<Effect> for Style {
    type Output = Style;
    fn add(self, effect: Effect) -> Self::Output {
        self.set_effect(effect, true)
    }
}

impl Sub<Effect> for Style {
    type Output = Style;
    fn sub(self, effect: Effect) -> Self::Output {
        self.set_effect(effect, false)
    }
}