anstyle_stream/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<'s> Iterator for WinconBytesIter<'s> {
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        for param in params {
109            let mut state = State::Normal;
110            let mut r = None;
111            let mut g = None;
112            let mut is_bg = false;
113            for value in param {
114                match (state, *value) {
115                    (State::Normal, 0) => {
116                        style = anstyle::Style::default();
117                        break;
118                    }
119                    (State::Normal, 1) => {
120                        style = style.bold();
121                        break;
122                    }
123                    (State::Normal, 4) => {
124                        style = style.underline();
125                        break;
126                    }
127                    (State::Normal, 30..=37) => {
128                        let color = to_ansi_color(value - 30).unwrap();
129                        style = style.fg_color(Some(color.into()));
130                        break;
131                    }
132                    (State::Normal, 38) => {
133                        is_bg = false;
134                        state = State::PrepareCustomColor;
135                    }
136                    (State::Normal, 39) => {
137                        style = style.fg_color(None);
138                        break;
139                    }
140                    (State::Normal, 40..=47) => {
141                        let color = to_ansi_color(value - 40).unwrap();
142                        style = style.bg_color(Some(color.into()));
143                        break;
144                    }
145                    (State::Normal, 48) => {
146                        is_bg = true;
147                        state = State::PrepareCustomColor;
148                    }
149                    (State::Normal, 49) => {
150                        style = style.bg_color(None);
151                        break;
152                    }
153                    (State::Normal, 90..=97) => {
154                        let color = to_ansi_color(value - 90).unwrap().bright(true);
155                        style = style.fg_color(Some(color.into()));
156                        break;
157                    }
158                    (State::Normal, 100..=107) => {
159                        let color = to_ansi_color(value - 100).unwrap().bright(true);
160                        style = style.bg_color(Some(color.into()));
161                        break;
162                    }
163                    (State::PrepareCustomColor, 5) => {
164                        state = State::Ansi256;
165                    }
166                    (State::PrepareCustomColor, 2) => {
167                        state = State::Rgb;
168                        r = None;
169                        g = None;
170                    }
171                    (State::Ansi256, n) => {
172                        let color = anstyle::Ansi256Color(n as u8);
173                        if is_bg {
174                            style = style.bg_color(Some(color.into()));
175                        } else {
176                            style = style.fg_color(Some(color.into()));
177                        }
178                        break;
179                    }
180                    (State::Rgb, b) => match (r, g) {
181                        (None, _) => {
182                            r = Some(b);
183                        }
184                        (Some(_), None) => {
185                            g = Some(b);
186                        }
187                        (Some(r), Some(g)) => {
188                            let color = anstyle::RgbColor(r as u8, g as u8, b as u8);
189                            if is_bg {
190                                style = style.bg_color(Some(color.into()));
191                            } else {
192                                style = style.fg_color(Some(color.into()));
193                            }
194                            break;
195                        }
196                    },
197                    _ => {
198                        break;
199                    }
200                }
201            }
202        }
203
204        if style != self.style && !self.printable.is_empty() {
205            self.ready = Some(self.style);
206        }
207        self.style = style;
208    }
209}
210
211#[derive(Copy, Clone, PartialEq, Eq, Debug)]
212enum State {
213    Normal,
214    PrepareCustomColor,
215    Ansi256,
216    Rgb,
217}
218
219fn to_ansi_color(digit: u16) -> Option<anstyle::AnsiColor> {
220    match digit {
221        0 => Some(anstyle::AnsiColor::Black),
222        1 => Some(anstyle::AnsiColor::Red),
223        2 => Some(anstyle::AnsiColor::Green),
224        3 => Some(anstyle::AnsiColor::Yellow),
225        4 => Some(anstyle::AnsiColor::Blue),
226        5 => Some(anstyle::AnsiColor::Magenta),
227        6 => Some(anstyle::AnsiColor::Cyan),
228        7 => Some(anstyle::AnsiColor::White),
229        _ => None,
230    }
231}
232
233#[cfg(test)]
234mod test {
235    use super::*;
236    use owo_colors::OwoColorize as _;
237    use proptest::prelude::*;
238
239    #[track_caller]
240    fn verify(input: &str, expected: Vec<(anstyle::Style, &str)>) {
241        let expected = expected
242            .into_iter()
243            .map(|(style, value)| (style, value.to_owned()))
244            .collect::<Vec<_>>();
245        let mut state = WinconBytes::new();
246        let actual = state.extract_next(input.as_bytes()).collect::<Vec<_>>();
247        assert_eq!(expected, actual);
248    }
249
250    #[test]
251    fn start() {
252        let input = format!("{} world!", "Hello".green().on_red());
253        let expected = vec![
254            (
255                anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red),
256                "Hello",
257            ),
258            (anstyle::Style::default(), " world!"),
259        ];
260        verify(&input, expected);
261    }
262
263    #[test]
264    fn middle() {
265        let input = format!("Hello {}!", "world".green().on_red());
266        let expected = vec![
267            (anstyle::Style::default(), "Hello "),
268            (
269                anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red),
270                "world",
271            ),
272            (anstyle::Style::default(), "!"),
273        ];
274        verify(&input, expected);
275    }
276
277    #[test]
278    fn end() {
279        let input = format!("Hello {}", "world!".green().on_red());
280        let expected = vec![
281            (anstyle::Style::default(), "Hello "),
282            (
283                anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red),
284                "world!",
285            ),
286        ];
287        verify(&input, expected);
288    }
289
290    proptest! {
291        #[test]
292        #[cfg_attr(miri, ignore)]  // See https://github.com/AltSysrq/proptest/issues/253
293        fn wincon_no_escapes(s in "\\PC*") {
294            let expected = if s.is_empty() {
295                vec![]
296            } else {
297                vec![(anstyle::Style::default(), s.clone())]
298            };
299            let mut state = WinconBytes::new();
300            let actual = state.extract_next(s.as_bytes()).collect::<Vec<_>>();
301            assert_eq!(expected, actual);
302        }
303    }
304}