television_previewers/ansi/
code.rs

1use tui::style::Color;
2
3/// This enum stores most types of ansi escape sequences
4///
5/// You can turn an escape sequence to this enum variant using
6/// `AnsiCode::from(code: u8)`
7/// This doesn't support all of them but does support most of them.
8
9#[derive(Debug, PartialEq, Clone)]
10#[non_exhaustive]
11pub enum AnsiCode {
12    /// Reset the terminal
13    Reset,
14    /// Set font to bold
15    Bold,
16    /// Set font to faint
17    Faint,
18    /// Set font to italic
19    Italic,
20    /// Set font to underline
21    Underline,
22    /// Set cursor to slowblink
23    SlowBlink,
24    /// Set cursor to rapidblink
25    RapidBlink,
26    /// Invert the colors
27    Reverse,
28    /// Conceal text
29    Conceal,
30    /// Display crossed out text
31    CrossedOut,
32    /// Choose primary font
33    PrimaryFont,
34    /// Choose alternate font
35    AlternateFont,
36    /// Choose alternate fonts 1-9
37    #[allow(dead_code)]
38    AlternateFonts(u8), // = 11..19, // from 11 to 19
39    /// Fraktur ? No clue
40    Fraktur,
41    /// Turn off bold
42    BoldOff,
43    /// Set text to normal
44    Normal,
45    /// Turn off Italic
46    NotItalic,
47    /// Turn off underline
48    UnderlineOff,
49    /// Turn off blinking
50    BlinkOff,
51    // 26 ?
52    /// Don't invert colors
53    InvertOff,
54    /// Reveal text
55    Reveal,
56    /// Turn off Crossedout text
57    CrossedOutOff,
58    /// Set foreground color (4-bit)
59    ForegroundColor(Color), //, 31..37//Issue 60553 https://github.com/rust-lang/rust/issues/60553
60    /// Set foreground color (8-bit and 24-bit)
61    SetForegroundColor,
62    /// Default foreground color
63    DefaultForegroundColor,
64    /// Set background color (4-bit)
65    BackgroundColor(Color), // 41..47
66    /// Set background color (8-bit and 24-bit)
67    SetBackgroundColor,
68    /// Default background color
69    DefaultBackgroundColor, // 49
70    /// Other / non supported escape codes
71    Code(Vec<u8>),
72}
73
74impl From<u8> for AnsiCode {
75    fn from(code: u8) -> Self {
76        match code {
77            0 => AnsiCode::Reset,
78            1 => AnsiCode::Bold,
79            2 => AnsiCode::Faint,
80            3 => AnsiCode::Italic,
81            4 => AnsiCode::Underline,
82            5 => AnsiCode::SlowBlink,
83            6 => AnsiCode::RapidBlink,
84            7 => AnsiCode::Reverse,
85            8 => AnsiCode::Conceal,
86            9 => AnsiCode::CrossedOut,
87            10 => AnsiCode::PrimaryFont,
88            11 => AnsiCode::AlternateFont,
89            // AnsiCode::// AlternateFont = 11..19, // from 11 to 19
90            20 => AnsiCode::Fraktur,
91            21 => AnsiCode::BoldOff,
92            22 => AnsiCode::Normal,
93            23 => AnsiCode::NotItalic,
94            24 => AnsiCode::UnderlineOff,
95            25 => AnsiCode::BlinkOff,
96            // 26 ?
97            27 => AnsiCode::InvertOff,
98            28 => AnsiCode::Reveal,
99            29 => AnsiCode::CrossedOutOff,
100            30 => AnsiCode::ForegroundColor(Color::Black),
101            31 => AnsiCode::ForegroundColor(Color::Red),
102            32 => AnsiCode::ForegroundColor(Color::Green),
103            33 => AnsiCode::ForegroundColor(Color::Yellow),
104            34 => AnsiCode::ForegroundColor(Color::Blue),
105            35 => AnsiCode::ForegroundColor(Color::Magenta),
106            36 => AnsiCode::ForegroundColor(Color::Cyan),
107            37 => AnsiCode::ForegroundColor(Color::Gray),
108            38 => AnsiCode::SetForegroundColor,
109            39 => AnsiCode::DefaultForegroundColor,
110            40 => AnsiCode::BackgroundColor(Color::Black),
111            41 => AnsiCode::BackgroundColor(Color::Red),
112            42 => AnsiCode::BackgroundColor(Color::Green),
113            43 => AnsiCode::BackgroundColor(Color::Yellow),
114            44 => AnsiCode::BackgroundColor(Color::Blue),
115            45 => AnsiCode::BackgroundColor(Color::Magenta),
116            46 => AnsiCode::BackgroundColor(Color::Cyan),
117            47 => AnsiCode::BackgroundColor(Color::Gray),
118            48 => AnsiCode::SetBackgroundColor,
119            49 => AnsiCode::DefaultBackgroundColor,
120            90 => AnsiCode::ForegroundColor(Color::DarkGray),
121            91 => AnsiCode::ForegroundColor(Color::LightRed),
122            92 => AnsiCode::ForegroundColor(Color::LightGreen),
123            93 => AnsiCode::ForegroundColor(Color::LightYellow),
124            94 => AnsiCode::ForegroundColor(Color::LightBlue),
125            95 => AnsiCode::ForegroundColor(Color::LightMagenta),
126            96 => AnsiCode::ForegroundColor(Color::LightCyan),
127            #[allow(clippy::match_same_arms)]
128            97 => AnsiCode::ForegroundColor(Color::White),
129            100 => AnsiCode::BackgroundColor(Color::DarkGray),
130            101 => AnsiCode::BackgroundColor(Color::LightRed),
131            102 => AnsiCode::BackgroundColor(Color::LightGreen),
132            103 => AnsiCode::BackgroundColor(Color::LightYellow),
133            104 => AnsiCode::BackgroundColor(Color::LightBlue),
134            105 => AnsiCode::BackgroundColor(Color::LightMagenta),
135            106 => AnsiCode::BackgroundColor(Color::LightCyan),
136            107 => AnsiCode::ForegroundColor(Color::White),
137            code => AnsiCode::Code(vec![code]),
138        }
139    }
140}