// src/modes/handlers/command_mode.rs use crossterm::event::{KeyEvent, KeyCode, KeyModifiers}; use crate::tui::terminal::AppTerminal; use crate::config::config::Config; use crate::ui::handlers::form::FormState; use super::common; pub async fn handle_command_event( key: KeyEvent, config: &Config, form_state: &mut FormState, command_input: &mut String, command_message: &mut String, app_terminal: &mut AppTerminal, is_saved: &mut bool, current_position: &mut u64, total_count: u64, ) -> Result<(bool, String, bool), Box> { // Return value: (should_exit, message, should_exit_command_mode) // 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)); } } // Ignore all other keys Ok((false, "".to_string(), false)) } async fn process_command( config: &Config, form_state: &mut FormState, command_input: &mut String, command_message: &mut String, app_terminal: &mut AppTerminal, is_saved: &mut bool, current_position: &mut u64, total_count: u64, ) -> Result<(bool, String, bool), Box> { // Clone the trimmed command to avoid borrow issues let command = command_input.trim().to_string(); if command.is_empty() { *command_message = "Empty command".to_string(); return Ok((false, command_message.clone(), false)); } // Get the action for the command (now checks global and common bindings too) let action = config.get_action_for_command(&command) .unwrap_or("unknown"); // For debugging eprintln!("Command: '{}', Action: '{}'", command, action); match action { "save" => { let message = common::save( form_state, app_terminal, is_saved, current_position, total_count, ).await?; command_input.clear(); return Ok((false, message, true)); }, "force_quit" => { let (should_exit, message) = common::force_quit(); command_input.clear(); return Ok((should_exit, message, true)); }, "save_and_quit" => { let (should_exit, message) = common::save_and_quit( form_state, app_terminal, current_position, total_count, ).await?; command_input.clear(); return Ok((should_exit, message, true)); }, "unknown" => { let message = format!("Unknown command: {}", command); command_input.clear(); return Ok((false, message, true)); }, _ => { let message = format!("Unhandled action: {}", action); command_input.clear(); return Ok((false, message, true)); } } }