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
//! Provides stuff related to terminal style.
//!
//! They are referenced from [ANSI escape code].
//!
//! [ANSI escape code]: https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_(Select_Graphic_Rendition)_parameters

use crate::Level;

/// The terminal text color style.
#[allow(missing_docs)]
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum Color {
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
}

impl Color {
    // Gets foreground color terminal escape code.
    #[must_use]
    pub(crate) fn fg_code(&self) -> &'static str {
        match self {
            Color::Black => "\x1b[30m",
            Color::Red => "\x1b[31m",
            Color::Green => "\x1b[32m",
            Color::Yellow => "\x1b[33m",
            Color::Blue => "\x1b[34m",
            Color::Magenta => "\x1b[35m",
            Color::Cyan => "\x1b[36m",
            Color::White => "\x1b[37m",
        }
    }

    // Gets background color terminal escape code.
    #[must_use]
    pub(crate) fn bg_code(&self) -> &'static str {
        match self {
            Color::Black => "\x1b[40m",
            Color::Red => "\x1b[41m",
            Color::Green => "\x1b[42m",
            Color::Yellow => "\x1b[43m",
            Color::Blue => "\x1b[44m",
            Color::Magenta => "\x1b[45m",
            Color::Cyan => "\x1b[46m",
            Color::White => "\x1b[47m",
        }
    }
}

/// The terminal text style structure.
///
/// You can construct it easily with [`StyleBuilder`].
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct Style {
    color: Option<Color>,
    bg_color: Option<Color>,
    bold: bool,
    faint: bool,
    italic: bool,
    underline: bool,
    slow_blink: bool,
    rapid_blink: bool,
    invert: bool,
    conceal: bool,
    strikethrough: bool,
    reset: bool,
}

impl Style {
    /// Constructs a `Style` with no styles.
    #[allow(clippy::new_without_default)]
    #[deprecated(
        since = "0.3.0",
        note = "it may be removed in the future, use `Style::builder()` instead"
    )]
    #[must_use]
    pub fn new() -> Style {
        Style::builder().build()
    }

    /// Constructs a [`StyleBuilder`].
    #[must_use]
    pub fn builder() -> StyleBuilder {
        StyleBuilder {
            style: Style {
                color: None,
                bg_color: None,
                bold: false,
                faint: false,
                italic: false,
                underline: false,
                slow_blink: false,
                rapid_blink: false,
                invert: false,
                conceal: false,
                strikethrough: false,
                reset: false,
            },
        }
    }

    #[must_use]
    pub(crate) fn code(&self) -> StyleCode {
        if self.reset {
            return StyleCode {
                start: Style::reset_code(),
                end: Style::reset_code(),
            };
        }

        let mut res = String::new();

        macro_rules! push_escape_code {
            () => {};
            ($field_name:ident: Option => $code:expr, $($tail:tt)*) => {
                if let Some($field_name) = self.$field_name {
                    res.push_str($code);
                }
                push_escape_code! { $($tail)* }
            };
            ($field_name:ident: bool => $code:expr, $($tail:tt)*) => {
                if self.$field_name {
                    res.push_str($code);
                }
                push_escape_code! { $($tail)* }
            };
        }

        push_escape_code! {
            color: Option => color.fg_code(),
            bg_color: Option => bg_color.bg_code(),
            bold: bool => "\x1b[1m",
            faint: bool => "\x1b[2m",
            italic: bool => "\x1b[3m",
            underline: bool => "\x1b[4m",
            slow_blink: bool => "\x1b[5m",
            rapid_blink: bool => "\x1b[6m",
            invert: bool => "\x1b[7m",
            conceal: bool => "\x1b[8m",
            strikethrough: bool => "\x1b[9m",
        }

        StyleCode {
            start: res,
            end: Style::reset_code(),
        }
    }

    #[must_use]
    fn reset_code() -> String {
        "\x1b[m".to_string()
    }
}

/// The builder of [`Style`].
#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub struct StyleBuilder {
    style: Style,
}

pub(crate) mod macros {
    macro_rules! impl_style_builder_setters {
        ($builder_type:ident =>) => {};
        ($builder_type:ident => $visibility:vis $field_name:ident: Option<$field_type:ty>, $($tail:tt)*) => {
            #[allow(missing_docs)]
            $visibility fn $field_name(&mut self, $field_name: $field_type) -> &mut $builder_type {
                self.style.$field_name = Some($field_name);
                self
            }
            macros::impl_style_builder_setters! { $builder_type => $($tail)* }
        };
        ($builder_type:ident => $visibility:vis $field_name:ident: bool, $($tail:tt)*) => {
            #[allow(missing_docs)]
            $visibility fn $field_name(&mut self) -> &mut $builder_type {
                self.style.$field_name = true;
                self
            }
            macros::impl_style_builder_setters! { $builder_type => $($tail)* }
        };
    }
    pub(crate) use impl_style_builder_setters;
}

impl StyleBuilder {
    /// Constructs a `StyleBuilder`.
    #[allow(clippy::new_without_default)]
    #[deprecated(
        since = "0.3.0",
        note = "it may be removed in the future, use `Style::builder()` instead"
    )]
    #[must_use]
    pub fn new() -> StyleBuilder {
        Style::builder()
    }

    macros::impl_style_builder_setters! {
        StyleBuilder =>
        pub reset: bool,
        pub color: Option<Color>,
        pub bg_color: Option<Color>,
        pub bold: bool,
        pub faint: bool,
        pub italic: bool,
        pub underline: bool,
        pub slow_blink: bool,
        pub rapid_blink: bool,
        pub invert: bool,
        pub conceal: bool,
        pub strikethrough: bool,
    }

    /// Builds a [`Style`].
    #[must_use]
    pub fn build(&mut self) -> Style {
        self.style.clone()
    }
}

/// Represents style enable mode.
#[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
pub enum StyleMode {
    /// Always output style escape codes.
    Always,
    /// Output style escape codes only when the target is detected as a
    /// terminal.
    Auto,
    /// Always do not output style escape codes.
    Never,
}

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub(crate) struct LevelStyles([Style; Level::count()]);

impl LevelStyles {
    #[allow(dead_code)]
    #[must_use]
    pub(crate) fn style(&self, level: Level) -> &Style {
        &self.0[level as usize]
    }

    #[allow(dead_code)]
    pub(crate) fn set_style(&mut self, level: Level, style: Style) {
        self.0[level as usize] = style;
    }
}

impl From<LevelStyles> for LevelStyleCodes {
    fn from(level_styles: LevelStyles) -> LevelStyleCodes {
        LevelStyleCodes(level_styles.0.map(|style| style.into()))
    }
}

impl Default for LevelStyles {
    fn default() -> LevelStyles {
        LevelStyles([
            Style::builder().bg_color(Color::Red).bold().build(), // Critical
            Style::builder().color(Color::Red).bold().build(),    // Error
            Style::builder().color(Color::Yellow).bold().build(), // Warn
            Style::builder().color(Color::Green).build(),         // Info
            Style::builder().color(Color::Cyan).build(),          // Debug
            Style::builder().color(Color::White).build(),         // Trace
        ])
    }
}

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub(crate) struct StyleCode {
    /// The start escape code for rendering style text.
    pub(crate) start: String,
    /// The end escape code for rendering style text.
    pub(crate) end: String,
}

#[derive(Clone, Eq, PartialEq, Hash, Debug)]
pub(crate) struct LevelStyleCodes([StyleCode; Level::count()]);

impl LevelStyleCodes {
    #[must_use]
    pub(crate) fn code(&self, level: Level) -> &StyleCode {
        &self.0[level as usize]
    }

    pub(crate) fn set_code<C>(&mut self, level: Level, code: C)
    where
        C: Into<StyleCode>,
    {
        self.0[level as usize] = code.into();
    }
}

impl From<Style> for StyleCode {
    fn from(style: Style) -> StyleCode {
        style.code()
    }
}

impl Default for LevelStyleCodes {
    fn default() -> LevelStyleCodes {
        LevelStyles::default().into()
    }
}