anstream/adapter/
wincon.rs

1/// Incrementally convert to wincon calls for non-contiguous data
2#[derive(Default, Clone, Debug, PartialEq, Eq)]
3pub struct WinconBytes {
4    parser: anstyle_parse::Parser,
5    capture: WinconCapture,
6}
7
8impl WinconBytes {
9    /// Initial state
10    pub fn new() -> Self {
11        Default::default()
12    }
13
14    /// Strip the next segment of data
15    pub fn extract_next<'s>(&'s mut self, bytes: &'s [u8]) -> WinconBytesIter<'s> {
16        self.capture.reset();
17        self.capture.printable.reserve(bytes.len());
18        WinconBytesIter {
19            bytes,
20            parser: &mut self.parser,
21            capture: &mut self.capture,
22        }
23    }
24}
25
26/// See [`WinconBytes`]
27#[derive(Debug, PartialEq, Eq)]
28pub struct WinconBytesIter<'s> {
29    bytes: &'s [u8],
30    parser: &'s mut anstyle_parse::Parser,
31    capture: &'s mut WinconCapture,
32}
33
34impl Iterator for WinconBytesIter<'_> {
35    type Item = (anstyle::Style, String);
36
37    #[inline]
38    fn next(&mut self) -> Option<Self::Item> {
39        next_bytes(&mut self.bytes, self.parser, self.capture)
40    }
41}
42
43#[inline]
44fn next_bytes(
45    bytes: &mut &[u8],
46    parser: &mut anstyle_parse::Parser,
47    capture: &mut WinconCapture,
48) -> Option<(anstyle::Style, String)> {
49    capture.reset();
50    while capture.ready.is_none() {
51        let byte = if let Some((byte, remainder)) = (*bytes).split_first() {
52            *bytes = remainder;
53            *byte
54        } else {
55            break;
56        };
57        parser.advance(capture, byte);
58    }
59    if capture.printable.is_empty() {
60        return None;
61    }
62
63    let style = capture.ready.unwrap_or(capture.style);
64    Some((style, std::mem::take(&mut capture.printable)))
65}
66
67#[derive(Default, Clone, Debug, PartialEq, Eq)]
68struct WinconCapture {
69    style: anstyle::Style,
70    printable: String,
71    ready: Option<anstyle::Style>,
72}
73
74impl WinconCapture {
75    fn reset(&mut self) {
76        self.ready = None;
77    }
78}
79
80impl anstyle_parse::Perform for WinconCapture {
81    /// Draw a character to the screen and update states.
82    fn print(&mut self, c: char) {
83        self.printable.push(c);
84    }
85
86    /// Execute a C0 or C1 control function.
87    fn execute(&mut self, byte: u8) {
88        if byte.is_ascii_whitespace() {
89            self.printable.push(byte as char);
90        }
91    }
92
93    fn csi_dispatch(
94        &mut self,
95        params: &anstyle_parse::Params,
96        _intermediates: &[u8],
97        ignore: bool,
98        action: u8,
99    ) {
100        if ignore {
101            return;
102        }
103        if action != b'm' {
104            return;
105        }
106
107        let mut style = self.style;
108        // param/value differences are dependent on the escape code
109        let mut state = CsiState::Normal;
110        let mut r = None;
111        let mut g = None;
112        let mut color_target = ColorTarget::Fg;
113        for param in params {
114            for value in param {
115                match (state, *value) {
116                    (CsiState::Normal, 0) => {
117                        style = anstyle::Style::default();
118                        break;
119                    }
120                    (CsiState::Normal, 1) => {
121                        style = style.bold();
122                        break;
123                    }
124                    (CsiState::Normal, 2) => {
125                        style = style.dimmed();
126                        break;
127                    }
128                    (CsiState::Normal, 3) => {
129                        style = style.italic();
130                        break;
131                    }
132                    (CsiState::Normal, 4) => {
133                        style = style.underline();
134                        state = CsiState::Underline;
135                    }
136                    (CsiState::Normal, 21) => {
137                        style |= anstyle::Effects::DOUBLE_UNDERLINE;
138                        break;
139                    }
140                    (CsiState::Normal, 7) => {
141                        style = style.invert();
142                        break;
143                    }
144                    (CsiState::Normal, 8) => {
145                        style = style.hidden();
146                        break;
147                    }
148                    (CsiState::Normal, 9) => {
149                        style = style.strikethrough();
150                        break;
151                    }
152                    (CsiState::Normal, 30..=37) => {
153                        let color = to_ansi_color(value - 30).expect("within 4-bit range");
154                        style = style.fg_color(Some(color.into()));
155                        break;
156                    }
157                    (CsiState::Normal, 38) => {
158                        color_target = ColorTarget::Fg;
159                        state = CsiState::PrepareCustomColor;
160                    }
161                    (CsiState::Normal, 39) => {
162                        style = style.fg_color(None);
163                        break;
164                    }
165                    (CsiState::Normal, 40..=47) => {
166                        let color = to_ansi_color(value - 40).expect("within 4-bit range");
167                        style = style.bg_color(Some(color.into()));
168                        break;
169                    }
170                    (CsiState::Normal, 48) => {
171                        color_target = ColorTarget::Bg;
172                        state = CsiState::PrepareCustomColor;
173                    }
174                    (CsiState::Normal, 49) => {
175                        style = style.bg_color(None);
176                        break;
177                    }
178                    (CsiState::Normal, 58) => {
179                        color_target = ColorTarget::Underline;
180                        state = CsiState::PrepareCustomColor;
181                    }
182                    (CsiState::Normal, 90..=97) => {
183                        let color = to_ansi_color(value - 90)
184                            .expect("within 4-bit range")
185                            .bright(true);
186                        style = style.fg_color(Some(color.into()));
187                        break;
188                    }
189                    (CsiState::Normal, 100..=107) => {
190                        let color = to_ansi_color(value - 100)
191                            .expect("within 4-bit range")
192                            .bright(true);
193                        style = style.bg_color(Some(color.into()));
194                        break;
195                    }
196                    (CsiState::PrepareCustomColor, 5) => {
197                        state = CsiState::Ansi256;
198                    }
199                    (CsiState::PrepareCustomColor, 2) => {
200                        state = CsiState::Rgb;
201                        r = None;
202                        g = None;
203                    }
204                    (CsiState::Ansi256, n) => {
205                        let color = anstyle::Ansi256Color(n as u8);
206                        style = match color_target {
207                            ColorTarget::Fg => style.fg_color(Some(color.into())),
208                            ColorTarget::Bg => style.bg_color(Some(color.into())),
209                            ColorTarget::Underline => style.underline_color(Some(color.into())),
210                        };
211                        break;
212                    }
213                    (CsiState::Rgb, b) => match (r, g) {
214                        (None, _) => {
215                            r = Some(b);
216                        }
217                        (Some(_), None) => {
218                            g = Some(b);
219                        }
220                        (Some(r), Some(g)) => {
221                            let color = anstyle::RgbColor(r as u8, g as u8, b as u8);
222                            style = match color_target {
223                                ColorTarget::Fg => style.fg_color(Some(color.into())),
224                                ColorTarget::Bg => style.bg_color(Some(color.into())),
225                                ColorTarget::Underline => style.underline_color(Some(color.into())),
226                            };
227                            break;
228                        }
229                    },
230                    (CsiState::Underline, 0) => {
231                        style =
232                            style.effects(style.get_effects().remove(anstyle::Effects::UNDERLINE));
233                    }
234                    (CsiState::Underline, 1) => {
235                        // underline already set
236                    }
237                    (CsiState::Underline, 2) => {
238                        style = style
239                            .effects(style.get_effects().remove(anstyle::Effects::UNDERLINE))
240                            | anstyle::Effects::DOUBLE_UNDERLINE;
241                    }
242                    (CsiState::Underline, 3) => {
243                        style = style
244                            .effects(style.get_effects().remove(anstyle::Effects::UNDERLINE))
245                            | anstyle::Effects::CURLY_UNDERLINE;
246                    }
247                    (CsiState::Underline, 4) => {
248                        style = style
249                            .effects(style.get_effects().remove(anstyle::Effects::UNDERLINE))
250                            | anstyle::Effects::DOTTED_UNDERLINE;
251                    }
252                    (CsiState::Underline, 5) => {
253                        style = style
254                            .effects(style.get_effects().remove(anstyle::Effects::UNDERLINE))
255                            | anstyle::Effects::DASHED_UNDERLINE;
256                    }
257                    _ => {
258                        break;
259                    }
260                }
261            }
262        }
263
264        if style != self.style && !self.printable.is_empty() {
265            self.ready = Some(self.style);
266        }
267        self.style = style;
268    }
269}
270
271#[derive(Copy, Clone, PartialEq, Eq, Debug)]
272enum CsiState {
273    Normal,
274    PrepareCustomColor,
275    Ansi256,
276    Rgb,
277    Underline,
278}
279
280#[derive(Copy, Clone, PartialEq, Eq, Debug)]
281enum ColorTarget {
282    Fg,
283    Bg,
284    Underline,
285}
286
287fn to_ansi_color(digit: u16) -> Option<anstyle::AnsiColor> {
288    match digit {
289        0 => Some(anstyle::AnsiColor::Black),
290        1 => Some(anstyle::AnsiColor::Red),
291        2 => Some(anstyle::AnsiColor::Green),
292        3 => Some(anstyle::AnsiColor::Yellow),
293        4 => Some(anstyle::AnsiColor::Blue),
294        5 => Some(anstyle::AnsiColor::Magenta),
295        6 => Some(anstyle::AnsiColor::Cyan),
296        7 => Some(anstyle::AnsiColor::White),
297        _ => None,
298    }
299}
300
301#[cfg(test)]
302mod test {
303    use super::*;
304    use proptest::prelude::*;
305
306    #[track_caller]
307    fn verify(input: &str, expected: Vec<(anstyle::Style, &str)>) {
308        let expected = expected
309            .into_iter()
310            .map(|(style, value)| (style, value.to_owned()))
311            .collect::<Vec<_>>();
312        let mut state = WinconBytes::new();
313        let actual = state.extract_next(input.as_bytes()).collect::<Vec<_>>();
314        assert_eq!(expected, actual, "{input:?}");
315    }
316
317    #[test]
318    fn start() {
319        let green_on_red = anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red);
320        let input = format!("{green_on_red}Hello{green_on_red:#} world!");
321        let expected = vec![
322            (green_on_red, "Hello"),
323            (anstyle::Style::default(), " world!"),
324        ];
325        verify(&input, expected);
326    }
327
328    #[test]
329    fn middle() {
330        let green_on_red = anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red);
331        let input = format!("Hello {green_on_red}world{green_on_red:#}!");
332        let expected = vec![
333            (anstyle::Style::default(), "Hello "),
334            (green_on_red, "world"),
335            (anstyle::Style::default(), "!"),
336        ];
337        verify(&input, expected);
338    }
339
340    #[test]
341    fn end() {
342        let green_on_red = anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red);
343        let input = format!("Hello {green_on_red}world!{green_on_red:#}");
344        let expected = vec![
345            (anstyle::Style::default(), "Hello "),
346            (green_on_red, "world!"),
347        ];
348        verify(&input, expected);
349    }
350
351    #[test]
352    fn ansi256_colors() {
353        let ansi_11 = anstyle::Ansi256Color(11).on_default();
354        // termcolor only supports "brights" via these
355        let input = format!("Hello {ansi_11}world{ansi_11:#}!");
356        let expected = vec![
357            (anstyle::Style::default(), "Hello "),
358            (ansi_11, "world"),
359            (anstyle::Style::default(), "!"),
360        ];
361        verify(&input, expected);
362    }
363
364    proptest! {
365        #[test]
366        #[cfg_attr(miri, ignore)]  // See https://github.com/AltSysrq/proptest/issues/253
367        fn wincon_no_escapes(s in "\\PC*") {
368            let expected = if s.is_empty() {
369                vec![]
370            } else {
371                vec![(anstyle::Style::default(), s.clone())]
372            };
373            let mut state = WinconBytes::new();
374            let actual = state.extract_next(s.as_bytes()).collect::<Vec<_>>();
375            assert_eq!(expected, actual);
376        }
377    }
378}