lscolors 0.10.0

Colorize paths using the LS_COLORS environment variable
Documentation
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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
//! A module for ANSI styles
//!
//! For more information, see
//! [ANSI escape code (Wikipedia)](https://en.wikipedia.org/wiki/ANSI_escape_code).
use std::collections::VecDeque;

#[cfg(ansi_term)]
use ansi_term;

#[cfg(crossterm)]
use crossterm;

/// A `Color` can be one of the pre-defined ANSI colors (`Red`, `Green`, ..),
/// a 8-bit ANSI color (`Fixed(u8)`) or a 24-bit color (`RGB(u8, u8, u8)`).
#[derive(Debug, Clone, PartialEq)]
pub enum Color {
    Black,
    Red,
    Green,
    Yellow,
    Blue,
    Magenta,
    Cyan,
    White,
    BrightBlack,
    BrightRed,
    BrightGreen,
    BrightYellow,
    BrightBlue,
    BrightMagenta,
    BrightCyan,
    BrightWhite,
    Fixed(u8),
    RGB(u8, u8, u8),
}

impl Color {
    /// Convert to a `ansi_term::Color` (if the `ansi_term` feature is enabled).
    #[cfg(feature = "ansi_term")]
    pub fn to_ansi_term_color(&self) -> ansi_term::Color {
        match self {
            Color::RGB(r, g, b) => ansi_term::Color::RGB(*r, *g, *b),
            Color::Fixed(n) => ansi_term::Color::Fixed(*n),
            Color::Black => ansi_term::Color::Black,
            Color::Red => ansi_term::Color::Red,
            Color::Green => ansi_term::Color::Green,
            Color::Yellow => ansi_term::Color::Yellow,
            Color::Blue => ansi_term::Color::Blue,
            Color::Magenta => ansi_term::Color::Purple,
            Color::Cyan => ansi_term::Color::Cyan,
            Color::White => ansi_term::Color::White,

            // Below items are a rough translations to 256 colors as
            // we do not have bright varients available on ansi-term
            Color::BrightBlack => ansi_term::Color::Fixed(8),
            Color::BrightRed => ansi_term::Color::Fixed(9),
            Color::BrightGreen => ansi_term::Color::Fixed(10),
            Color::BrightYellow => ansi_term::Color::Fixed(11),
            Color::BrightBlue => ansi_term::Color::Fixed(12),
            Color::BrightMagenta => ansi_term::Color::Fixed(13),
            Color::BrightCyan => ansi_term::Color::Fixed(14),
            Color::BrightWhite => ansi_term::Color::Fixed(15),
        }
    }

    /// Convert to a `crossterm::style::Color` (if the `crossterm` feature is enabled).
    #[cfg(feature = "crossterm")]
    pub fn to_crossterm_color(&self) -> crossterm::style::Color {
        match self {
            Color::RGB(r, g, b) => crossterm::style::Color::Rgb {
                r: *r,
                g: *g,
                b: *b,
            },
            Color::Fixed(n) => crossterm::style::Color::AnsiValue(*n),
            Color::Black => crossterm::style::Color::Black,
            Color::Red => crossterm::style::Color::DarkRed,
            Color::Green => crossterm::style::Color::DarkGreen,
            Color::Yellow => crossterm::style::Color::DarkYellow,
            Color::Blue => crossterm::style::Color::DarkBlue,
            Color::Magenta => crossterm::style::Color::DarkMagenta,
            Color::Cyan => crossterm::style::Color::DarkCyan,
            Color::White => crossterm::style::Color::Grey,
            Color::BrightBlack => crossterm::style::Color::DarkGrey,
            Color::BrightRed => crossterm::style::Color::Red,
            Color::BrightGreen => crossterm::style::Color::Green,
            Color::BrightYellow => crossterm::style::Color::Yellow,
            Color::BrightBlue => crossterm::style::Color::Blue,
            Color::BrightMagenta => crossterm::style::Color::Magenta,
            Color::BrightCyan => crossterm::style::Color::Cyan,
            Color::BrightWhite => crossterm::style::Color::White,
        }
    }
}

/// Font-style attributes.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct FontStyle {
    pub bold: bool,
    pub dimmed: bool, // a.k.a. faint
    pub italic: bool,
    pub underline: bool,
    pub slow_blink: bool,
    pub rapid_blink: bool,
    pub reverse: bool,       // a.k.a. inverse or reverse video
    pub hidden: bool,        // a.k.a. conceal
    pub strikethrough: bool, // a.k.a. crossed-out
}

impl FontStyle {
    pub fn bold() -> Self {
        FontStyle {
            bold: true,
            ..Default::default()
        }
    }

    pub fn dimmed() -> Self {
        FontStyle {
            dimmed: true,
            ..Default::default()
        }
    }

    pub fn italic() -> Self {
        FontStyle {
            italic: true,
            ..Default::default()
        }
    }

    pub fn underline() -> Self {
        FontStyle {
            underline: true,
            ..Default::default()
        }
    }

    pub fn slow_blink() -> Self {
        FontStyle {
            slow_blink: true,
            ..Default::default()
        }
    }

    pub fn rapid_blink() -> Self {
        FontStyle {
            rapid_blink: true,
            ..Default::default()
        }
    }

    pub fn reverse() -> Self {
        FontStyle {
            reverse: true,
            ..Default::default()
        }
    }

    pub fn hidden() -> Self {
        FontStyle {
            hidden: true,
            ..Default::default()
        }
    }

    pub fn strikethrough() -> Self {
        FontStyle {
            strikethrough: true,
            ..Default::default()
        }
    }

    /// Convert to `crossterm::style::Attributes` (if the `crossterm` feature is enabled).
    #[cfg(feature = "crossterm")]
    pub fn to_crossterm_attributes(&self) -> crossterm::style::Attributes {
        let mut attributes = crossterm::style::Attributes::default();
        if self.bold {
            attributes.set(crossterm::style::Attribute::Bold);
        }
        if self.dimmed {
            attributes.set(crossterm::style::Attribute::Dim);
        }
        if self.italic {
            attributes.set(crossterm::style::Attribute::Italic);
        }
        if self.underline {
            attributes.set(crossterm::style::Attribute::Underlined);
        }
        if self.slow_blink {
            attributes.set(crossterm::style::Attribute::SlowBlink);
        }
        if self.rapid_blink {
            attributes.set(crossterm::style::Attribute::RapidBlink);
        }
        if self.reverse {
            attributes.set(crossterm::style::Attribute::Reverse);
        }
        if self.hidden {
            attributes.set(crossterm::style::Attribute::Hidden);
        }
        if self.strikethrough {
            attributes.set(crossterm::style::Attribute::CrossedOut);
        }
        attributes
    }
}

/// A foreground color, background color and font-style.
#[derive(Debug, Clone, PartialEq, Default)]
pub struct Style {
    pub foreground: Option<Color>,
    pub background: Option<Color>,
    pub font_style: FontStyle,
}

impl Style {
    /// Parse ANSI escape sequences like `38;2;255;0;100;1;4` (pink, bold, underlined).
    pub fn from_ansi_sequence(code: &str) -> Option<Style> {
        if code.is_empty() || code == "0" || code == "00" {
            return None;
        }

        let mut parts: VecDeque<u8> = code
            .split(';')
            .map(|c| c.parse::<u8>().ok())
            .collect::<Option<_>>()?;

        let mut font_style = FontStyle::default();
        let mut foreground = None;
        let mut background = None;

        loop {
            match parts.pop_front() {
                Some(0) => font_style = FontStyle::default(),
                Some(1) => font_style.bold = true,
                Some(2) => font_style.dimmed = true,
                Some(3) => font_style.italic = true,
                Some(4) => font_style.underline = true,
                Some(5) => font_style.slow_blink = true,
                Some(6) => font_style.rapid_blink = true,
                Some(7) => font_style.reverse = true,
                Some(8) => font_style.hidden = true,
                Some(9) => font_style.strikethrough = true,
                Some(22) => {
                    font_style.bold = false;
                    font_style.dimmed = false;
                }
                Some(23) => {
                    font_style.italic = false;
                }
                Some(24) => {
                    font_style.underline = false;
                }
                Some(25) => {
                    font_style.slow_blink = false;
                    font_style.rapid_blink = false;
                }
                Some(27) => {
                    font_style.reverse = false;
                }
                Some(28) => {
                    font_style.hidden = false;
                }
                Some(29) => {
                    font_style.strikethrough = false;
                }
                Some(30) => foreground = Some(Color::Black),
                Some(31) => foreground = Some(Color::Red),
                Some(32) => foreground = Some(Color::Green),
                Some(33) => foreground = Some(Color::Yellow),
                Some(34) => foreground = Some(Color::Blue),
                Some(35) => foreground = Some(Color::Magenta),
                Some(36) => foreground = Some(Color::Cyan),
                Some(37) => foreground = Some(Color::White),
                Some(38) => match (parts.pop_front(), parts.pop_front()) {
                    (Some(5), Some(color)) => foreground = Some(Color::Fixed(color)),
                    (Some(2), Some(red)) => match (parts.pop_front(), parts.pop_front()) {
                        (Some(green), Some(blue)) => {
                            foreground = Some(Color::RGB(red, green, blue))
                        }
                        _ => {
                            break;
                        }
                    },
                    _ => {
                        break;
                    }
                },
                Some(39) => foreground = None,
                Some(40) => background = Some(Color::Black),
                Some(41) => background = Some(Color::Red),
                Some(42) => background = Some(Color::Green),
                Some(43) => background = Some(Color::Yellow),
                Some(44) => background = Some(Color::Blue),
                Some(45) => background = Some(Color::Magenta),
                Some(46) => background = Some(Color::Cyan),
                Some(47) => background = Some(Color::White),
                Some(48) => match (parts.pop_front(), parts.pop_front()) {
                    (Some(5), Some(color)) => background = Some(Color::Fixed(color)),
                    (Some(2), Some(red)) => match (parts.pop_front(), parts.pop_front()) {
                        (Some(green), Some(blue)) => {
                            background = Some(Color::RGB(red, green, blue))
                        }
                        _ => {
                            break;
                        }
                    },
                    _ => {
                        break;
                    }
                },
                Some(49) => background = None,
                Some(90) => foreground = Some(Color::BrightBlack),
                Some(91) => foreground = Some(Color::BrightRed),
                Some(92) => foreground = Some(Color::BrightGreen),
                Some(93) => foreground = Some(Color::BrightYellow),
                Some(94) => foreground = Some(Color::BrightBlue),
                Some(95) => foreground = Some(Color::BrightMagenta),
                Some(96) => foreground = Some(Color::BrightCyan),
                Some(97) => foreground = Some(Color::BrightWhite),
                Some(100) => background = Some(Color::BrightBlack),
                Some(101) => background = Some(Color::BrightRed),
                Some(102) => background = Some(Color::BrightGreen),
                Some(103) => background = Some(Color::BrightYellow),
                Some(104) => background = Some(Color::BrightBlue),
                Some(105) => background = Some(Color::BrightMagenta),
                Some(106) => background = Some(Color::BrightCyan),
                Some(107) => background = Some(Color::BrightWhite),
                Some(_) => {
                    continue;
                }
                None => {
                    break;
                }
            }
        }

        Some(Style {
            foreground,
            background,
            font_style,
        })
    }

    /// Convert to a `ansi_term::Style` (if the `ansi_term` feature is enabled).
    #[cfg(feature = "ansi_term")]
    pub fn to_ansi_term_style(&self) -> ansi_term::Style {
        ansi_term::Style {
            foreground: self.foreground.as_ref().map(Color::to_ansi_term_color),
            background: self.background.as_ref().map(Color::to_ansi_term_color),
            is_bold: self.font_style.bold,
            is_dimmed: self.font_style.dimmed,
            is_italic: self.font_style.italic,
            is_underline: self.font_style.underline,
            is_blink: self.font_style.rapid_blink || self.font_style.slow_blink,
            is_reverse: self.font_style.reverse,
            is_hidden: self.font_style.hidden,
            is_strikethrough: self.font_style.strikethrough,
        }
    }

    /// Convert to a `crossterm::style::ContentStyle` (if the `crossterm` feature is enabled).
    #[cfg(feature = "crossterm")]
    pub fn to_crossterm_style(&self) -> crossterm::style::ContentStyle {
        crossterm::style::ContentStyle {
            foreground_color: self.foreground.as_ref().map(Color::to_crossterm_color),
            background_color: self.background.as_ref().map(Color::to_crossterm_color),
            attributes: self.font_style.to_crossterm_attributes(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::{Color, FontStyle, Style};

    fn assert_style(
        code: &str,
        foreground: Option<Color>,
        background: Option<Color>,
        font_style: FontStyle,
    ) {
        let style = Style::from_ansi_sequence(code).unwrap();
        assert_eq!(foreground, style.foreground);
        assert_eq!(background, style.background);
        assert_eq!(font_style, style.font_style);
    }

    #[test]
    fn parse_simple() {
        assert_style("31", Some(Color::Red), None, FontStyle::default());
        assert_style("47", None, Some(Color::White), FontStyle::default());
        assert_style("91", Some(Color::BrightRed), None, FontStyle::default());
        assert_style("107", None, Some(Color::BrightWhite), FontStyle::default());
        assert_style(
            "32;40",
            Some(Color::Green),
            Some(Color::Black),
            FontStyle::default(),
        );
    }

    #[test]
    fn parse_reject() {
        assert_eq!(None, Style::from_ansi_sequence("a"));
        assert_eq!(None, Style::from_ansi_sequence("1;"));
        assert_eq!(None, Style::from_ansi_sequence("33; 42"));
    }

    #[test]
    fn parse_font_style() {
        assert_style("00;31", Some(Color::Red), None, FontStyle::default());
        assert_style("03;34", Some(Color::Blue), None, FontStyle::italic());
        assert_style("06;34", Some(Color::Blue), None, FontStyle::rapid_blink());
        assert_style("01;36", Some(Color::Cyan), None, FontStyle::bold());
        let italic_and_bold = FontStyle {
            bold: true,
            italic: true,
            ..Default::default()
        };
        assert_style("01;03", None, None, italic_and_bold);
    }

    #[test]
    fn ignore_unsupported_styles() {
        let style = Style::from_ansi_sequence("14;31").unwrap();
        assert_eq!(Some(Color::Red), style.foreground);
    }

    #[test]
    fn support_reset_of_styles() {
        assert_style("01;31", Some(Color::Red), None, FontStyle::bold());
        assert_style("01;31;22", Some(Color::Red), None, FontStyle::default());
    }

    #[test]
    fn parse_font_style_backwards() {
        assert_style("34;03", Some(Color::Blue), None, FontStyle::italic());
        assert_style("36;01", Some(Color::Cyan), None, FontStyle::bold());
        assert_style("31;00", Some(Color::Red), None, FontStyle::default());
    }

    #[test]
    fn parse_8_bit_colors() {
        assert_style(
            "38;5;115",
            Some(Color::Fixed(115)),
            None,
            FontStyle::default(),
        );
        assert_style(
            "00;38;5;115",
            Some(Color::Fixed(115)),
            None,
            FontStyle::default(),
        );
        assert_style(
            "01;38;5;119",
            Some(Color::Fixed(119)),
            None,
            FontStyle::bold(),
        );
        assert_style(
            "38;5;119;01",
            Some(Color::Fixed(119)),
            None,
            FontStyle::bold(),
        );
    }

    #[test]
    fn parse_24_bit_colors() {
        assert_style(
            "38;2;115;3;100",
            Some(Color::RGB(115, 3, 100)),
            None,
            FontStyle::default(),
        );
        assert_style(
            "38;2;115;3;100;3",
            Some(Color::RGB(115, 3, 100)),
            None,
            FontStyle::italic(),
        );
        assert_style(
            "48;2;100;200;0;1;38;2;0;10;20",
            Some(Color::RGB(0, 10, 20)),
            Some(Color::RGB(100, 200, 0)),
            FontStyle::bold(),
        );
    }
}