From 1a55f9c066cf25e02efcfaf9215fbd405500e75a Mon Sep 17 00:00:00 2001 From: filipriec Date: Fri, 21 Mar 2025 12:52:33 +0100 Subject: [PATCH] more fixes --- client/src/modes/handlers/command_mode.rs | 17 +++++------------ client/src/modes/handlers/common.rs | 19 +++++++++---------- client/src/modes/handlers/edit.rs | 20 +++++--------------- client/src/modes/handlers/event.rs | 21 +++------------------ client/src/modes/handlers/read_only.rs | 16 ++++++++-------- 5 files changed, 30 insertions(+), 63 deletions(-) diff --git a/client/src/modes/handlers/command_mode.rs b/client/src/modes/handlers/command_mode.rs index 05b6763..eefebd9 100644 --- a/client/src/modes/handlers/command_mode.rs +++ b/client/src/modes/handlers/command_mode.rs @@ -1,11 +1,7 @@ // src/modes/handlers/command_mode.rs use crossterm::event::{KeyEvent, KeyCode, KeyModifiers}; -use crate::tui::terminal::{ - core::TerminalCore, - grpc_client::GrpcClient, - commands::CommandHandler, -}; +use crate::tui::terminal::grpc_client::GrpcClient; use crate::config::config::Config; use crate::ui::handlers::form::FormState; use super::common; @@ -38,7 +34,7 @@ pub async fn handle_command_event( form_state, command_input, command_message, - app_terminal, + grpc_client, is_saved, current_position, total_count, @@ -85,14 +81,11 @@ async fn process_command( let action = config.get_action_for_command(&command) .unwrap_or("unknown"); - // TODO remove in the future this comment. For debugging to instantly output action - // eprintln!("Command: '{}', Action: '{}'", command, action); - match action { "save" => { let message = common::save( form_state, - app_terminal, + grpc_client, is_saved, current_position, total_count, @@ -108,7 +101,7 @@ async fn process_command( "save_and_quit" => { let (should_exit, message) = common::save_and_quit( form_state, - app_terminal, + grpc_client, current_position, total_count, ).await?; @@ -118,7 +111,7 @@ async fn process_command( "revert" => { let message = common::revert( form_state, - app_terminal, + grpc_client, current_position, total_count, ).await?; diff --git a/client/src/modes/handlers/common.rs b/client/src/modes/handlers/common.rs index 40555c9..06215d7 100644 --- a/client/src/modes/handlers/common.rs +++ b/client/src/modes/handlers/common.rs @@ -3,7 +3,6 @@ use crate::tui::terminal::{ core::TerminalCore, grpc_client::GrpcClient, - commands::CommandHandler, }; use crate::ui::handlers::form::FormState; use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest}; @@ -11,7 +10,7 @@ use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest}; /// Shared logic for saving the current form state pub async fn save( form_state: &mut FormState, - app_terminal: &mut AppTerminal, + grpc_client: &mut GrpcClient, is_saved: &mut bool, current_position: &mut u64, total_count: u64, @@ -36,8 +35,8 @@ pub async fn save( 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?; + let response = grpc_client.post_adresar(post_request).await?; + let new_total = grpc_client.get_adresar_count().await?; *current_position = new_total; form_state.id = response.into_inner().id; "New entry created".to_string() @@ -60,7 +59,7 @@ pub async fn save( skladu: form_state.values[13].clone(), fax: form_state.values[14].clone(), }; - let _ = app_terminal.put_adresar(put_request).await?; + let _ = grpc_client.put_adresar(put_request).await?; "Entry updated".to_string() }; @@ -77,7 +76,7 @@ pub fn force_quit() -> (bool, String) { /// Shared logic for saving and quitting pub async fn save_and_quit( form_state: &mut FormState, - app_terminal: &mut AppTerminal, + grpc_client: &mut GrpcClient, current_position: &mut u64, total_count: u64, ) -> Result<(bool, String), Box> { @@ -101,7 +100,7 @@ pub async fn save_and_quit( skladu: form_state.values[13].clone(), fax: form_state.values[14].clone(), }; - let _ = app_terminal.post_adresar(post_request).await?; + let _ = grpc_client.post_adresar(post_request).await?; } else { let put_request = PutAdresarRequest { id: form_state.id, @@ -121,7 +120,7 @@ pub async fn save_and_quit( skladu: form_state.values[13].clone(), fax: form_state.values[14].clone(), }; - let _ = app_terminal.put_adresar(put_request).await?; + let _ = grpc_client.put_adresar(put_request).await?; } Ok((true, "Saved and exiting application".to_string())) @@ -130,7 +129,7 @@ pub async fn save_and_quit( /// Discard changes since last save pub async fn revert( form_state: &mut FormState, - app_terminal: &mut AppTerminal, + grpc_client: &mut GrpcClient, current_position: &mut u64, total_count: u64, ) -> Result> { @@ -143,7 +142,7 @@ pub async fn revert( return Ok("New entry cleared".to_string()); } - let data = app_terminal.get_adresar_by_position(*current_position).await?; + let data = grpc_client.get_adresar_by_position(*current_position).await?; // Update form fields with saved values form_state.values = vec![ diff --git a/client/src/modes/handlers/edit.rs b/client/src/modes/handlers/edit.rs index 0013415..0500c75 100644 --- a/client/src/modes/handlers/edit.rs +++ b/client/src/modes/handlers/edit.rs @@ -4,13 +4,11 @@ use crossterm::event::{KeyEvent, KeyCode, KeyModifiers}; use crate::tui::terminal::{ core::TerminalCore, grpc_client::GrpcClient, - commands::CommandHandler, }; use crate::config::config::Config; use crate::ui::handlers::form::FormState; use super::common; -// This function is called from event.rs and doesn't need to handle mode transitions anymore pub async fn handle_edit_event_internal( key: KeyEvent, config: &Config, @@ -21,20 +19,20 @@ pub async fn handle_edit_event_internal( is_saved: &mut bool, current_position: &mut u64, total_count: u64, + grpc_client: &mut GrpcClient, // Changed from AppTerminal ) -> Result> { if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) { return execute_edit_action( action, form_state, ideal_cursor_column, - app_terminal, // Pass them here + grpc_client, // Changed from AppTerminal is_saved, current_position, total_count, ).await; } - // If no Edit mode action is found, handle fallback behavior handle_edit_specific_input( key, form_state, @@ -44,16 +42,13 @@ pub async fn handle_edit_event_internal( Ok(command_message.clone()) } -// Handle edit-specific key input as a fallback (character input, backspace, delete) fn handle_edit_specific_input( key: KeyEvent, form_state: &mut FormState, ideal_cursor_column: &mut usize, ) { - // This is now explicitly a fallback function for default edit behavior match key.code { KeyCode::Char(c) => { - // Character input let cursor_pos = form_state.current_cursor_pos; let field_value = form_state.get_current_input_mut(); let mut chars: Vec = field_value.chars().collect(); @@ -66,7 +61,6 @@ fn handle_edit_specific_input( } } KeyCode::Backspace => { - // Delete character backward if form_state.current_cursor_pos > 0 { let cursor_pos = form_state.current_cursor_pos; let field_value = form_state.get_current_input_mut(); @@ -81,7 +75,6 @@ fn handle_edit_specific_input( } } KeyCode::Delete => { - // Delete character forward let cursor_pos = form_state.current_cursor_pos; let field_value = form_state.get_current_input_mut(); let chars: Vec = field_value.chars().collect(); @@ -93,7 +86,6 @@ fn handle_edit_specific_input( } } KeyCode::Tab => { - // Tab key special handling if key.modifiers.contains(KeyModifiers::SHIFT) { if form_state.current_field == 0 { form_state.current_field = form_state.fields.len() - 1; @@ -108,13 +100,12 @@ fn handle_edit_specific_input( form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos); } KeyCode::Enter => { - // Enter key moves to next field form_state.current_field = (form_state.current_field + 1) % form_state.fields.len(); let current_input = form_state.get_current_input(); let max_cursor_pos = current_input.len(); form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos); } - _ => {} // Ignore other keys + _ => {} } } @@ -122,7 +113,7 @@ async fn execute_edit_action( action: &str, form_state: &mut FormState, ideal_cursor_column: &mut usize, - app_terminal: &mut AppTerminal, + grpc_client: &mut GrpcClient, // Changed from AppTerminal is_saved: &mut bool, current_position: &mut u64, total_count: u64, @@ -131,13 +122,12 @@ async fn execute_edit_action( "save" => { common::save( form_state, - app_terminal, + grpc_client, // Changed from AppTerminal is_saved, current_position, total_count, ).await }, - // Navigation actions "move_left" => { form_state.current_cursor_pos = form_state.current_cursor_pos.saturating_sub(1); *ideal_cursor_column = form_state.current_cursor_pos; diff --git a/client/src/modes/handlers/event.rs b/client/src/modes/handlers/event.rs index db07859..79ae0fc 100644 --- a/client/src/modes/handlers/event.rs +++ b/client/src/modes/handlers/event.rs @@ -86,7 +86,6 @@ impl EventHandler { } } - // Handle command mode with highest priority if self.command_mode { let (should_exit, message, exit_command_mode) = command_mode::handle_command_event( key, @@ -104,19 +103,10 @@ impl EventHandler { self.command_mode = false; } - if !message.is_empty() || should_exit { - return Ok((should_exit, message)); - } - - return Ok((false, String::new())); + return Ok((should_exit, message)); } - // Mode transitions and mode-specific handling if self.is_edit_mode { - // When in EDIT mode, we DON'T want to check for entering command mode - // as ':' should just be a normal character input - - // Check for exiting edit mode if config.is_exit_edit_mode(key.code, key.modifiers) { if form_state.has_unsaved_changes { self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string(); @@ -135,7 +125,6 @@ impl EventHandler { return Ok((false, self.command_message.clone())); } - // Handle edit mode events let result = edit::handle_edit_event_internal( key, config, @@ -143,17 +132,15 @@ impl EventHandler { &mut self.ideal_cursor_column, &mut self.command_message, terminal, - grpc_client, is_saved, current_position, total_count, + grpc_client, // Moved to end to match parameter order ).await?; self.key_sequence_tracker.reset(); return Ok((false, result)); } else { - // In READ-ONLY mode, we DO want to check for entering command mode - // Check for entering command mode (only in read-only mode) if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) { if action == "enter_command_mode" { self.command_mode = true; @@ -163,7 +150,6 @@ impl EventHandler { } } - // Check for entering edit mode from read-only mode if config.is_enter_edit_mode_before(key.code, key.modifiers) { self.is_edit_mode = true; self.edit_mode_cooldown = true; @@ -185,7 +171,6 @@ impl EventHandler { return Ok((false, self.command_message.clone())); } - // Handle read-only mode events return read_only::handle_read_only_event( key, config, @@ -194,10 +179,10 @@ impl EventHandler { current_position, total_count, terminal, - grpc_client, &mut self.command_message, &mut self.edit_mode_cooldown, &mut self.ideal_cursor_column, + grpc_client, // Moved to end ).await; } } diff --git a/client/src/modes/handlers/read_only.rs b/client/src/modes/handlers/read_only.rs index 3a989f2..a1b2157 100644 --- a/client/src/modes/handlers/read_only.rs +++ b/client/src/modes/handlers/read_only.rs @@ -7,7 +7,6 @@ use crate::config::key_sequences::KeySequenceTracker; use crate::tui::terminal::{ core::TerminalCore, grpc_client::GrpcClient, - commands::CommandHandler, }; #[derive(PartialEq)] @@ -24,7 +23,8 @@ pub async fn handle_read_only_event( key_sequence_tracker: &mut KeySequenceTracker, current_position: &mut u64, total_count: u64, - app_terminal: &mut AppTerminal, + terminal: &mut TerminalCore, + grpc_client: &mut GrpcClient, command_message: &mut String, edit_mode_cooldown: &mut bool, ideal_cursor_column: &mut usize, @@ -62,7 +62,7 @@ pub async fn handle_read_only_event( command_message, current_position, total_count, - app_terminal, + grpc_client, ).await?; key_sequence_tracker.reset(); return Ok((false, result)); @@ -84,7 +84,7 @@ pub async fn handle_read_only_event( command_message, current_position, total_count, - app_terminal, + grpc_client, ).await?; key_sequence_tracker.reset(); return Ok((false, result)); @@ -103,7 +103,7 @@ pub async fn handle_read_only_event( command_message, current_position, total_count, - app_terminal, + grpc_client, ).await?; return Ok((false, result)); } @@ -129,14 +129,14 @@ async fn execute_action( command_message: &mut String, current_position: &mut u64, total_count: u64, - app_terminal: &mut AppTerminal, + grpc_client: &mut GrpcClient, ) -> Result> { match action { "previous_entry" => { let new_position = current_position.saturating_sub(1); if new_position >= 1 { *current_position = new_position; - match app_terminal.get_adresar_by_position(*current_position).await { + match grpc_client.get_adresar_by_position(*current_position).await { Ok(response) => { form_state.id = response.id; form_state.values = vec![ @@ -166,7 +166,7 @@ async fn execute_action( if *current_position <= total_count { *current_position += 1; if *current_position <= total_count { - match app_terminal.get_adresar_by_position(*current_position).await { + match grpc_client.get_adresar_by_position(*current_position).await { Ok(response) => { form_state.id = response.id; form_state.values = vec![