| 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 | 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)] |
| 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 | fn next(&mut self) -> Option<Self::Item> { |
| 39 | next_bytes(&mut self.bytes, self.parser, self.capture) |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | #[inline ] |
| 44 | fn 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)] |
| 68 | struct WinconCapture { |
| 69 | style: anstyle::Style, |
| 70 | printable: String, |
| 71 | ready: Option<anstyle::Style>, |
| 72 | } |
| 73 | |
| 74 | impl WinconCapture { |
| 75 | fn reset(&mut self) { |
| 76 | self.ready = None; |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | impl 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).expect("within 4-bit range" ); |
| 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).expect("within 4-bit range" ); |
| 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) |
| 184 | .expect("within 4-bit range" ) |
| 185 | .bright(true); |
| 186 | style = style.fg_color(Some(color.into())); |
| 187 | break; |
| 188 | } |
| 189 | (State::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 | (State::PrepareCustomColor, 5) => { |
| 197 | state = State::Ansi256; |
| 198 | } |
| 199 | (State::PrepareCustomColor, 2) => { |
| 200 | state = State::Rgb; |
| 201 | r = None; |
| 202 | g = None; |
| 203 | } |
| 204 | (State::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 | (State::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 | (State::Underline, 0) => { |
| 231 | style = |
| 232 | style.effects(style.get_effects().remove(anstyle::Effects::UNDERLINE)); |
| 233 | } |
| 234 | (State::Underline, 1) => { |
| 235 | // underline already set |
| 236 | } |
| 237 | (State::Underline, 2) => { |
| 238 | style = style |
| 239 | .effects(style.get_effects().remove(anstyle::Effects::UNDERLINE)) |
| 240 | | anstyle::Effects::DOUBLE_UNDERLINE; |
| 241 | } |
| 242 | (State::Underline, 3) => { |
| 243 | style = style |
| 244 | .effects(style.get_effects().remove(anstyle::Effects::UNDERLINE)) |
| 245 | | anstyle::Effects::CURLY_UNDERLINE; |
| 246 | } |
| 247 | (State::Underline, 4) => { |
| 248 | style = style |
| 249 | .effects(style.get_effects().remove(anstyle::Effects::UNDERLINE)) |
| 250 | | anstyle::Effects::DOTTED_UNDERLINE; |
| 251 | } |
| 252 | (State::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)] |
| 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 | fn 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)] |
| 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 | } |
| 388 | |