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