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