| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
#[cfg(windows)] |
| 23 |
pub mod windows; |
| 24 |
|
| 25 |
#[cfg(all(windows, feature = "windows-legacy"))] |
| 26 |
use windows::legacy; |
| 27 |
#[cfg(windows)] |
| 28 |
use windows::InputReaderMode; |
| 29 |
|
| 30 |
use std::{collections::VecDeque, num::NonZeroU16, str}; |
| 31 |
|
| 32 |
#[cfg(doc)] |
| 33 |
use crate::EventReader; |
| 34 |
|
| 35 |
use crate::{ |
| 36 |
escape::{ |
| 37 |
self, |
| 38 |
csi::{self, Csi, KittyKeyboardFlags, ThemeMode}, |
| 39 |
dcs, osc, |
| 40 |
}, |
| 41 |
event::{ |
| 42 |
KeyCode, KeyEvent, KeyEventKind, KeyEventState, MediaKeyCode, ModifierKeyCode, Modifiers, |
| 43 |
MouseButton, MouseEvent, MouseEventKind, |
| 44 |
}, |
| 45 |
style, Event, |
| 46 |
}; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
#[derive(Debug)] |
| 64 |
pub struct Parser { |
| 65 |
buffer: Vec<u8>, |
| 66 |
|
| 67 |
events: VecDeque<Event>, |
| 68 |
#[cfg(windows)] |
| 69 |
mode: InputReaderMode, |
| 70 |
#[cfg(all(windows, feature = "windows-legacy"))] |
| 71 |
surrogate_buffer: Option<u16>, |
| 72 |
#[cfg(all(windows, feature = "windows-legacy"))] |
| 73 |
mouse_buttons_pressed: legacy::MouseButtonsPressed, |
| 74 |
} |
| 75 |
|
| 76 |
impl Default for Parser { |
| 77 |
fn default() -> Self { |
| 78 |
Self { |
| 79 |
buffer: Vec::with_capacity(256), |
| 80 |
events: VecDeque::with_capacity(32), |
| 81 |
#[cfg(windows)] |
| 82 |
mode: InputReaderMode::Vte, |
| 83 |
#[cfg(all(windows, feature = "windows-legacy"))] |
| 84 |
surrogate_buffer: None, |
| 85 |
#[cfg(all(windows, feature = "windows-legacy"))] |
| 86 |
mouse_buttons_pressed: legacy::MouseButtonsPressed::default(), |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
impl Parser { |
| 92 |
|
| 93 |
|
| 94 |
#[cfg(windows)] |
| 95 |
pub(crate) fn with_mode(mode: InputReaderMode) -> Self { |
| 96 |
Self { |
| 97 |
mode, |
| 98 |
..Default::default() |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
pub fn pop(&mut self) -> Option<Event> { |
| 104 |
self.events.pop_front() |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
pub fn parse(&mut self, bytes: &[u8], maybe_more: bool) { |
| 113 |
self.buffer.extend_from_slice(bytes); |
| 114 |
self.process_bytes(maybe_more); |
| 115 |
} |
| 116 |
|
| 117 |
fn process_bytes(&mut self, maybe_more: bool) { |
| 118 |
let mut start = 0; |
| 119 |
for n in 0..self.buffer.len() { |
| 120 |
let end = n + 1; |
| 121 |
match parse_event( |
| 122 |
&self.buffer[start..end], |
| 123 |
maybe_more || end < self.buffer.len(), |
| 124 |
) { |
| 125 |
Ok(Some(event)) => { |
| 126 |
self.events.push_back(event); |
| 127 |
start = end; |
| 128 |
} |
| 129 |
Ok(None) => continue, |
| 130 |
Err(_) => start = end, |
| 131 |
} |
| 132 |
} |
| 133 |
self.advance(start); |
| 134 |
} |
| 135 |
|
| 136 |
fn advance(&mut self, len: usize) { |
| 137 |
if len == 0 { |
| 138 |
return; |
| 139 |
} |
| 140 |
let remain = self.buffer.len() - len; |
| 141 |
self.buffer.rotate_left(len); |
| 142 |
self.buffer.truncate(remain); |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
#[derive(Debug)] |
| 147 |
struct MalformedSequenceError; |
| 148 |
|
| 149 |
|
| 150 |
impl From<str::Utf8Error> for MalformedSequenceError { |
| 151 |
fn from(_: str::Utf8Error) -> Self { |
| 152 |
Self |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
type Result<T> = std::result::Result<T, MalformedSequenceError>; |
| 157 |
|
| 158 |
macro_rules! bail { |
| 159 |
() => { |
| 160 |
return Err(MalformedSequenceError) |
| 161 |
}; |
| 162 |
} |
| 163 |
|
| 164 |
fn parse_event(buffer: &[u8], maybe_more: bool) -> Result<Option<Event>> { |
| 165 |
if buffer.is_empty() { |
| 166 |
return Ok(None); |
| 167 |
} |
| 168 |
|
| 169 |
match buffer[0] { |
| 170 |
b'\x1B' => { |
| 171 |
if buffer.len() == 1 { |
| 172 |
if maybe_more { |
| 173 |
|
| 174 |
Ok(None) |
| 175 |
} else { |
| 176 |
Ok(Some(Event::Key(KeyCode::Escape.into()))) |
| 177 |
} |
| 178 |
} else { |
| 179 |
match buffer[1] { |
| 180 |
b'O' => { |
| 181 |
if buffer.len() == 2 { |
| 182 |
Ok(None) |
| 183 |
} else { |
| 184 |
match buffer[2] { |
| 185 |
b'D' => Ok(Some(Event::Key(KeyCode::Left.into()))), |
| 186 |
b'C' => Ok(Some(Event::Key(KeyCode::Right.into()))), |
| 187 |
b'A' => Ok(Some(Event::Key(KeyCode::Up.into()))), |
| 188 |
b'B' => Ok(Some(Event::Key(KeyCode::Down.into()))), |
| 189 |
b'H' => Ok(Some(Event::Key(KeyCode::Home.into()))), |
| 190 |
b'F' => Ok(Some(Event::Key(KeyCode::End.into()))), |
| 191 |
|
| 192 |
val @ b'P'..=b'S' => { |
| 193 |
Ok(Some(Event::Key(KeyCode::Function(1 + val - b'P').into()))) |
| 194 |
} |
| 195 |
_ => bail!(), |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
b'[' => parse_csi(buffer), |
| 200 |
b']' => parse_osc(buffer), |
| 201 |
b'P' => parse_dcs(buffer), |
| 202 |
b'\x1B' => Ok(Some(Event::Key(KeyCode::Escape.into()))), |
| 203 |
_ => parse_event(&buffer[1..], maybe_more).map(|event_option| { |
| 204 |
event_option.map(|event| { |
| 205 |
if let Event::Key(key_event) = event { |
| 206 |
let mut alt_key_event = key_event; |
| 207 |
alt_key_event.modifiers |= Modifiers::ALT; |
| 208 |
Event::Key(alt_key_event) |
| 209 |
} else { |
| 210 |
event |
| 211 |
} |
| 212 |
}) |
| 213 |
}), |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
b'\r' => Ok(Some(Event::Key(KeyCode::Enter.into()))), |
| 218 |
b'\t' => Ok(Some(Event::Key(KeyCode::Tab.into()))), |
| 219 |
b'\x7F' => Ok(Some(Event::Key(KeyCode::Backspace.into()))), |
| 220 |
b'\0' => Ok(Some(Event::Key(KeyEvent::new( |
| 221 |
KeyCode::Char(' '), |
| 222 |
Modifiers::CONTROL, |
| 223 |
)))), |
| 224 |
c @ b'\x01'..=b'\x1A' => Ok(Some(Event::Key(KeyEvent::new( |
| 225 |
KeyCode::Char((c - 0x1 + b'a') as char), |
| 226 |
Modifiers::CONTROL, |
| 227 |
)))), |
| 228 |
c @ b'\x1C'..=b'\x1F' => Ok(Some(Event::Key(KeyEvent::new( |
| 229 |
KeyCode::Char((c - 0x1C + b'4') as char), |
| 230 |
Modifiers::CONTROL, |
| 231 |
)))), |
| 232 |
_ => parse_utf8_char(buffer).map(|maybe_char| { |
| 233 |
maybe_char.map(|ch| { |
| 234 |
let modifiers = if ch.is_uppercase() { |
| 235 |
Modifiers::SHIFT |
| 236 |
} else { |
| 237 |
Modifiers::NONE |
| 238 |
}; |
| 239 |
Event::Key(KeyEvent::new(KeyCode::Char(ch), modifiers)) |
| 240 |
}) |
| 241 |
}), |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
fn parse_utf8_char(buffer: &[u8]) -> Result<Option<char>> { |
| 246 |
assert!(!buffer.is_empty()); |
| 247 |
match str::from_utf8(buffer) { |
| 248 |
Ok(s) => Ok(Some(s.chars().next().unwrap())), |
| 249 |
Err(_) => { |
| 250 |
|
| 251 |
|
| 252 |
let required_bytes = match buffer[0] { |
| 253 |
|
| 254 |
(0x00..=0x7F) => 1, |
| 255 |
(0xC0..=0xDF) => 2, |
| 256 |
(0xE0..=0xEF) => 3, |
| 257 |
(0xF0..=0xF7) => 4, |
| 258 |
(0x80..=0xBF) | (0xF8..=0xFF) => bail!(), |
| 259 |
}; |
| 260 |
if required_bytes > 1 && buffer.len() > 1 { |
| 261 |
for byte in &buffer[1..] { |
| 262 |
if byte & !0b0011_1111 != 0b1000_0000 { |
| 263 |
bail!() |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
if buffer.len() < required_bytes { |
| 268 |
Ok(None) |
| 269 |
} else { |
| 270 |
bail!() |
| 271 |
} |
| 272 |
} |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
fn parse_csi(buffer: &[u8]) -> Result<Option<Event>> { |
| 277 |
assert!(buffer.starts_with(b"\x1B[")); |
| 278 |
if buffer.len() == 2 { |
| 279 |
return Ok(None); |
| 280 |
} |
| 281 |
let maybe_event = match buffer[2] { |
| 282 |
b'[' => match buffer.get(3) { |
| 283 |
None => None, |
| 284 |
Some(b @ b'A'..=b'E') => Some(Event::Key(KeyCode::Function(1 + b - b'A').into())), |
| 285 |
Some(_) => bail!(), |
| 286 |
}, |
| 287 |
b'D' => Some(Event::Key(KeyCode::Left.into())), |
| 288 |
b'C' => Some(Event::Key(KeyCode::Right.into())), |
| 289 |
b'A' => Some(Event::Key(KeyCode::Up.into())), |
| 290 |
b'B' => Some(Event::Key(KeyCode::Down.into())), |
| 291 |
b'H' => Some(Event::Key(KeyCode::Home.into())), |
| 292 |
b'F' => Some(Event::Key(KeyCode::End.into())), |
| 293 |
b'Z' => Some(Event::Key(KeyEvent { |
| 294 |
code: KeyCode::BackTab, |
| 295 |
modifiers: Modifiers::SHIFT, |
| 296 |
kind: KeyEventKind::Press, |
| 297 |
state: KeyEventState::NONE, |
| 298 |
})), |
| 299 |
b'M' => return parse_csi_normal_mouse(buffer), |
| 300 |
b'<' => return parse_csi_sgr_mouse(buffer), |
| 301 |
b'I' => Some(Event::FocusIn), |
| 302 |
b'O' => Some(Event::FocusOut), |
| 303 |
b';' => return parse_csi_modifier_key_code(buffer), |
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
b'P' => Some(Event::Key(KeyCode::Function(1).into())), |
| 309 |
b'Q' => Some(Event::Key(KeyCode::Function(2).into())), |
| 310 |
b'S' => Some(Event::Key(KeyCode::Function(4).into())), |
| 311 |
b'?' => match buffer[buffer.len() - 1] { |
| 312 |
b'u' => return parse_csi_keyboard_enhancement_flags(buffer), |
| 313 |
b'c' => return parse_csi_primary_device_attributes(buffer), |
| 314 |
b'n' => return parse_csi_theme_mode(buffer), |
| 315 |
b'y' => return parse_csi_mode(buffer), |
| 316 |
_ => None, |
| 317 |
}, |
| 318 |
b'>' => match buffer[buffer.len() - 2..buffer.len()] { |
| 319 |
[b' ', b'q'] => return parse_csi_cursor_shape_query_response(buffer), |
| 320 |
_ => None, |
| 321 |
}, |
| 322 |
b'0'..=b'9' => { |
| 323 |
|
| 324 |
if buffer.len() == 3 { |
| 325 |
None |
| 326 |
} else { |
| 327 |
|
| 328 |
|
| 329 |
let last_byte = buffer[buffer.len() - 1]; |
| 330 |
if !(64..=126).contains(&last_byte) { |
| 331 |
None |
| 332 |
} else { |
| 333 |
if buffer.starts_with(b"\x1B[200~") { |
| 334 |
return parse_csi_bracketed_paste(buffer); |
| 335 |
} |
| 336 |
match last_byte { |
| 337 |
b'M' => return parse_csi_rxvt_mouse(buffer), |
| 338 |
b'~' => return parse_csi_special_key_code(buffer), |
| 339 |
b'u' => return parse_csi_u_encoded_key_code(buffer), |
| 340 |
b'R' => return parse_csi_cursor_position(buffer), |
| 341 |
_ => return parse_csi_modifier_key_code(buffer), |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
} |
| 346 |
_ => bail!(), |
| 347 |
}; |
| 348 |
Ok(maybe_event) |
| 349 |
} |
| 350 |
|
| 351 |
fn parse_osc(buffer: &[u8]) -> Result<Option<Event>> { |
| 352 |
assert!(buffer.starts_with(b"\x1B]")); |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
let Some(buffer) = buffer |
| 357 |
.strip_suffix(escape::ST.as_bytes()) |
| 358 |
.or_else(|| buffer.strip_suffix(escape::BEL.as_bytes())) |
| 359 |
else { |
| 360 |
return Ok(None); |
| 361 |
}; |
| 362 |
let s = str::from_utf8(&buffer[2..buffer.len()])?; |
| 363 |
let mut split = s.split(';'); |
| 364 |
let index = next_parsed::<u8>(&mut split)?; |
| 365 |
let Some(color_number) = osc::DynamicColorNumber::from_index(index) else { |
| 366 |
bail!() |
| 367 |
}; |
| 368 |
let Some(color_or_query) = split.next() else { |
| 369 |
bail!() |
| 370 |
}; |
| 371 |
let response = match color_or_query { |
| 372 |
"?" => osc::ColorOrQuery::Query, |
| 373 |
_ => osc::ColorOrQuery::Color(color_or_query.parse().map_err(|_| MalformedSequenceError)?), |
| 374 |
}; |
| 375 |
|
| 376 |
Ok(Some(Event::Osc(osc::Osc::ChangeDynamicColors( |
| 377 |
color_number, |
| 378 |
vec![response], |
| 379 |
)))) |
| 380 |
} |
| 381 |
|
| 382 |
fn next_parsed<T>(iter: &mut dyn Iterator<Item = &str>) -> Result<T> |
| 383 |
where |
| 384 |
T: str::FromStr, |
| 385 |
{ |
| 386 |
iter.next() |
| 387 |
.ok_or(MalformedSequenceError)? |
| 388 |
.parse::<T>() |
| 389 |
.map_err(|_| MalformedSequenceError) |
| 390 |
} |
| 391 |
|
| 392 |
fn modifier_and_kind_parsed(iter: &mut dyn Iterator<Item = &str>) -> Result<(u8, u8)> { |
| 393 |
let mut sub_split = iter.next().ok_or(MalformedSequenceError)?.split(':'); |
| 394 |
|
| 395 |
let modifier_mask = next_parsed::<u8>(&mut sub_split)?; |
| 396 |
|
| 397 |
if let Ok(kind_code) = next_parsed::<u8>(&mut sub_split) { |
| 398 |
Ok((modifier_mask, kind_code)) |
| 399 |
} else { |
| 400 |
Ok((modifier_mask, 1)) |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
fn parse_csi_u_encoded_key_code(buffer: &[u8]) -> Result<Option<Event>> { |
| 405 |
assert!(buffer.starts_with(b"\x1B")); |
| 406 |
assert!(buffer.ends_with(b"u")); |
| 407 |
|
| 408 |
er |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
let s = str::from_utf8(&buffer[2..buffer.len() - 1])?; |
| 413 |
let mut split = s.split(';'); |
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
let mut codepoints = split.next().ok_or(MalformedSequenceError)?.split(':'); |
| 425 |
|
| 426 |
let codepoint = codepoints |
| 427 |
.next() |
| 428 |
.ok_or(MalformedSequenceError)? |
| 429 |
.parse::<u32>() |
| 430 |
.map_err(|_| MalformedSequenceError)?; |
| 431 |
|
| 432 |
let (mut modifiers, kind, state_from_modifiers) = |
| 433 |
if let Ok((modifier_mask, kind_code)) = modifier_and_kind_parsed(&mut split) { |
| 434 |
( |
| 435 |
parse_modifiers(modifier_mask), |
| 436 |
parse_key_event_kind(kind_code), |
| 437 |
parse_modifiers_to_state(modifier_mask), |
| 438 |
) |
| 439 |
} else { |
| 440 |
(Modifiers::NONE, KeyEventKind::Press, KeyEventState::NONE) |
| 441 |
}; |
| 442 |
|
| 443 |
let (mut code, state_from_keycode) = { |
| 444 |
if let Some((special_key_code, state)) = translate_functional_key_code(codepoint) { |
| 445 |
(special_key_code, state) |
| 446 |
} else if let Some(c) = char::from_u32(codepoint) { |
| 447 |
( |
| 448 |
match c { |
| 449 |
'\x1B' => KeyCode::Escape, |
| 450 |
'\r' => KeyCode::Enter, |
| 451 |
|
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
'\t' => { |
| 459 |
if modifiers.contains(Modifiers::SHIFT) { |
| 460 |
KeyCode::BackTab |
| 461 |
} else { |
| 462 |
KeyCode::Tab |
| 463 |
} |
| 464 |
} |
| 465 |
'\x7F' => KeyCode::Backspace, |
| 466 |
_ => KeyCode::Char(c), |
| 467 |
}, |
| 468 |
KeyEventState::empty(), |
| 469 |
) |
| 470 |
} else { |
| 471 |
bail!(); |
| 472 |
} |
| 473 |
}; |
| 474 |
|
| 475 |
if let KeyCode::Modifier(modifier_keycode) = code { |
| 476 |
match modifier_keycode { |
| 477 |
ModifierKeyCode::LeftAlt | ModifierKeyCode::RightAlt => { |
| 478 |
modifiers.set(Modifiers::ALT, true) |
| 479 |
} |
| 480 |
ModifierKeyCode::LeftControl | ModifierKeyCode::RightControl => { |
| 481 |
modifiers.set(Modifiers::CONTROL, true) |
| 482 |
} |
| 483 |
ModifierKeyCode::LeftShift | ModifierKeyCode::RightShift => { |
| 484 |
modifiers.set(Modifiers::SHIFT, true) |
| 485 |
} |
| 486 |
ModifierKeyCode::LeftSuper | ModifierKeyCode::RightSuper => { |
| 487 |
modifiers.set(Modifiers::SUPER, true) |
| 488 |
} |
| 489 |
ModifierKeyCode::LeftHyper | ModifierKeyCode::RightHyper => { |
| 490 |
modifiers.set(Modifiers::HYPER, true) |
| 491 |
} |
| 492 |
ModifierKeyCode::LeftMeta | ModifierKeyCode::RightMeta => { |
| 493 |
modifiers.set(Modifiers::META, true) |
| 494 |
} |
| 495 |
_ => {} |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
if modifiers.contains(Modifiers::SHIFT) { |
| 504 |
if let Some(shifted_c) = codepoints |
| 505 |
.next() |
| 506 |
.and_then(|codepoint| codepoint.parse::<u32>().ok()) |
| 507 |
.and_then(char::from_u32) |
| 508 |
{ |
| 509 |
code = KeyCode::Char(shifted_c); |
| 510 |
modifiers.set(Modifiers::SHIFT, false); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
let event = Event::Key(KeyEvent { |
| 515 |
code, |
| 516 |
modifiers, |
| 517 |
kind, |
| 518 |
state: state_from_keycode | state_from_modifiers, |
| 519 |
}); |
| 520 |
|
| 521 |
Ok(Some(event)) |
| 522 |
} |
| 523 |
|
| 524 |
fn parse_modifiers(mask: u8) -> Modifiers { |
| 525 |
let modifier_mask = mask.saturating_sub(1); |
| 526 |
let mut modifiers = Modifiers::empty(); |
| 527 |
if modifier_mask & 1 != 0 { |
| 528 |
modifiers |= Modifiers::SHIFT; |
| 529 |
} |
| 530 |
if modifier_mask & 2 != 0 { |
| 531 |
modifiers |= Modifiers::ALT; |
| 532 |
} |
| 533 |
if modifier_mask & 4 != 0 { |
| 534 |
modifiers |= Modifiers::CONTROL; |
| 535 |
} |
| 536 |
if modifier_mask & 8 != 0 { |
| 537 |
modifiers |= Modifiers::SUPER; |
| 538 |
} |
| 539 |
if modifier_mask & 16 != 0 { |
| 540 |
modifiers |= Modifiers::HYPER; |
| 541 |
} |
| 542 |
if modifier_mask & 32 != 0 { |
| 543 |
modifiers |= Modifiers::META; |
| 544 |
} |
| 545 |
modifiers |
| 546 |
} |
| 547 |
|
| 548 |
fn parse_modifiers_to_state(mask: u8) -> KeyEventState { |
| 549 |
let modifier_mask = mask.saturating_sub(1); |
| 550 |
let mut state = KeyEventState::empty(); |
| 551 |
if modifier_mask & 64 != 0 { |
| 552 |
state |= KeyEventState::CAPS_LOCK; |
| 553 |
} |
| 554 |
if modifier_mask & 128 != 0 { |
| 555 |
state |= KeyEventState::NUM_LOCK; |
| 556 |
} |
| 557 |
state |
| 558 |
} |
| 559 |
|
| 560 |
fn parse_key_event_kind(kind: u8) -> KeyEventKind { |
| 561 |
match kind { |
| 562 |
1 => KeyEventKind::Press, |
| 563 |
2 => KeyEventKind::Repeat, |
| 564 |
3 => KeyEventKind::Release, |
| 565 |
_ => KeyEventKind::Press, |
| 566 |
} |
| 567 |
} |
| 568 |
|
| 569 |
fn parse_csi_modifier_key_code(buffer: &[u8]) -> Result<Option<Event>> { |
| 570 |
assert!(buffer.starts_with(b"\x1B[")); |
| 571 |
let s = str::from_utf8(&buffer[2..buffer.len() - 1])?; |
| 572 |
let mut split = s.split(';'); |
| 573 |
|
| 574 |
split.next(); |
| 575 |
|
| 576 |
let (modifiers, kind) = |
| 577 |
if let Ok((modifier_mask, kind_code)) = modifier_and_kind_parsed(&mut split) { |
| 578 |
( |
| 579 |
parse_modifiers(modifier_mask), |
| 580 |
parse_key_event_kind(kind_code), |
| 581 |
) |
| 582 |
} else if buffer.len() > 3 { |
| 583 |
( |
| 584 |
parse_modifiers( |
| 585 |
(buffer[buffer.len() - 2] as char) |
| 586 |
.to_digit(10) |
| 587 |
.ok_or(MalformedSequenceError)? as u8, |
| 588 |
), |
| 589 |
KeyEventKind::Press, |
| 590 |
) |
| 591 |
} else { |
| 592 |
(Modifiers::NONE, KeyEventKind::Press) |
| 593 |
}; |
| 594 |
let key = buffer[buffer.len() - 1]; |
| 595 |
|
| 596 |
let code = match key { |
| 597 |
b'A' => KeyCode::Up, |
| 598 |
b'B' => KeyCode::Down, |
| 599 |
b'C' => KeyCode::Right, |
| 600 |
b'D' => KeyCode::Left, |
| 601 |
b'F' => KeyCode::End, |
| 602 |
b'H' => KeyCode::Home, |
| 603 |
b'P' => KeyCode::Function(1), |
| 604 |
b'Q' => KeyCode::Function(2), |
| 605 |
b'R' => KeyCode::Function(3), |
| 606 |
b'S' => KeyCode::Function(4), |
| 607 |
_ => bail!(), |
| 608 |
}; |
| 609 |
|
| 610 |
let event = Event::Key(KeyEvent { |
| 611 |
code, |
| 612 |
modifiers, |
| 613 |
kind, |
| 614 |
state: KeyEventState::NONE, |
| 615 |
}); |
| 616 |
|
| 617 |
Ok(Some(event)) |
| 618 |
} |
| 619 |
|
| 620 |
fn parse_csi_special_key_code(buffer: &[u8]) -> Result<Option<Event>> { |
| 621 |
assert!(buffer.starts_with(b"\x1B[")); |
| 622 |
assert!(buffer.ends_with(b"~")); |
| 623 |
|
| 624 |
let s = str::from_utf8(&buffer[2..buffer.len() - 1])?; |
| 625 |
let mut split = s.split(';'); |
| 626 |
|
| 627 |
|
| 628 |
let first = next_parsed::<u8>(&mut split)?; |
| 629 |
|
| 630 |
let (modifiers, kind, state) = |
| 631 |
if let Ok((modifier_mask, kind_code)) = modifier_and_kind_parsed(&mut split) { |
| 632 |
( |
| 633 |
parse_modifiers(modifier_mask), |
| 634 |
parse_key_event_kind(kind_code), |
| 635 |
parse_modifiers_to_state(modifier_mask), |
| 636 |
) |
| 637 |
} else { |
| 638 |
(Modifiers::NONE, KeyEventKind::Press, KeyEventState::NONE) |
| 639 |
}; |
| 640 |
|
| 641 |
let code = match first { |
| 642 |
1 | 7 => KeyCode::Home, |
| 643 |
2 => KeyCode::Insert, |
| 644 |
3 => KeyCode::Delete, |
| 645 |
4 | 8 => KeyCode::End, |
| 646 |
5 => KeyCode::PageUp, |
| 647 |
6 => KeyCode::PageDown, |
| 648 |
v @ 11..=15 => KeyCode::Function(v - 10), |
| 649 |
v @ 17..=21 => KeyCode::Function(v - 11), |
| 650 |
v @ 23..=26 => KeyCode::Function(v - 12), |
| 651 |
v @ 28..=29 => KeyCode::Function(v - 15), |
| 652 |
v @ 31..=34 => KeyCode::Function(v - 17), |
| 653 |
_ => bail!(), |
| 654 |
}; |
| 655 |
|
| 656 |
let event = Event::Key(KeyEvent { |
| 657 |
code, |
| 658 |
modifiers, |
| 659 |
kind, |
| 660 |
state, |
| 661 |
}); |
| 662 |
|
| 663 |
Ok(Some(event)) |
| 664 |
} |
| 665 |
|
| 666 |
fn translate_functional_key_code(codepoint: u32) -> Option<(KeyCode, KeyEventState)> { |
| 667 |
if let Some(keycode) = match codepoint { |
| 668 |
57399 => Some(KeyCode::Char('0')), |
| 669 |
57400 => Some(KeyCode::Char('1')), |
| 670 |
57401 => Some(KeyCode::Char('2')), |
| 671 |
57402 => Some(KeyCode::Char('3')), |
| 672 |
57403 => Some(KeyCode::Char('4')), |
| 673 |
57404 => Some(KeyCode::Char('5')), |
| 674 |
57405 => Some(KeyCode::Char('6')), |
| 675 |
57406 => Some(KeyCode::Char('7')), |
| 676 |
57407 => Some(KeyCode::Char('8')), |
| 677 |
57408 => Some(KeyCode::Char('9')), |
| 678 |
57409 => Some(KeyCode::Char('.')), |
| 679 |
57410 => Some(KeyCode::Char('/')), |
| 680 |
57411 => Some(KeyCode::Char('*')), |
| 681 |
57412 => Some(KeyCode::Char('-')), |
| 682 |
57413 => Some(KeyCode::Char('+')), |
| 683 |
57414 => Some(KeyCode::Enter), |
| 684 |
57415 => Some(KeyCode::Char('=')), |
| 685 |
57416 => Some(KeyCode::Char(',')), |
| 686 |
57417 => Some(KeyCode::Left), |
| 687 |
57418 => Some(KeyCode::Right), |
| 688 |
57419 => Some(KeyCode::Up), |
| 689 |
57420 => Some(KeyCode::Down), |
| 690 |
57421 => Some(KeyCode::PageUp), |
| 691 |
57422 => Some(KeyCode::PageDown), |
| 692 |
57423 => Some(KeyCode::Home), |
| 693 |
57424 => Some(KeyCode::End), |
| 694 |
57425 => Some(KeyCode::Insert), |
| 695 |
57426 => Some(KeyCode::Delete), |
| 696 |
57427 => Some(KeyCode::KeypadBegin), |
| 697 |
_ => None, |
| 698 |
} { |
| 699 |
return Some((keycode, KeyEventState::KEYPAD)); |
| 700 |
} |
| 701 |
|
| 702 |
if let Some(keycode) = match codepoint { |
| 703 |
57358 => Some(KeyCode::CapsLock), |
| 704 |
57359 => Some(KeyCode::ScrollLock), |
| 705 |
57360 => Some(KeyCode::NumLock), |
| 706 |
57361 => Some(KeyCode::PrintScreen), |
| 707 |
57362 => Some(KeyCode::Pause), |
| 708 |
57363 => Some(KeyCode::Menu), |
| 709 |
57376 => Some(KeyCode::Function(13)), |
| 710 |
57377 => Some(KeyCode::Function(14)), |
| 711 |
57378 => Some(KeyCode::Function(15)), |
| 712 |
57379 => Some(KeyCode::Function(16)), |
| 713 |
57380 => Some(KeyCode::Function(17)), |
| 714 |
57381 => Some(KeyCode::Function(18)), |
| 715 |
57382 => Some(KeyCode::Function(19)), |
| 716 |
57383 => Some(KeyCode::Function(20)), |
| 717 |
57384 => Some(KeyCode::Function(21)), |
| 718 |
57385 => Some(KeyCode::Function(22)), |
| 719 |
57386 => Some(KeyCode::Function(23)), |
| 720 |
57387 => Some(KeyCode::Function(24)), |
| 721 |
57388 => Some(KeyCode::Function(25)), |
| 722 |
57389 => Some(KeyCode::Function(26)), |
| 723 |
57390 => Some(KeyCode::Function(27)), |
| 724 |
57391 => Some(KeyCode::Function(28)), |
| 725 |
57392 => Some(KeyCode::Function(29)), |
| 726 |
57393 => Some(KeyCode::Function(30)), |
| 727 |
57394 => Some(KeyCode::Function(31)), |
| 728 |
57395 => Some(KeyCode::Function(32)), |
| 729 |
57396 => Some(KeyCode::Function(33)), |
| 730 |
57397 => Some(KeyCode::Function(34)), |
| 731 |
57398 => Some(KeyCode::Function(35)), |
| 732 |
57428 => Some(KeyCode::Media(MediaKeyCode::Play)), |
| 733 |
57429 => Some(KeyCode::Media(MediaKeyCode::Pause)), |
| 734 |
57430 => Some(KeyCode::Media(MediaKeyCode::PlayPause)), |
| 735 |
57431 => Some(KeyCode::Media(MediaKeyCode::Reverse)), |
| 736 |
57432 => Some(KeyCode::Media(MediaKeyCode::Stop)), |
| 737 |
57433 => Some(KeyCode::Media(MediaKeyCode::FastForward)), |
| 738 |
57434 => Some(KeyCode::Media(MediaKeyCode::Rewind)), |
| 739 |
57435 => Some(KeyCode::Media(MediaKeyCode::TrackNext)), |
| 740 |
57436 => Some(KeyCode::Media(MediaKeyCode::TrackPrevious)), |
| 741 |
57437 => Some(KeyCode::Media(MediaKeyCode::Record)), |
| 742 |
57438 => Some(KeyCode::Media(MediaKeyCode::LowerVolume)), |
| 743 |
57439 => Some(KeyCode::Media(MediaKeyCode::RaiseVolume)), |
| 744 |
57440 => Some(KeyCode::Media(MediaKeyCode::MuteVolume)), |
| 745 |
57441 => Some(KeyCode::Modifier(ModifierKeyCode::LeftShift)), |
| 746 |
57442 => Some(KeyCode::Modifier(ModifierKeyCode::LeftControl)), |
| 747 |
57443 => Some(KeyCode::Modifier(ModifierKeyCode::LeftAlt)), |
| 748 |
57444 => Some(KeyCode::Modifier(ModifierKeyCode::LeftSuper)), |
| 749 |
57445 => Some(KeyCode::Modifier(ModifierKeyCode::LeftHyper)), |
| 750 |
57446 => Some(KeyCode::Modifier(ModifierKeyCode::LeftMeta)), |
| 751 |
57447 => Some(KeyCode::Modifier(ModifierKeyCode::RightShift)), |
| 752 |
57448 => Some(KeyCode::Modifier(ModifierKeyCode::RightControl)), |
| 753 |
57449 => Some(KeyCode::Modifier(ModifierKeyCode::RightAlt)), |
| 754 |
57450 => Some(KeyCode::Modifier(ModifierKeyCode::RightSuper)), |
| 755 |
57451 => Some(KeyCode::Modifier(ModifierKeyCode::RightHyper)), |
| 756 |
57452 => Some(KeyCode::Modifier(ModifierKeyCode::RightMeta)), |
| 757 |
57453 => Some(KeyCode::Modifier(ModifierKeyCode::IsoLevel3Shift)), |
| 758 |
57454 => Some(KeyCode::Modifier(ModifierKeyCode::IsoLevel5Shift)), |
| 759 |
_ => None, |
| 760 |
} { |
| 761 |
return Some((keycode, KeyEventState::empty())); |
| 762 |
} |
| 763 |
|
| 764 |
None |
| 765 |
} |
| 766 |
|
| 767 |
fn parse_csi_rxvt_mouse(buffer: &[u8]) -> Result<Option<Event>> { |
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
assert!(buffer.starts_with(b"\x1B[")); |
| 772 |
assert!(buffer.ends_with(b"M")); |
| 773 |
|
| 774 |
let s = str::from_utf8(&buffer[2..buffer.len() - 1])?; |
| 775 |
let mut split = s.split(';'); |
| 776 |
|
| 777 |
let cb = next_parsed::<u8>(&mut split)? |
| 778 |
.checked_sub(32) |
| 779 |
.ok_or(MalformedSequenceError)?; |
| 780 |
let (kind, modifiers) = parse_cb(cb)?; |
| 781 |
|
| 782 |
let cx = next_parsed::<u16>(&mut split)? - 1; |
| 783 |
let cy = next_parsed::<u16>(&mut split)? - 1; |
| 784 |
|
| 785 |
Ok(Some(Event::Mouse(MouseEvent { |
| 786 |
kind, |
| 787 |
column: cx, |
| 788 |
row: cy, |
| 789 |
modifiers, |
| 790 |
}))) |
| 791 |
} |
| 792 |
|
| 793 |
fn parse_csi_normal_mouse(buffer: &[u8]) -> Result<Option<Event>> { |
| 794 |
|
| 795 |
|
| 796 |
assert!(buffer.starts_with(b"\x1B[M")); |
| 797 |
|
| 798 |
if buffer.len() < 6 { |
| 799 |
return Ok(None); |
| 800 |
} |
| 801 |
|
| 802 |
let cb = buffer[3].checked_sub(32).ok_or(MalformedSequenceError)?; |
| 803 |
let (kind, modifiers) = parse_cb(cb)?; |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
let cx = u16::from(buffer[4].saturating_sub(33)); |
| 810 |
let cy = u16::from(buffer[5].saturating_sub(33)); |
| 811 |
|
| 812 |
Ok(Some(Event::Mouse(MouseEvent { |
| 813 |
kind, |
| 814 |
column: cx, |
| 815 |
row: cy, |
| 816 |
modifiers, |
| 817 |
}))) |
| 818 |
} |
| 819 |
|
| 820 |
fn parse_csi_sgr_mouse(buffer: &[u8]) -> Result<Option<Event>> { |
| 821 |
|
| 822 |
|
| 823 |
assert!(buffer.starts_with(b"\x1B[<")); |
| 824 |
|
| 825 |
if !buffer.ends_with(b"m") && !buffer.ends_with(b"M") { |
| 826 |
return Ok(None); |
| 827 |
} |
| 828 |
|
| 829 |
let s = str::from_utf8(&buffer[3..buffer.len() - 1])?; |
| 830 |
let mut split = s.split(';'); |
| 831 |
|
| 832 |
let cb = next_parsed::<u8>(&mut split)?; |
| 833 |
let (kind, modifiers) = parse_cb(cb)?; |
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
let cx = next_parsed::<u16>(&mut split)? - 1; |
| 839 |
let cy = next_parsed::<u16>(&mut split)? - 1; |
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
|
| 846 |
|
| 847 |
let kind = if buffer.last() == Some(&b'm') { |
| 848 |
match kind { |
| 849 |
MouseEventKind::Down(button) => MouseEventKind::Up(button), |
| 850 |
other => other, |
| 851 |
} |
| 852 |
} else { |
| 853 |
kind |
| 854 |
}; |
| 855 |
|
| 856 |
Ok(Some(Event::Mouse(MouseEvent { |
| 857 |
kind, |
| 858 |
column: cx, |
| 859 |
row: cy, |
| 860 |
modifiers, |
| 861 |
}))) |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
|
| 867 |
|
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
fn parse_cb(cb: u8) -> Result<(MouseEventKind, Modifiers)> { |
| 878 |
let button_number = (cb & 0b0000_0011) | ((cb & 0b1100_0000) >> 4); |
| 879 |
let dragging = cb & 0b0010_0000 == 0b0010_0000; |
| 880 |
|
| 881 |
let kind = match (button_number, dragging) { |
| 882 |
(0, false) => MouseEventKind::Down(MouseButton::Left), |
| 883 |
(1, false) => MouseEventKind::Down(MouseButton::Middle), |
| 884 |
(2, false) => MouseEventKind::Down(MouseButton::Right), |
| 885 |
(0, true) => MouseEventKind::Drag(MouseButton::Left), |
| 886 |
(1, true) => MouseEventKind::Drag(MouseButton::Middle), |
| 887 |
(2, true) => MouseEventKind::Drag(MouseButton::Right), |
| 888 |
(3, false) => MouseEventKind::Up(MouseButton::Left), |
| 889 |
(3, true) | (4, true) | (5, true) => MouseEventKind::Moved, |
| 890 |
(4, false) => MouseEventKind::ScrollUp, |
| 891 |
(5, false) => MouseEventKind::ScrollDown, |
| 892 |
(6, false) => MouseEventKind::ScrollLeft, |
| 893 |
(7, false) => MouseEventKind::ScrollRight, |
| 894 |
|
| 895 |
_ => bail!(), |
| 896 |
}; |
| 897 |
|
| 898 |
let mut modifiers = Modifiers::empty(); |
| 899 |
|
| 900 |
if cb & 0b0000_0100 == 0b0000_0100 { |
| 901 |
modifiers |= Modifiers::SHIFT; |
| 902 |
} |
| 903 |
if cb & 0b0000_1000 == 0b0000_1000 { |
| 904 |
modifiers |= Modifiers::ALT; |
| 905 |
} |
| 906 |
if cb & 0b0001_0000 == 0b0001_0000 { |
| 907 |
modifiers |= Modifiers::CONTROL; |
| 908 |
} |
| 909 |
|
| 910 |
Ok((kind, modifiers)) |
| 911 |
} |
| 912 |
|
| 913 |
fn parse_csi_bracketed_paste(buffer: &[u8]) -> Result<Option<Event>> { |
| 914 |
|
| 915 |
let buffer = buffer |
| 916 |
.strip_prefix(b"\x1b[200~") |
| 917 |
.expect("asserted by calling functions"); |
| 918 |
|
| 919 |
if let Some(contents) = buffer.strip_suffix(b"\x1b[201~") { |
| 920 |
let paste = String::from_utf8_lossy(contents).to_string(); |
| 921 |
Ok(Some(Event::Paste(paste))) |
| 922 |
} else { |
| 923 |
Ok(None) |
| 924 |
} |
| 925 |
} |
| 926 |
|
| 927 |
fn parse_csi_cursor_position(buffer: &[u8]) -> Result<Option<Event>> { |
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
assert!(buffer.starts_with(b"\x1B[")); |
| 932 |
assert!(buffer.ends_with(b"R")); |
| 933 |
|
| 934 |
let s = str::from_utf8(&buffer[2..buffer.len() - 1])?; |
| 935 |
|
| 936 |
let mut split = s.split(';'); |
| 937 |
|
| 938 |
let line = next_parsed::<NonZeroU16>(&mut split)?.into(); |
| 939 |
let col = next_parsed::<NonZeroU16>(&mut split)?.into(); |
| 940 |
|
| 941 |
Ok(Some(Event::Csi(Csi::Cursor( |
| 942 |
csi::Cursor::ActivePositionReport { line, col }, |
| 943 |
)))) |
| 944 |
} |
| 945 |
|
| 946 |
fn parse_csi_cursor_shape_query_response(buffer: &[u8]) -> Result<Option<Event>> { |
| 947 |
assert!(buffer.starts_with(b"\x1B[>")); |
| 948 |
assert!(buffer.ends_with(b" q")); |
| 949 |
|
| 950 |
if buffer.len() < 5 { |
| 951 |
|
| 952 |
return Ok(None); |
| 953 |
} |
| 954 |
|
| 955 |
let s = str::from_utf8(&buffer[3..buffer.len() - 2])?; |
| 956 |
|
| 957 |
|
| 958 |
if s.is_empty() { |
| 959 |
return Ok(Some(Event::Csi(Csi::Cursor(csi::Cursor::QueryCursorShape)))); |
| 960 |
} |
| 961 |
|
| 962 |
let caps: Vec<csi::MultiCursorCapability> = s |
| 963 |
.split(';') |
| 964 |
.filter(|part| !part.is_empty()) |
| 965 |
.map(|part| { |
| 966 |
part.parse::<u8>() |
| 967 |
.map_err(|_| MalformedSequenceError) |
| 968 |
.and_then(|v| { |
| 969 |
csi::MultiCursorCapability::try_from(v).map_err(|_| MalformedSequenceError) |
| 970 |
}) |
| 971 |
}) |
| 972 |
.collect::<Result<Vec<_>>>()?; |
| 973 |
|
| 974 |
Ok(Some(Event::Csi(Csi::Cursor( |
| 975 |
csi::Cursor::CursorShapeQueryResponse(caps), |
| 976 |
)))) |
| 977 |
} |
| 978 |
|
| 979 |
fn parse_csi_keyboard_enhancement_flags(buffer: &[u8]) -> Result<Option<Event>> { |
| 980 |
|
| 981 |
assert!(buffer.starts_with(b"\x1B[?")); |
| 982 |
assert!(buffer.ends_with(b"u")); |
| 983 |
|
| 984 |
if buffer.len() < 5 { |
| 985 |
return Ok(None); |
| 986 |
} |
| 987 |
|
| 988 |
let bits = buffer[3]; |
| 989 |
let mut flags = KittyKeyboardFlags::empty(); |
| 990 |
|
| 991 |
if bits & 1 != 0 { |
| 992 |
flags |= KittyKeyboardFlags::DISAMBIGUATE_ESCAPE_CODES; |
| 993 |
} |
| 994 |
if bits & 2 != 0 { |
| 995 |
flags |= KittyKeyboardFlags::REPORT_EVENT_TYPES; |
| 996 |
} |
| 997 |
if bits & 4 != 0 { |
| 998 |
flags |= KittyKeyboardFlags::REPORT_ALTERNATE_KEYS; |
| 999 |
} |
| 1000 |
if bits & 8 != 0 { |
| 1001 |
flags |= KittyKeyboardFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES; |
| 1002 |
} |
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
Ok(Some(Event::Csi(Csi::Keyboard(csi::Keyboard::ReportFlags( |
| 1009 |
flags, |
| 1010 |
))))) |
| 1011 |
} |
| 1012 |
|
| 1013 |
fn parse_csi_primary_device_attributes(buffer: &[u8]) -> Result<Option<Event>> { |
| 1014 |
|
| 1015 |
assert!(buffer.starts_with(b"\x1B[?")); |
| 1016 |
assert!(buffer.ends_with(b"c")); |
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
Ok(Some(Event::Csi(Csi::Device( |
| 1023 |
csi::Device::DeviceAttributes(()), |
| 1024 |
)))) |
| 1025 |
} |
| 1026 |
|
| 1027 |
fn parse_csi_theme_mode(buffer: &[u8]) -> Result<Option<Event>> { |
| 1028 |
|
| 1029 |
|
| 1030 |
assert!(buffer.starts_with(b"\x1B[?")); |
| 1031 |
assert!(buffer.ends_with(b"n")); |
| 1032 |
|
| 1033 |
let s = str::from_utf8(&buffer[3..buffer.len() - 1])?; |
| 1034 |
|
| 1035 |
let mut split = s.split(';'); |
| 1036 |
|
| 1037 |
if next_parsed::<u16>(&mut split)? != 997 { |
| 1038 |
bail!(); |
| 1039 |
} |
| 1040 |
|
| 1041 |
let theme_mode = match next_parsed::<u8>(&mut split)? { |
| 1042 |
1 => ThemeMode::Dark, |
| 1043 |
2 => ThemeMode::Light, |
| 1044 |
_ => bail!(), |
| 1045 |
}; |
| 1046 |
|
| 1047 |
Ok(Some(Event::Csi(Csi::Mode(csi::Mode::ReportTheme( |
| 1048 |
theme_mode, |
| 1049 |
))))) |
| 1050 |
} |
| 1051 |
|
| 1052 |
fn parse_csi_mode(buffer: &[u8]) -> Result<Option<Event>> { |
| 1053 |
|
| 1054 |
|
| 1055 |
assert!(buffer.starts_with(b"\x1B[?")); |
| 1056 |
assert!(buffer.ends_with(b"y")); |
| 1057 |
|
| 1058 |
let s = str::from_utf8(&buffer[3..buffer.len() - 1])?; |
| 1059 |
let s = match s.strip_suffix('$') { |
| 1060 |
Some(s) => s, |
| 1061 |
None => bail!(), |
| 1062 |
}; |
| 1063 |
|
| 1064 |
let mut split = s.split(';'); |
| 1065 |
|
| 1066 |
let mode = match next_parsed::<u16>(&mut split)? { |
| 1067 |
2026 => csi::DecPrivateMode::Code(csi::DecPrivateModeCode::SynchronizedOutput), |
| 1068 |
2027 => csi::DecPrivateMode::Code(csi::DecPrivateModeCode::GraphemeClustering), |
| 1069 |
_ => bail!(), |
| 1070 |
}; |
| 1071 |
|
| 1072 |
let setting = match next_parsed::<u8>(&mut split)? { |
| 1073 |
|
| 1074 |
|
| 1075 |
0 | 4 if mode == csi::DecPrivateMode::Code(csi::DecPrivateModeCode::SynchronizedOutput) => { |
| 1076 |
csi::DecModeSetting::NotRecognized |
| 1077 |
} |
| 1078 |
0 => csi::DecModeSetting::NotRecognized, |
| 1079 |
1 => csi::DecModeSetting::Set, |
| 1080 |
2 => csi::DecModeSetting::Reset, |
| 1081 |
3 if mode == csi::DecPrivateMode::Code(csi::DecPrivateModeCode::GraphemeClustering) => { |
| 1082 |
csi::DecModeSetting::PermanentlySet |
| 1083 |
} |
| 1084 |
4 => csi::DecModeSetting::PermanentlyReset, |
| 1085 |
_ => bail!(), |
| 1086 |
}; |
| 1087 |
|
| 1088 |
Ok(Some(Event::Csi(Csi::Mode( |
| 1089 |
csi::Mode::ReportDecPrivateMode { mode, setting }, |
| 1090 |
)))) |
| 1091 |
} |
| 1092 |
|
| 1093 |
fn parse_dcs(buffer: &[u8]) -> Result<Option<Event>> { |
| 1094 |
assert!(buffer.starts_with(escape::DCS.as_bytes())); |
| 1095 |
if !buffer.ends_with(escape::ST.as_bytes()) { |
| 1096 |
return Ok(None); |
| 1097 |
} |
| 1098 |
match buffer[buffer.len() - 3] { |
| 1099 |
|
| 1100 |
b'm' => { |
| 1101 |
if buffer.get(3..5) != Some(b"$r") { |
| 1102 |
bail!(); |
| 1103 |
} |
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
let is_request_valid = match buffer[2] { |
| 1108 |
b'1' => true, |
| 1109 |
|
| 1110 |
b'0' => false, |
| 1111 |
_ => bail!(), |
| 1112 |
}; |
| 1113 |
let s = str::from_utf8(&buffer[5..buffer.len() - 3])?; |
| 1114 |
let mut sgrs = Vec::new(); |
| 1115 |
|
| 1116 |
for sgr in s.split(';') { |
| 1117 |
sgrs.push(parse_sgr(sgr)?); |
| 1118 |
} |
| 1119 |
Ok(Some(Event::Dcs(dcs::Dcs::Response { |
| 1120 |
is_request_valid, |
| 1121 |
value: dcs::DcsResponse::GraphicRendition(sgrs), |
| 1122 |
}))) |
| 1123 |
} |
| 1124 |
_ => bail!(), |
| 1125 |
} |
| 1126 |
} |
| 1127 |
|
| 1128 |
fn parse_sgr(buffer: &str) -> Result<csi::Sgr> { |
| 1129 |
use csi::Sgr; |
| 1130 |
use style::*; |
| 1131 |
|
| 1132 |
let sgr = match buffer { |
| 1133 |
"0" => Sgr::Reset, |
| 1134 |
"22" => Sgr::Intensity(Intensity::Normal), |
| 1135 |
"1" => Sgr::Intensity(Intensity::Bold), |
| 1136 |
"2" => Sgr::Intensity(Intensity::Dim), |
| 1137 |
"24" => Sgr::Underline(Underline::None), |
| 1138 |
"4" => Sgr::Underline(Underline::Single), |
| 1139 |
"21" => Sgr::Underline(Underline::Double), |
| 1140 |
"4:3 " => Sgr::Underline(Underline::Curly), |
| 1141 |
"4:4" => Sgr::Underline(Underline::Dotted), |
| 1142 |
"4:5" => Sgr::Underline(Underline::Dashed), |
| 1143 |
"25" => Sgr::Blink(Blink::None), |
| 1144 |
"5" => Sgr::Blink(Blink::Slow), |
| 1145 |
"6" => Sgr::Blink(Blink::Rapid), |
| 1146 |
"3" => Sgr::Italic(true), |
| 1147 |
"23" => Sgr::Italic(false), |
| 1148 |
"7" => Sgr::Reverse(true), |
| 1149 |
"27" => Sgr::Reverse(false), |
| 1150 |
"8" => Sgr::Invisible(true), |
| 1151 |
"28" => Sgr::Invisible(false), |
| 1152 |
"9" => Sgr::StrikeThrough(true), |
| 1153 |
"29" => Sgr::StrikeThrough(false), |
| 1154 |
"53" => Sgr::Overline(true), |
| 1155 |
"55" => Sgr::Overline(false), |
| 1156 |
"10" => Sgr::Font(Font::Default), |
| 1157 |
"11" => Sgr::Font(Font::Alternate(1)), |
| 1158 |
"12" => Sgr::Font(Font::Alternate(2)), |
| 1159 |
"13" => Sgr::Font(Font::Alternate(3)), |
| 1160 |
"14" => Sgr::Font(Font::Alternate(4)), |
| 1161 |
"15" => Sgr::Font(Font::Alternate(5)), |
| 1162 |
"16" => Sgr::Font(Font::Alternate(6)), |
| 1163 |
"17" => Sgr::Font(Font::Alternate(7)), |
| 1164 |
"18" => Sgr::Font(Font::Alternate(8)), |
| 1165 |
"19" => Sgr::Font(Font::Alternate(9)), |
| 1166 |
"75" => Sgr::VerticalAlign(VerticalAlign::BaseLine), |
| 1167 |
"73" => Sgr::VerticalAlign(VerticalAlign::SuperScript), |
| 1168 |
"74" => Sgr::VerticalAlign(VerticalAlign::SubScript), |
| 1169 |
"39" => Sgr::Foreground(ColorSpec::Reset), |
| 1170 |
"30" => Sgr::Foreground(ColorSpec::BLACK), |
| 1171 |
"31" => Sgr::Foreground(ColorSpec::RED), |
| 1172 |
"32" => Sgr::Foreground(ColorSpec::GREEN), |
| 1173 |
"33" => Sgr::Foreground(ColorSpec::YELLOW), |
| 1174 |
"34" => Sgr::Foreground(ColorSpec::BLUE), |
| 1175 |
"35" => Sgr::Foreground(ColorSpec::MAGENTA), |
| 1176 |
"36" => Sgr::Foreground(ColorSpec::CYAN), |
| 1177 |
"37" => Sgr::Foreground(ColorSpec::WHITE), |
| 1178 |
"90" => Sgr::Foreground(ColorSpec::BRIGHT_BLACK), |
| 1179 |
"91" => Sgr::Foreground(ColorSpec::BRIGHT_RED), |
| 1180 |
"92" => Sgr::Foreground(ColorSpec::BRIGHT_GREEN), |
| 1181 |
"93" => Sgr::Foreground(ColorSpec::BRIGHT_YELLOW), |
| 1182 |
"94" => Sgr::Foreground(ColorSpec::BRIGHT_BLUE), |
| 1183 |
"95" => Sgr::Foreground(ColorSpec::BRIGHT_MAGENTA), |
| 1184 |
"96" => Sgr::Foreground(ColorSpec::BRIGHT_CYAN), |
| 1185 |
"97" => Sgr::Foreground(ColorSpec::BRIGHT_WHITE), |
| 1186 |
"49" => Sgr::Background(ColorSpec::Reset), |
| 1187 |
"40" => Sgr::Background(ColorSpec::BLACK), |
| 1188 |
"41" => Sgr::Background(ColorSpec::RED), |
| 1189 |
"42" => Sgr::Background(ColorSpec::GREEN), |
| 1190 |
"43" => Sgr::Background(ColorSpec::YELLOW), |
| 1191 |
"44" => Sgr::Background(ColorSpec::BLUE), |
| 1192 |
"45" => Sgr::Background(ColorSpec::MAGENTA), |
| 1193 |
"46" => Sgr::Background(ColorSpec::CYAN), |
| 1194 |
"47" => Sgr::Background(ColorSpec::WHITE), |
| 1195 |
"100" => Sgr::Background(ColorSpec::BRIGHT_BLACK), |
| 1196 |
"101" => Sgr::Background(ColorSpec::BRIGHT_RED), |
| 1197 |
"102" => Sgr::Background(ColorSpec::BRIGHT_GREEN), |
| 1198 |
"103" => Sgr::Background(ColorSpec::BRIGHT_YELLOW), |
| 1199 |
"104" => Sgr::Background(ColorSpec::BRIGHT_BLUE), |
| 1200 |
"105" => Sgr::Background(ColorSpec::BRIGHT_MAGENTA), |
| 1201 |
"106" => Sgr::Background(ColorSpec::BRIGHT_CYAN), |
| 1202 |
"107" => Sgr::Background(ColorSpec::BRIGHT_WHITE), |
| 1203 |
"59" => Sgr::UnderlineColor(ColorSpec::Reset), |
| 1204 |
_ => { |
| 1205 |
let mut split = buffer.split(':').filter(|s| !s.is_empty()); |
| 1206 |
let first = next_parsed::<u8>(&mut split)?; |
| 1207 |
let color = match next_parsed::<u8>(&mut split)? { |
| 1208 |
2 => RgbColor { |
| 1209 |
red: next_parsed::<u8>(&mut split)?, |
| 1210 |
green: next_parsed::<u8>(&mut split)?, |
| 1211 |
blue: next_parsed::<u8>(&mut split)?, |
| 1212 |
} |
| 1213 |
.into(), |
| 1214 |
5 => ColorSpec::PaletteIndex(next_parsed::<u8>(&mut split)?), |
| 1215 |
6 => RgbaColor { |
| 1216 |
red: next_parsed::<u8>(&mut split)?, |
| 1217 |
green: next_parsed::<u8>(&mut split)?, |
| 1218 |
blue: next_parsed::<u8>(&mut split)?, |
| 1219 |
alpha: next_parsed::<u8>(&mut split)?, |
| 1220 |
} |
| 1221 |
.into(), |
| 1222 |
_ => bail!(), |
| 1223 |
}; |
| 1224 |
match first { |
| 1225 |
38 => Sgr::Foreground(color), |
| 1226 |
48 => Sgr::Background(color), |
| 1227 |
58 => Sgr::UnderlineColor(color), |
| 1228 |
_ => bail!(), |
| 1229 |
} |
| 1230 |
} |
| 1231 |
}; |
| 1232 |
Ok(sgr) |
| 1233 |
} |
| 1234 |
|
| 1235 |
#[cfg(test)] |
| 1236 |
mod test { |
| 1237 |
use super::*; |
| 1238 |
|
| 1239 |
#[test] |
| 1240 |
fn parse_dcs_sgr_response() { |
| 1241 |
|
| 1242 |
|
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
let event = parse_event(b"\x1bP0$r0;4;5;7m\x1b\\", false) |
| 1248 |
.unwrap() |
| 1249 |
.unwrap(); |
| 1250 |
assert_eq!( |
| 1251 |
event, |
| 1252 |
Event::Dcs(dcs::Dcs::Response { |
| 1253 |
is_request_valid: false, |
| 1254 |
value: dcs::DcsResponse::GraphicRendition(vec![ |
| 1255 |
csi::Sgr::Reset, |
| 1256 |
csi::Sgr::Underline(style::Underline::Single), |
| 1257 |
csi::Sgr::Blink(style::Blink::Slow), |
| 1258 |
csi::Sgr::Reverse(true), |
| 1259 |
]) |
| 1260 |
}) |
| 1261 |
); |
| 1262 |
} |
| 1263 |
|
| 1264 |
#[test] |
| 1265 |
fn parse_osc_dynamic_color_response() { |
| 1266 |
assert_eq!( |
| 1267 |
parse_event(b"\x1b]11;rgb:2828/2828/2828\x1b\\", false) |
| 1268 |
.unwrap() |
| 1269 |
.unwrap(), |
| 1270 |
Event::Osc(osc::Osc::ChangeDynamicColors( |
| 1271 |
osc::DynamicColorNumber::TextBackgroundColor, |
| 1272 |
vec![style::RgbColor::new(40, 40, 40).into()] |
| 1273 |
)) |
| 1274 |
); |
| 1275 |
|
| 1276 |
assert_eq!( |
| 1277 |
parse_event(b"\x1b]11;rgb:2828/2828/2828\x07", false) |
| 1278 |
.unwrap() |
| 1279 |
.unwrap(), |
| 1280 |
Event::Osc(osc::Osc::ChangeDynamicColors( |
| 1281 |
osc::DynamicColorNumber::TextBackgroundColor, |
| 1282 |
vec![style::RgbColor::new(40, 40, 40).into()] |
| 1283 |
)) |
| 1284 |
); |
| 1285 |
} |
| 1286 |
|
| 1287 |
#[test] |
| 1288 |
fn parse_cursor_shape_query() { |
| 1289 |
|
| 1290 |
let event = parse_event(b"\x1b[> q", false).unwrap().unwrap(); |
| 1291 |
assert_eq!( |
| 1292 |
event, |
| 1293 |
Event::Csi(Csi::Cursor(csi::Cursor::QueryCursorShape)) |
| 1294 |
); |
| 1295 |
} |
| 1296 |
|
| 1297 |
#[test] |
| 1298 |
fn parse_cursor_shape_query_response() { |
| 1299 |
|
| 1300 |
let event = parse_event(b"\x1b[>1;2;29;100 q", false).unwrap().unwrap(); |
| 1301 |
assert_eq!( |
| 1302 |
event, |
| 1303 |
Event::Csi(Csi::Cursor(csi::Cursor::CursorShapeQueryResponse(vec![ |
| 1304 |
csi::MultiCursorCapability::BlockShape, |
| 1305 |
csi::MultiCursorCapability::BeamShape, |
| 1306 |
csi::MultiCursorCapability::FollowMainCursorShape, |
| 1307 |
csi::MultiCursorCapability::QueryCurrentCursors, |
| 1308 |
]))) |
| 1309 |
); |
| 1310 |
} |
| 1311 |
|
| 1312 |
#[test] |
| 1313 |
fn parse_cursor_shape_query_response_invalid() { |
| 1314 |
|
| 1315 |
assert!(parse_event(b"\x1b[>7 q", false).is_err()); |
| 1316 |
} |
| 1317 |
|
| 1318 |
#[test] |
| 1319 |
fn cursor_shape_query_response_round_trip() { |
| 1320 |
let response = csi::Cursor::CursorShapeQueryResponse(vec![ |
| 1321 |
csi::MultiCursorCapability::BlockShape, |
| 1322 |
csi::MultiCursorCapability::FollowMainCursorShape, |
| 1323 |
csi::MultiCursorCapability::QueryCurrentCursors, |
| 1324 |
]); |
| 1325 |
let encoded = Csi::Cursor(response.clone()).to_string(); |
| 1326 |
assert_eq!(encoded, "\x1b[>1;29;100 q"); |
| 1327 |
|
| 1328 |
let parsed = parse_event(encoded.as_bytes(), false).unwrap().unwrap(); |
| 1329 |
assert_eq!(parsed, Event::Csi(Csi::Cursor(response))); |
| 1330 |
} |
| 1331 |
|
| 1332 |
#[test] |
| 1333 |
fn parse_synchronized_output_mode_set() { |
| 1334 |
let event = parse_event(b"\x1b[?2026;1$y", false).unwrap().unwrap(); |
| 1335 |
assert_eq!( |
| 1336 |
event, |
| 1337 |
Event::Csi(Csi::Mode(csi::Mode::ReportDecPrivateMode { |
| 1338 |
mode: csi::DecPrivateMode::Code(csi::DecPrivateModeCode::SynchronizedOutput), |
| 1339 |
setting: csi::DecModeSetting::Set, |
| 1340 |
})) |
| 1341 |
); |
| 1342 |
} |
| 1343 |
|
| 1344 |
#[test] |
| 1345 |
fn parse_grapheme_clustering_mode_set() { |
| 1346 |
let event = parse_event(b"\x1b[?2027;1$y", false).unwrap().unwrap(); |
| 1347 |
assert_eq!( |
| 1348 |
event, |
| 1349 |
Event::Csi(Csi::Mode(csi::Mode::ReportDecPrivateMode { |
| 1350 |
mode: csi::DecPrivateMode::Code(csi::DecPrivateModeCode::GraphemeClustering), |
| 1351 |
setting: csi::DecModeSetting::Set, |
| 1352 |
})) |
| 1353 |
); |
| 1354 |
} |
| 1355 |
|
| 1356 |
#[test] |
| 1357 |
fn parse_bracketed_paste() { |
| 1358 |
|
| 1359 |
let event = parse_event(b"\x1b[200~", false).unwrap(); |
| 1360 |
assert_eq!(event, None); |
| 1361 |
let event = parse_event(b"\x1b[200~Hello, world!\x1b[201~", false).unwrap(); |
| 1362 |
assert_eq!(event, Some(Event::Paste("Hello, world!".to_string()))); |
| 1363 |
let event = parse_event(b"\x1b[200~\x1b[201~", false).unwrap(); |
| 1364 |
assert_eq!(event, Some(Event::Paste("".to_string()))); |
| 1365 |
} |
| 1366 |
} |