From 1bf5eda8542ca1b9f65b1dbe87577001801798dc Mon Sep 17 00:00:00 2001 From: filipriec Date: Fri, 28 Feb 2025 16:54:05 +0100 Subject: [PATCH] command mode keybindings propery done --- client/config.toml | 5 +- client/src/config/config.rs | 18 ++++++ client/src/modes/handlers/command_mode.rs | 69 +++++++++++------------ 3 files changed, 52 insertions(+), 40 deletions(-) diff --git a/client/config.toml b/client/config.toml index d23936e..b79d565 100644 --- a/client/config.toml +++ b/client/config.toml @@ -6,8 +6,6 @@ quit = [":q", "ctrl+q"] force_quit = [":q!", "ctrl+shift+q"] save_and_quit = [":wq", "ctrl+shift+s"] -enter_command_mode = [":", "ctrl+;"] - # MODE SPECIFIC # READ ONLY MODE enter_edit_mode_before = ["i"] @@ -36,9 +34,10 @@ next_field = ["tab", "enter"] prev_field = ["shift+tab", "backtab"] # COMMAND MODE +enter_command_mode = [":", "ctrl+;"] +exit_command_mode = ["ctrl+g", "esc"] command_execute = ["enter"] command_backspace = ["backspace"] -exit_command_mode = ["ctrl+g", "esc"] [colors] theme = "dark" diff --git a/client/src/config/config.rs b/client/src/config/config.rs index d7d1bb3..968c739 100644 --- a/client/src/config/config.rs +++ b/client/src/config/config.rs @@ -300,4 +300,22 @@ impl Config { false } + + pub fn is_command_execute(&self, key: KeyCode, modifiers: KeyModifiers) -> bool { + if let Some(bindings) = self.keybindings.get("command_execute") { + bindings.iter().any(|b| Self::matches_keybinding(b, key, modifiers)) + } else { + // Fall back to Enter key if no command_execute is defined + key == KeyCode::Enter && modifiers.is_empty() + } + } + + pub fn is_command_backspace(&self, key: KeyCode, modifiers: KeyModifiers) -> bool { + if let Some(bindings) = self.keybindings.get("command_backspace") { + bindings.iter().any(|b| Self::matches_keybinding(b, key, modifiers)) + } else { + // Fall back to Backspace key if no command_backspace is defined + key == KeyCode::Backspace && modifiers.is_empty() + } + } } diff --git a/client/src/modes/handlers/command_mode.rs b/client/src/modes/handlers/command_mode.rs index b3bd1ea..a80bd4f 100644 --- a/client/src/modes/handlers/command_mode.rs +++ b/client/src/modes/handlers/command_mode.rs @@ -4,7 +4,6 @@ use crossterm::event::{KeyEvent, KeyCode, KeyModifiers}; use crate::tui::terminal::AppTerminal; use crate::config::config::Config; use crate::ui::handlers::form::FormState; -use crate::config::key_sequences::KeySequenceTracker; use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest}; pub async fn handle_command_event( @@ -20,47 +19,43 @@ pub async fn handle_command_event( ) -> Result<(bool, String, bool), Box> { // Return value: (should_exit, message, should_exit_command_mode) - // ONLY check for command mode specific actions - // This is a limited set of actions, not all keybindings - match key.code { - // Check for exit_command_mode action - KeyCode::Esc | KeyCode::Char('g') if key.modifiers.contains(KeyModifiers::CONTROL) => { - if config.is_exit_command_mode(key.code, key.modifiers) { - command_input.clear(); - *command_message = "".to_string(); - return Ok((false, "".to_string(), true)); - } - } - KeyCode::Enter => { - // Execute the command - return process_command( - config, - form_state, - command_input, - command_message, - app_terminal, - is_saved, - current_position, - total_count, - ).await; - } - KeyCode::Char(c) => { - // In command mode, all character keys should be treated as text input - // NOT as potential keybindings + // Exit command mode (via configurable keybinding) + if config.is_exit_command_mode(key.code, key.modifiers) { + command_input.clear(); + *command_message = "".to_string(); + return Ok((false, "".to_string(), true)); + } + + // Execute command (via configurable keybinding, defaults to Enter) + if config.is_command_execute(key.code, key.modifiers) { + return process_command( + config, + form_state, + command_input, + command_message, + app_terminal, + is_saved, + current_position, + total_count, + ).await; + } + + // Backspace (via configurable keybinding, defaults to Backspace) + if config.is_command_backspace(key.code, key.modifiers) { + command_input.pop(); + return Ok((false, "".to_string(), false)); + } + + // Regular character input - accept any character in command mode + if let KeyCode::Char(c) = key.code { + // Accept regular or shifted characters (e.g., 'a' or 'A') + if key.modifiers.is_empty() || key.modifiers == KeyModifiers::SHIFT { command_input.push(c); return Ok((false, "".to_string(), false)); } - KeyCode::Backspace => { - command_input.pop(); - return Ok((false, "".to_string(), false)); - } - _ => { - // Ignore other keys - don't process them as keybindings - return Ok((false, "".to_string(), false)); - } } - // Default case - nothing matched + // Ignore all other keys Ok((false, "".to_string(), false)) }