diff options
| author | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-07-18 02:51:29 +0300 |
|---|---|---|
| committer | Ogulcan Celik <ogulcancelik@gmail.com> | 2026-07-18 02:51:29 +0300 |
| commit | c1f7b6f331501a784bfad896310eae1d064bd9b9 (patch) | |
| tree | bcab5fd693c024543ab90ea766f56153c4fee891 /src/raw_input.rs | |
| parent | e298e4589050349630d0faaec00ef11555b83ae1 (diff) | |
fix: preserve lone escape input boundaries
refs #541
Diffstat (limited to 'src/raw_input.rs')
| -rw-r--r-- | src/raw_input.rs | 46 |
1 files changed, 45 insertions, 1 deletions
diff --git a/src/raw_input.rs b/src/raw_input.rs index 848df88d..a6ca316a 100644 --- a/src/raw_input.rs +++ b/src/raw_input.rs @@ -128,6 +128,12 @@ pub(crate) struct RawInputFramer { } impl RawInputFramer { + pub(crate) fn for_host_input() -> Self { + Self { + byte_framer: RawInputByteFramer::for_host_input(), + } + } + pub(crate) fn push(&mut self, data: &[u8]) -> Vec<RawInputEvent> { Self::events_from_chunks(self.byte_framer.push(data)) } @@ -185,12 +191,26 @@ pub(crate) struct RawInputByteFramer { host_color_replies_awaited: u8, held_pending_color_esc: bool, host_color_scheme_change_tracking: bool, + split_coalesced_escape: bool, } const HOST_COLOR_QUERY_REPLIES: u8 = 2; const MAX_ORPHANED_SGR_MOUSE_TAIL_BYTES: usize = 32; impl RawInputByteFramer { + pub(crate) fn for_host_input() -> Self { + Self::with_host_input_policy( + crate::platform::capabilities().preserve_legacy_doubled_escape_input, + ) + } + + fn with_host_input_policy(preserve_legacy_doubled_escape_input: bool) -> Self { + Self { + split_coalesced_escape: !preserve_legacy_doubled_escape_input, + ..Self::default() + } + } + pub(crate) fn push(&mut self, data: &[u8]) -> Vec<Vec<u8>> { self.buffer.extend_from_slice(data); self.drain_available_chunks() @@ -405,6 +425,12 @@ impl RawInputByteFramer { continue; } + if self.split_coalesced_escape && self.buffer.starts_with(b"\x1b\x1b") { + chunks.push(vec![ESC]); + self.buffer.drain(..1); + continue; + } + let Some((event, consumed)) = extract_one_event(&self.buffer) else { break; }; @@ -491,7 +517,7 @@ pub fn spawn_input_reader() -> mpsc::Receiver<RawInputEvent> { let stdin = std::io::stdin(); let mut reader = stdin.lock(); let mut scratch = [0u8; 1024]; - let mut framer = RawInputFramer::default(); + let mut framer = RawInputFramer::for_host_input(); framer.host_color_query_sent(); framer.enable_host_color_scheme_change_tracking(); @@ -1620,6 +1646,24 @@ mod tests { assert!(framer.flush_timeout().is_empty()); } + #[cfg(not(target_os = "macos"))] + #[test] + fn non_macos_host_input_splits_lone_escape_from_arrow() { + let mut framer = RawInputByteFramer::for_host_input(); + + assert_eq!( + framer.push(b"\x1b\x1b[D"), + vec![b"\x1b".to_vec(), b"\x1b[D".to_vec()] + ); + } + + #[test] + fn macos_host_input_policy_preserves_legacy_doubled_escape_alt_arrow() { + let mut framer = RawInputByteFramer::with_host_input_policy(true); + + assert_eq!(framer.push(b"\x1b\x1b[D"), vec![b"\x1b\x1b[D".to_vec()]); + } + #[test] fn legacy_reader_extends_only_incomplete_sgr_mouse_timeout() { let mut mouse = RawInputFramer::default(); |
