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
//! Parse text with ANSI color codes.
//!
//! Needs the `ansi` feature to be enabled.
#![cfg(feature = "ansi")]
#![cfg_attr(feature = "doc-cfg", doc(cfg(feature = "ansi")))]

use crate::theme::{BaseColor, Color, Effect, Style};
use crate::utils::markup::{StyledIndexedSpan, StyledString};
use crate::utils::span::IndexedCow;

use ansi_parser::AnsiParser;
use unicode_width::UnicodeWidthStr;

/// Parses the given text with ANSI codes.
pub fn parse<S>(input: S) -> StyledString
where
    S: Into<String>,
{
    let input = input.into();

    let spans = Parser::new(&input).collect();

    StyledString::with_spans(input, spans)
}

/// Parses the given text with ANSI codes, using the given starting style.
///
/// Useful if you need to parse something in the middle of a large text.
///
/// Returns the parsed string, and the ending style.
pub fn parse_with_starting_style<S>(
    current_style: Style,
    input: S,
) -> (StyledString, Style)
where
    S: Into<String>,
{
    let input = input.into();

    let mut parser = Parser::with_starting_style(current_style, &input);
    let spans = (&mut parser).collect();
    let ending_style = parser.current_style();

    (StyledString::with_spans(input, spans), ending_style)
}

/// Parses the given string as text with ANSI color codes.
pub struct Parser<'a> {
    input: &'a str,
    current_style: Style,
    parser: ansi_parser::AnsiParseIterator<'a>,
}

fn parse_color(mut bytes: impl Iterator<Item = u8>) -> Option<Color> {
    Some(match bytes.next()? {
        5 => {
            let color = bytes.next()?;
            Color::from_256colors(color)
        }
        2 => {
            let r = bytes.next()?;
            let g = bytes.next()?;
            let b = bytes.next()?;
            Color::Rgb(r, g, b)
        }
        _ => {
            // ???
            return None;
        }
    })
}

impl<'a> Parser<'a> {
    /// Creates a new parser with the given input text.
    pub fn new(input: &'a str) -> Self {
        Self::with_starting_style(Style::default(), input)
    }

    /// Returns the current style.
    pub fn current_style(&self) -> Style {
        self.current_style
    }

    /// Creates a new parser with the given input text,
    /// using the given initial style.
    ///
    /// Useful if you need to parse something in the middle of a large text.
    pub fn with_starting_style(current_style: Style, input: &'a str) -> Self {
        Parser {
            input,
            current_style,
            parser: input.ansi_parse(),
        }
    }

    fn parse_sequence(&mut self, seq: &[u8]) -> Option<()> {
        let mut bytes = seq.iter().copied();
        loop {
            let byte = bytes.next()?;

            match byte {
                0 => self.current_style = Style::default(),
                1 => {
                    self.current_style.effects.insert(Effect::Bold);
                    self.current_style.effects.remove(Effect::Dim);
                }
                2 => {
                    self.current_style.effects.insert(Effect::Dim);
                    self.current_style.effects.remove(Effect::Bold);
                }
                22 => {
                    self.current_style.effects.remove(Effect::Dim);
                    self.current_style.effects.remove(Effect::Bold);
                }
                3 => {
                    self.current_style.effects.insert(Effect::Italic);
                }
                23 => {
                    self.current_style.effects.remove(Effect::Italic);
                }
                4 => {
                    self.current_style.effects.insert(Effect::Underline);
                }
                24 => {
                    self.current_style.effects.remove(Effect::Underline);
                }
                5 | 6 => {
                    // Technically 6 is rapid blink...
                    self.current_style.effects.insert(Effect::Blink);
                }
                25 => {
                    // Technically 6 is rapid blink...
                    self.current_style.effects.remove(Effect::Blink);
                }
                7 => {
                    self.current_style.effects.insert(Effect::Reverse);
                }
                27 => {
                    self.current_style.effects.remove(Effect::Reverse);
                }
                9 => {
                    self.current_style.effects.insert(Effect::Strikethrough);
                }
                29 => {
                    self.current_style.effects.remove(Effect::Strikethrough);
                }
                30..=37 => {
                    self.current_style.color.front =
                        BaseColor::from(byte - 30).dark().into();
                }
                38 => {
                    self.current_style.color.front =
                        parse_color(&mut bytes)?.into();
                }
                39 => {
                    self.current_style.color.front =
                        Color::TerminalDefault.into();
                }
                40..=47 => {
                    self.current_style.color.back =
                        BaseColor::from(byte - 40).dark().into();
                }
                48 => {
                    self.current_style.color.back =
                        parse_color(&mut bytes)?.into();
                }
                49 => {
                    self.current_style.color.back =
                        Color::TerminalDefault.into();
                }
                58 => {
                    // Set underline color.
                    // Not implemented, but consumes the rest
                    parse_color(&mut bytes)?;
                }
                90..=97 => {
                    self.current_style.color.front =
                        BaseColor::from(byte - 90).light().into();
                }
                100..=107 => {
                    self.current_style.color.back =
                        BaseColor::from(byte - 100).light().into();
                }
                _ => (),
            }
        }
    }
}

impl<'a> Iterator for Parser<'a> {
    type Item = StyledIndexedSpan;

    fn next(&mut self) -> Option<Self::Item> {
        loop {
            let next = self.parser.next()?;
            match next {
                ansi_parser::Output::TextBlock(text) => {
                    let width = text.width();
                    return Some(StyledIndexedSpan {
                        content: IndexedCow::from_str(text, self.input),
                        attr: self.current_style,
                        width,
                    });
                }
                ansi_parser::Output::Escape(sequence) => {
                    match sequence {
                        ansi_parser::AnsiSequence::SetGraphicsMode(bytes) => {
                            self.parse_sequence(&bytes);
                        }
                        // Nothing else to handle? Maybe SetMode/ResetMode?
                        _ => (),
                    }
                }
            }
        }
    }
}