From ed0c051c55e40c1ab3cdc029ba4ca518ee407a1e Mon Sep 17 00:00:00 2001 From: Ogulcan Celik Date: Tue, 7 Apr 2026 15:48:58 +0300 Subject: fix: preserve lf in raw terminal input --- src/input/parse.rs | 5 ++++- src/input/tests.rs | 13 +++++++++++++ src/raw_input.rs | 10 ++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/input/parse.rs b/src/input/parse.rs index 525119f4..b7b449ae 100644 --- a/src/input/parse.rs +++ b/src/input/parse.rs @@ -61,7 +61,10 @@ fn parse_legacy_key_sequence(data: &str) -> Option { } match data { - "\r" | "\n" => Some(TerminalKey::new(KeyCode::Enter, KeyModifiers::empty())), + // In raw mode, Enter is carriage return. A bare line feed is ambiguous in + // terminal input and is commonly used for Ctrl+J or Shift+Enter workarounds. + // Preserve LF by letting it fall through to legacy control-byte parsing. + "\r" => Some(TerminalKey::new(KeyCode::Enter, KeyModifiers::empty())), "\t" => Some(TerminalKey::new(KeyCode::Tab, KeyModifiers::empty())), "\x1b" => Some(TerminalKey::new(KeyCode::Esc, KeyModifiers::empty())), "\x7f" => Some(TerminalKey::new(KeyCode::Backspace, KeyModifiers::empty())), diff --git a/src/input/tests.rs b/src/input/tests.rs index a3c71675..6482b245 100644 --- a/src/input/tests.rs +++ b/src/input/tests.rs @@ -461,6 +461,19 @@ fn parse_legacy_ctrl_c_sequence() { assert_eq!(key.modifiers, KeyModifiers::CONTROL); } +#[test] +fn parse_legacy_lf_sequence_as_ctrl_j() { + let key = parse_terminal_key_sequence("\n").unwrap(); + assert_eq!(key.code, KeyCode::Char('j')); + assert_eq!(key.modifiers, KeyModifiers::CONTROL); +} + +#[test] +fn legacy_lf_roundtrips_as_lf() { + let key = parse_terminal_key_sequence("\n").unwrap(); + assert_eq!(encode_terminal_key(key, KeyboardProtocol::Legacy), b"\n"); +} + #[test] fn legacy_ctrl_byte_matrix_is_covered() { for (byte, expected) in [ diff --git a/src/raw_input.rs b/src/raw_input.rs index 059791ea..262721c8 100644 --- a/src/raw_input.rs +++ b/src/raw_input.rs @@ -530,6 +530,16 @@ mod tests { assert_eq!(key.modifiers, KeyModifiers::CONTROL); } + #[test] + fn parses_raw_lf_as_ctrl_j() { + let (RawInputEvent::Key(key), consumed) = extract_one_event(b"\n").unwrap() else { + panic!("expected key"); + }; + assert_eq!(consumed, 1); + assert_eq!(key.code, KeyCode::Char('j')); + assert_eq!(key.modifiers, KeyModifiers::CONTROL); + } + fn assert_fixture_extracts_whole_events(corpus: &str, macos_layout: bool) { for line in corpus.lines() { let line = line.trim(); -- cgit v1.2.3