// 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 common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest}; 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> { let command = command_input.trim(); if command.is_empty() { *command_message = "Empty command".to_string(); return Ok((false, command_message.clone(), false)); } let action = config.get_action_for_command(command) .unwrap_or("unknown"); if action == "save" { let is_new = *current_position == total_count + 1; let message = if is_new { let post_request = PostAdresarRequest { firma: form_state.values[0].clone(), kz: form_state.values[1].clone(), drc: form_state.values[2].clone(), ulica: form_state.values[3].clone(), psc: form_state.values[4].clone(), mesto: form_state.values[5].clone(), stat: form_state.values[6].clone(), banka: form_state.values[7].clone(), ucet: form_state.values[8].clone(), skladm: form_state.values[9].clone(), ico: form_state.values[10].clone(), kontakt: form_state.values[11].clone(), telefon: form_state.values[12].clone(), skladu: form_state.values[13].clone(), fax: form_state.values[14].clone(), }; let response = app_terminal.post_adresar(post_request).await?; let new_total = app_terminal.get_adresar_count().await?; *current_position = new_total; form_state.id = response.into_inner().id; "New entry created".to_string() } else { let put_request = PutAdresarRequest { id: form_state.id, firma: form_state.values[0].clone(), kz: form_state.values[1].clone(), drc: form_state.values[2].clone(), ulica: form_state.values[3].clone(), psc: form_state.values[4].clone(), mesto: form_state.values[5].clone(), stat: form_state.values[6].clone(), banka: form_state.values[7].clone(), ucet: form_state.values[8].clone(), skladm: form_state.values[9].clone(), ico: form_state.values[10].clone(), kontakt: form_state.values[11].clone(), telefon: form_state.values[12].clone(), skladu: form_state.values[13].clone(), fax: form_state.values[14].clone(), }; let _ = app_terminal.put_adresar(put_request).await?; "Entry updated".to_string() }; *is_saved = true; form_state.has_unsaved_changes = false; command_input.clear(); return Ok((false, message, true)); } else { let (should_exit, message) = app_terminal .handle_command(action, is_saved) .await?; *command_message = message; command_input.clear(); return Ok((should_exit, command_message.clone(), true)); } }