From 133896256831363b30c1673424bc21635abe0651 Mon Sep 17 00:00:00 2001 From: hiasr Date: Thu, 2 May 2024 08:38:19 +0200 Subject: [PATCH] feat(stdin): allow binding Ctrl J (#3307) * Fixed Control j keybindings * Fix formatting --- zellij-client/src/input_handler.rs | 6 +++++- zellij-utils/src/input/mod.rs | 24 ++++++++++++++++++++++-- 2 files changed, 27 insertions(+), 3 deletions(-) diff --git a/zellij-client/src/input_handler.rs b/zellij-client/src/input_handler.rs index ba5fca18..058b2bbc 100644 --- a/zellij-client/src/input_handler.rs +++ b/zellij-client/src/input_handler.rs @@ -91,7 +91,11 @@ impl InputHandler { Ok((InputInstruction::KeyEvent(input_event, raw_bytes), _error_context)) => { match input_event { InputEvent::Key(key_event) => { - let key = cast_termwiz_key(key_event, &raw_bytes); + let key = cast_termwiz_key( + key_event, + &raw_bytes, + Some((&self.config.keybinds, &self.mode)), + ); self.handle_key(&key, raw_bytes); }, InputEvent::Mouse(mouse_event) => { diff --git a/zellij-utils/src/input/mod.rs b/zellij-utils/src/input/mod.rs index 1d30841f..31481377 100644 --- a/zellij-utils/src/input/mod.rs +++ b/zellij-utils/src/input/mod.rs @@ -24,6 +24,8 @@ mod not_wasm { }; use termwiz::input::{InputEvent, InputParser, KeyCode, KeyEvent, Modifiers}; + use super::keybinds::Keybinds; + /// Creates a [`ModeInfo`] struct indicating the current [`InputMode`] and its keybinds /// (as pairs of [`String`]s). pub fn get_mode_info( @@ -49,16 +51,26 @@ mod not_wasm { let maybe_more = false; let parse_input_event = |input_event: InputEvent| { if let InputEvent::Key(key_event) = input_event { - ret.push(cast_termwiz_key(key_event, input_bytes)); + ret.push(cast_termwiz_key(key_event, input_bytes, None)); } }; input_parser.parse(input_bytes, parse_input_event, maybe_more); ret } + fn key_is_bound(key: Key, keybinds: &Keybinds, mode: &InputMode) -> bool { + keybinds + .get_actions_for_key_in_mode(mode, &key) + .map_or(false, |actions| !actions.is_empty()) + } + // FIXME: This is an absolutely cursed function that should be destroyed as soon // as an alternative that doesn't touch zellij-tile can be developed... - pub fn cast_termwiz_key(event: KeyEvent, raw_bytes: &[u8]) -> Key { + pub fn cast_termwiz_key( + event: KeyEvent, + raw_bytes: &[u8], + keybinds_mode: Option<(&Keybinds, &InputMode)>, + ) -> Key { let modifiers = event.modifiers; // *** THIS IS WHERE WE SHOULD WORK AROUND ISSUES WITH TERMWIZ *** @@ -66,6 +78,14 @@ mod not_wasm { return Key::Ctrl('h'); }; + if raw_bytes == [10] { + if let Some((keybinds, mode)) = keybinds_mode { + if key_is_bound(Key::Ctrl('j'), keybinds, mode) { + return Key::Ctrl('j'); + } + } + } + match event.key { KeyCode::Char(c) => { if modifiers.contains(Modifiers::CTRL) {