From d239e65e64ee5f68264b50408d6d7850e8b8cc2e Mon Sep 17 00:00:00 2001 From: Wez Furlong Date: Sat, 8 Apr 2023 18:19:07 -0700 Subject: [PATCH] fixup kitty keyboard encoding for F1-F4 refs: https://github.com/wez/wezterm/issues/3473 --- docs/changelog.md | 1 + termwiz/src/input.rs | 82 +++++++++++++++++++++++++++----------------- 2 files changed, 51 insertions(+), 32 deletions(-) diff --git a/docs/changelog.md b/docs/changelog.md index 4bbcef522..e8e2cc064 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -25,6 +25,7 @@ As features stabilize some brief notes about them will accumulate here. * Modals, such as `CharSelect` and `CommandPalette` did not respect alternative OS-level key maps. #3470 +* key encoding: incorrect F1-F4 representation when using kitty encoding. #3473 ### 20230408-112425-69ae8472 diff --git a/termwiz/src/input.rs b/termwiz/src/input.rs index af65bf865..41ba44fdc 100644 --- a/termwiz/src/input.rs +++ b/termwiz/src/input.rs @@ -413,38 +413,28 @@ impl KeyCode { Ok(format!("\x1b[{c};{modifiers}{event_type}~")) } Function(n) if n < 13 => { - if mods.is_empty() && n < 5 { - // F1-F4 are encoded using SS3 if there are no modifiers - Ok(format!( - "{}", - match n { - 1 => "\x1bOP", - 2 => "\x1bOQ", - 3 => "\x1bOR", - 4 => "\x1bOS", - _ => unreachable!("wat?"), - } - )) - } else { - // Higher numbered F-keys plus modified F-keys are encoded - // using CSI instead of SS3. - let intro = match n { - 1 => "\x1b[11", - 2 => "\x1b[12", - 3 => "\x1b[13", - 4 => "\x1b[14", - 5 => "\x1b[15", - 6 => "\x1b[17", - 7 => "\x1b[18", - 8 => "\x1b[19", - 9 => "\x1b[20", - 10 => "\x1b[21", - 11 => "\x1b[23", - 12 => "\x1b[24", - _ => unreachable!(), - }; - Ok(format!("{intro};{modifiers}{event_type}~")) - } + // The spec says that kitty prefers an SS3 form for F1-F4, + // but then has some variance in the encoding and cites a + // compatibility issue with a cursor position report. + // Since it allows reporting these all unambiguously with + // the same general scheme, that is what we're using here. + let intro = match n { + 1 => "\x1b[11", + 2 => "\x1b[12", + 3 => "\x1b[13", + 4 => "\x1b[14", + 5 => "\x1b[15", + 6 => "\x1b[17", + 7 => "\x1b[18", + 8 => "\x1b[19", + 9 => "\x1b[20", + 10 => "\x1b[21", + 11 => "\x1b[23", + 12 => "\x1b[24", + _ => unreachable!(), + }; + + Ok(format!("{intro};{modifiers}{event_type}~")) } _ => { @@ -2087,4 +2077,32 @@ mod test { res ); } + + #[test] + fn encode_issue_3473() { + let mode = KeyCodeEncodeModes { + encoding: KeyboardEncoding::Kitty( + KittyKeyboardFlags::DISAMBIGUATE_ESCAPE_CODES + | KittyKeyboardFlags::REPORT_EVENT_TYPES + | KittyKeyboardFlags::REPORT_ALTERNATE_KEYS + | KittyKeyboardFlags::REPORT_ALL_KEYS_AS_ESCAPE_CODES, + ), + newline_mode: false, + application_cursor_keys: false, + modify_other_keys: None, + }; + + assert_eq!( + KeyCode::Function(1) + .encode(Modifiers::NONE, mode, true) + .unwrap(), + "\x1b[11;1~".to_string() + ); + assert_eq!( + KeyCode::Function(1) + .encode(Modifiers::NONE, mode, false) + .unwrap(), + "\x1b[11;1:3~".to_string() + ); + } }