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(additional: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: u8 = if let Some((byte: &u8, remainder: &[u8])) = (*bytes).split_first() {
52 *bytes = remainder;
53 *byte
54 } else {
55 break;
56 };
57 parser.advance(performer:capture, byte);
58 }
59 if capture.printable.is_empty() {
60 return None;
61 }
62
63 let style: Style = capture.ready.unwrap_or(default: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 = State::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 (State::Normal, 0) => {
117 style = anstyle::Style::default();
118 break;
119 }
120 (State::Normal, 1) => {
121 style = style.bold();
122 break;
123 }
124 (State::Normal, 2) => {
125 style = style.dimmed();
126 break;
127 }
128 (State::Normal, 3) => {
129 style = style.italic();
130 break;
131 }
132 (State::Normal, 4) => {
133 style = style.underline();
134 state = State::Underline;
135 }
136 (State::Normal, 21) => {
137 style |= anstyle::Effects::DOUBLE_UNDERLINE;
138 break;
139 }
140 (State::Normal, 7) => {
141 style = style.invert();
142 break;
143 }
144 (State::Normal, 8) => {
145 style = style.hidden();
146 break;
147 }
148 (State::Normal, 9) => {
149 style = style.strikethrough();
150 break;
151 }
152 (State::Normal, 30..=37) => {
153 let color = to_ansi_color(value - 30).unwrap();
154 style = style.fg_color(Some(color.into()));
155 break;
156 }
157 (State::Normal, 38) => {
158 color_target = ColorTarget::Fg;
159 state = State::PrepareCustomColor;
160 }
161 (State::Normal, 39) => {
162 style = style.fg_color(None);
163 break;
164 }
165 (State::Normal, 40..=47) => {
166 let color = to_ansi_color(value - 40).unwrap();
167 style = style.bg_color(Some(color.into()));
168 break;
169 }
170 (State::Normal, 48) => {
171 color_target = ColorTarget::Bg;
172 state = State::PrepareCustomColor;
173 }
174 (State::Normal, 49) => {
175 style = style.bg_color(None);
176 break;
177 }
178 (State::Normal, 58) => {
179 color_target = ColorTarget::Underline;
180 state = State::PrepareCustomColor;
181 }
182 (State::Normal, 90..=97) => {
183 let color = to_ansi_color(value - 90).unwrap().bright(true);
184 style = style.fg_color(Some(color.into()));
185 break;
186 }
187 (State::Normal, 100..=107) => {
188 let color = to_ansi_color(value - 100).unwrap().bright(true);
189 style = style.bg_color(Some(color.into()));
190 break;
191 }
192 (State::PrepareCustomColor, 5) => {
193 state = State::Ansi256;
194 }
195 (State::PrepareCustomColor, 2) => {
196 state = State::Rgb;
197 r = None;
198 g = None;
199 }
200 (State::Ansi256, n) => {
201 let color = anstyle::Ansi256Color(n as u8);
202 style = match color_target {
203 ColorTarget::Fg => style.fg_color(Some(color.into())),
204 ColorTarget::Bg => style.bg_color(Some(color.into())),
205 ColorTarget::Underline => style.underline_color(Some(color.into())),
206 };
207 break;
208 }
209 (State::Rgb, b) => match (r, g) {
210 (None, _) => {
211 r = Some(b);
212 }
213 (Some(_), None) => {
214 g = Some(b);
215 }
216 (Some(r), Some(g)) => {
217 let color = anstyle::RgbColor(r as u8, g as u8, b as u8);
218 style = match color_target {
219 ColorTarget::Fg => style.fg_color(Some(color.into())),
220 ColorTarget::Bg => style.bg_color(Some(color.into())),
221 ColorTarget::Underline => style.underline_color(Some(color.into())),
222 };
223 break;
224 }
225 },
226 (State::Underline, 0) => {
227 style =
228 style.effects(style.get_effects().remove(anstyle::Effects::UNDERLINE));
229 }
230 (State::Underline, 1) => {
231 // underline already set
232 }
233 (State::Underline, 2) => {
234 style = style
235 .effects(style.get_effects().remove(anstyle::Effects::UNDERLINE))
236 | anstyle::Effects::DOUBLE_UNDERLINE;
237 }
238 (State::Underline, 3) => {
239 style = style
240 .effects(style.get_effects().remove(anstyle::Effects::UNDERLINE))
241 | anstyle::Effects::CURLY_UNDERLINE;
242 }
243 (State::Underline, 4) => {
244 style = style
245 .effects(style.get_effects().remove(anstyle::Effects::UNDERLINE))
246 | anstyle::Effects::DOTTED_UNDERLINE;
247 }
248 (State::Underline, 5) => {
249 style = style
250 .effects(style.get_effects().remove(anstyle::Effects::UNDERLINE))
251 | anstyle::Effects::DASHED_UNDERLINE;
252 }
253 _ => {
254 break;
255 }
256 }
257 }
258 }
259
260 if style != self.style && !self.printable.is_empty() {
261 self.ready = Some(self.style);
262 }
263 self.style = style;
264 }
265}
266
267#[derive(Copy, Clone, PartialEq, Eq, Debug)]
268enum State {
269 Normal,
270 PrepareCustomColor,
271 Ansi256,
272 Rgb,
273 Underline,
274}
275
276#[derive(Copy, Clone, PartialEq, Eq, Debug)]
277enum ColorTarget {
278 Fg,
279 Bg,
280 Underline,
281}
282
283fn to_ansi_color(digit: u16) -> Option<anstyle::AnsiColor> {
284 match digit {
285 0 => Some(anstyle::AnsiColor::Black),
286 1 => Some(anstyle::AnsiColor::Red),
287 2 => Some(anstyle::AnsiColor::Green),
288 3 => Some(anstyle::AnsiColor::Yellow),
289 4 => Some(anstyle::AnsiColor::Blue),
290 5 => Some(anstyle::AnsiColor::Magenta),
291 6 => Some(anstyle::AnsiColor::Cyan),
292 7 => Some(anstyle::AnsiColor::White),
293 _ => None,
294 }
295}
296
297#[cfg(test)]
298mod test {
299 use super::*;
300 use owo_colors::OwoColorize as _;
301 use proptest::prelude::*;
302
303 #[track_caller]
304 fn verify(input: &str, expected: Vec<(anstyle::Style, &str)>) {
305 let expected = expected
306 .into_iter()
307 .map(|(style, value)| (style, value.to_owned()))
308 .collect::<Vec<_>>();
309 let mut state = WinconBytes::new();
310 let actual = state.extract_next(input.as_bytes()).collect::<Vec<_>>();
311 assert_eq!(expected, actual, "{input:?}");
312 }
313
314 #[test]
315 fn start() {
316 let input = format!("{} world!", "Hello".green().on_red());
317 let expected = vec![
318 (
319 anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red),
320 "Hello",
321 ),
322 (anstyle::Style::default(), " world!"),
323 ];
324 verify(&input, expected);
325 }
326
327 #[test]
328 fn middle() {
329 let input = format!("Hello {}!", "world".green().on_red());
330 let expected = vec![
331 (anstyle::Style::default(), "Hello "),
332 (
333 anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red),
334 "world",
335 ),
336 (anstyle::Style::default(), "!"),
337 ];
338 verify(&input, expected);
339 }
340
341 #[test]
342 fn end() {
343 let input = format!("Hello {}", "world!".green().on_red());
344 let expected = vec![
345 (anstyle::Style::default(), "Hello "),
346 (
347 anstyle::AnsiColor::Green.on(anstyle::AnsiColor::Red),
348 "world!",
349 ),
350 ];
351 verify(&input, expected);
352 }
353
354 #[test]
355 fn ansi256_colors() {
356 // termcolor only supports "brights" via these
357 let input = format!(
358 "Hello {}!",
359 "world".color(owo_colors::XtermColors::UserBrightYellow)
360 );
361 let expected = vec![
362 (anstyle::Style::default(), "Hello "),
363 (anstyle::Ansi256Color(11).on_default(), "world"),
364 (anstyle::Style::default(), "!"),
365 ];
366 verify(&input, expected);
367 }
368
369 proptest! {
370 #[test]
371 #[cfg_attr(miri, ignore)] // See https://github.com/AltSysrq/proptest/issues/253
372 fn wincon_no_escapes(s in "\\PC*") {
373 let expected = if s.is_empty() {
374 vec![]
375 } else {
376 vec![(anstyle::Style::default(), s.clone())]
377 };
378 let mut state = WinconBytes::new();
379 let actual = state.extract_next(s.as_bytes()).collect::<Vec<_>>();
380 assert_eq!(expected, actual);
381 }
382 }
383}
384