moved to common modes

This commit is contained in:
filipriec
2025-03-23 22:14:24 +01:00
parent 2cf4cd6748
commit 78e8cce08b
6 changed files with 6 additions and 2 deletions

View File

@@ -1,134 +0,0 @@
// src/modes/handlers/command_mode.rs
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
use crate::tui::terminal::grpc_client::GrpcClient;
use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState;
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,
is_saved: &mut bool,
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,
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,
grpc_client: &mut GrpcClient,
is_saved: &mut bool,
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 {
"save" => {
let message = common::save(
form_state,
grpc_client,
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,
grpc_client,
current_position,
total_count,
).await?;
command_input.clear();
return Ok((should_exit, 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));
}
}
}

View File

@@ -10,7 +10,7 @@ use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState;
use crate::ui::handlers::rat_state::UiStateHandler;
use crate::modes::{
handlers::{command_mode},
common::{command_mode},
canvas::{edit, read_only, common},
};
use crate::modes::navigation;