generalized command mode

This commit is contained in:
filipriec
2025-02-28 15:22:50 +01:00
parent 43e86abf1f
commit bd83fb47e1
2 changed files with 116 additions and 78 deletions

View File

@@ -7,7 +7,6 @@ force_quit = [":q!", "ctrl+shift+q"]
save_and_quit = [":wq", "ctrl+shift+s"]
enter_command_mode = [":", "ctrl+;"]
exit_command_mode = ["ctrl+g", "esc"]
# MODE SPECIFIC
# READ ONLY MODE
@@ -36,6 +35,11 @@ delete_char_backward = ["backspace"]
next_field = ["tab", "enter"]
prev_field = ["shift+tab", "backtab"]
# COMMAND MODE
command_execute = ["enter"]
command_backspace = ["backspace"]
exit_command_mode = ["ctrl+g", "esc"]
[colors]
theme = "dark"
# Options: "light", "dark", "high_contrast"

View File

@@ -1,9 +1,10 @@
// src/modes/handlers/command_mode.rs
use crossterm::event::{KeyEvent, KeyCode};
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(
@@ -19,8 +20,60 @@ pub async fn handle_command_event(
) -> Result<(bool, String, bool), Box<dyn std::error::Error>> {
// 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
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
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<dyn std::error::Error>> {
let command = command_input.trim();
if command.is_empty() {
*command_message = "Empty command".to_string();
@@ -91,23 +144,4 @@ pub async fn handle_command_event(
command_input.clear();
return Ok((should_exit, command_message.clone(), true));
}
}
KeyCode::Char(c) => {
command_input.push(c);
return Ok((false, "".to_string(), false));
}
KeyCode::Backspace => {
command_input.pop();
return Ok((false, "".to_string(), false));
}
KeyCode::Esc => {
command_input.clear();
*command_message = "".to_string();
return Ok((false, "".to_string(), true));
}
_ => {
// Ignore other keys
return Ok((false, "".to_string(), false));
}
}
}