132 lines
4.2 KiB
Rust
132 lines
4.2 KiB
Rust
// src/modes/handlers/command_mode.rs
|
|
|
|
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
|
use crate::config::binds::config::Config;
|
|
use crate::services::grpc_client::GrpcClient;
|
|
use crate::state::pages::form::FormState;
|
|
use crate::tui::controls::commands::CommandHandler;
|
|
use crate::tui::terminal::core::TerminalCore;
|
|
use crate::modes::{
|
|
canvas::{common},
|
|
};
|
|
|
|
pub async fn handle_command_event(
|
|
key: KeyEvent,
|
|
config: &Config,
|
|
form_state: &mut FormState,
|
|
command_input: &mut String,
|
|
command_message: &mut String,
|
|
grpc_client: &mut GrpcClient,
|
|
command_handler: &mut CommandHandler,
|
|
terminal: &mut TerminalCore,
|
|
current_position: &mut u64,
|
|
total_count: u64,
|
|
) -> Result<(bool, String, bool), Box<dyn std::error::Error>> {
|
|
|
|
// 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,
|
|
grpc_client,
|
|
command_handler,
|
|
terminal,
|
|
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,
|
|
grpc_client: &mut GrpcClient,
|
|
command_handler: &mut CommandHandler,
|
|
terminal: &mut TerminalCore,
|
|
current_position: &mut u64,
|
|
total_count: u64,
|
|
) -> Result<(bool, String, bool), Box<dyn std::error::Error>> {
|
|
// 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");
|
|
|
|
match action {
|
|
"force_quit" | "save_and_quit" | "quit" => {
|
|
let (should_exit, message) = command_handler
|
|
.handle_command(action, terminal)
|
|
.await?;
|
|
command_input.clear();
|
|
Ok((should_exit, message, true))
|
|
},
|
|
"save" => {
|
|
let message = common::save(
|
|
form_state,
|
|
grpc_client,
|
|
&mut command_handler.is_saved,
|
|
current_position,
|
|
total_count,
|
|
).await?;
|
|
command_input.clear();
|
|
return Ok((false, message, true));
|
|
},
|
|
"revert" => {
|
|
let message = common::revert(
|
|
form_state,
|
|
grpc_client,
|
|
current_position,
|
|
total_count,
|
|
).await?;
|
|
command_input.clear();
|
|
return Ok((false, 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));
|
|
}
|
|
}
|
|
}
|