command mode splitted

This commit is contained in:
filipriec
2025-02-27 11:10:40 +01:00
parent 5f41e9aa6f
commit 6fd46de89f
4 changed files with 134 additions and 87 deletions

View File

@@ -1,3 +1,4 @@
// src/client/modes/handlers.rs
pub mod event;
pub mod edit;
pub mod command_mode;

View File

@@ -0,0 +1,113 @@
// src/modes/handlers/command_mode.rs
use crossterm::event::{KeyEvent, KeyCode};
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<dyn std::error::Error>> {
// Return value: (should_exit, message, should_exit_command_mode)
match key.code {
KeyCode::Enter => {
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));
}
}
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));
}
}
}

View File

@@ -3,8 +3,8 @@
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
use crate::tui::terminal::AppTerminal;
use crate::config::config::Config;
use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};
use crate::ui::handlers::form::FormState;
use crate::modes::handlers::command_mode::handle_command_event;
pub async fn handle_edit_event(
key: KeyEvent,
@@ -22,92 +22,25 @@ pub async fn handle_edit_event(
total_count: u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if *command_mode {
match key.code {
KeyCode::Enter => {
let command = command_input.trim();
if command.is_empty() {
*command_message = "Empty command".to_string();
return Ok((false, command_message.clone()));
}
// Delegate to the command mode handler
let (should_exit, message, exit_command_mode) = handle_command_event(
key,
config,
form_state,
command_input,
command_message,
app_terminal,
is_saved,
current_position,
total_count,
).await?;
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();
if exit_command_mode {
*command_mode = false;
command_message.clear();
return Ok((false, message));
} else {
let (should_exit, message) = app_terminal
.handle_command(action, is_saved)
.await?;
*command_message = message;
command_input.clear();
*command_mode = false;
return Ok((should_exit, command_message.clone()));
}
}
KeyCode::Char(c) => command_input.push(c),
KeyCode::Backspace => {
command_input.pop();
}
KeyCode::Esc => {
*command_mode = false;
command_input.clear();
command_message.clear();
}
_ => {}
if !message.is_empty() {
return Ok((should_exit, message));
}
} else {
match key.code {