feat(stdin): allow binding Ctrl J (#3307)

* Fixed Control j keybindings

* Fix formatting
This commit is contained in:
hiasr 2024-05-02 08:38:19 +02:00 committed by GitHub
parent 1fda2e2c73
commit 1338962568
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 27 additions and 3 deletions

View file

@ -91,7 +91,11 @@ impl InputHandler {
Ok((InputInstruction::KeyEvent(input_event, raw_bytes), _error_context)) => { Ok((InputInstruction::KeyEvent(input_event, raw_bytes), _error_context)) => {
match input_event { match input_event {
InputEvent::Key(key_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); self.handle_key(&key, raw_bytes);
}, },
InputEvent::Mouse(mouse_event) => { InputEvent::Mouse(mouse_event) => {

View file

@ -24,6 +24,8 @@ mod not_wasm {
}; };
use termwiz::input::{InputEvent, InputParser, KeyCode, KeyEvent, Modifiers}; use termwiz::input::{InputEvent, InputParser, KeyCode, KeyEvent, Modifiers};
use super::keybinds::Keybinds;
/// Creates a [`ModeInfo`] struct indicating the current [`InputMode`] and its keybinds /// Creates a [`ModeInfo`] struct indicating the current [`InputMode`] and its keybinds
/// (as pairs of [`String`]s). /// (as pairs of [`String`]s).
pub fn get_mode_info( pub fn get_mode_info(
@ -49,16 +51,26 @@ mod not_wasm {
let maybe_more = false; let maybe_more = false;
let parse_input_event = |input_event: InputEvent| { let parse_input_event = |input_event: InputEvent| {
if let InputEvent::Key(key_event) = input_event { 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); input_parser.parse(input_bytes, parse_input_event, maybe_more);
ret 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 // 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... // 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; let modifiers = event.modifiers;
// *** THIS IS WHERE WE SHOULD WORK AROUND ISSUES WITH TERMWIZ *** // *** THIS IS WHERE WE SHOULD WORK AROUND ISSUES WITH TERMWIZ ***
@ -66,6 +78,14 @@ mod not_wasm {
return Key::Ctrl('h'); 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 { match event.key {
KeyCode::Char(c) => { KeyCode::Char(c) => {
if modifiers.contains(Modifiers::CTRL) { if modifiers.contains(Modifiers::CTRL) {