From 2e912dd261a2e60ce4536eb0b6c4a715d77b5232 Mon Sep 17 00:00:00 2001 From: filipriec Date: Thu, 3 Apr 2025 22:12:20 +0200 Subject: [PATCH] moved to form_ro --- .../src/functions/modes/read_only/form_ro.rs | 82 +++++++++++++++++- client/src/modes/canvas/read_only.rs | 4 - client/src/tui/functions/form.rs | 86 ------------------- client/src/tui/functions/login.rs | 53 +----------- 4 files changed, 84 insertions(+), 141 deletions(-) diff --git a/client/src/functions/modes/read_only/form_ro.rs b/client/src/functions/modes/read_only/form_ro.rs index 50a6f8a..86d2a48 100644 --- a/client/src/functions/modes/read_only/form_ro.rs +++ b/client/src/functions/modes/read_only/form_ro.rs @@ -19,14 +19,92 @@ pub async fn execute_action( command_message: &mut String, ) -> Result> { match action { - "previous_entry" | "next_entry" | "move_up" | "move_down" | - "move_first_line" | "move_last_line" => { + "previous_entry" | "next_entry" => { key_sequence_tracker.reset(); Ok(format!( "Action '{}' should be handled by context-specific logic", action )) } + "move_up" => { + key_sequence_tracker.reset(); + let num_fields = state.fields().len(); + if num_fields == 0 { + return Ok("No fields to navigate.".to_string()); + } + let current_field = state.current_field(); + let new_field = if current_field == 0 { + num_fields - 1 + } else { + current_field - 1 + }; + state.set_current_field(new_field); + let current_input = state.get_current_input(); + let max_cursor_pos = if current_input.is_empty() { + 0 + } else { + current_input.len().saturating_sub(1) + }; + let new_pos = (*ideal_cursor_column).min(max_cursor_pos); + state.set_current_cursor_pos(new_pos); + Ok("".to_string()) + } + "move_down" => { + key_sequence_tracker.reset(); + let num_fields = state.fields().len(); + if num_fields == 0 { + return Ok("No fields to navigate.".to_string()); + } + let current_field = state.current_field(); + let new_field = (current_field + 1) % num_fields; + state.set_current_field(new_field); + let current_input = state.get_current_input(); + let max_cursor_pos = if current_input.is_empty() { + 0 + } else { + current_input.len().saturating_sub(1) + }; + let new_pos = (*ideal_cursor_column).min(max_cursor_pos); + state.set_current_cursor_pos(new_pos); + Ok("".to_string()) + } + "move_first_line" => { + key_sequence_tracker.reset(); + let num_fields = state.fields().len(); + if num_fields == 0 { + return Ok("No fields to navigate to.".to_string()); + } + state.set_current_field(0); + let current_input = state.get_current_input(); + let max_cursor_pos = if current_input.is_empty() { + 0 + } else { + current_input.len().saturating_sub(1) + }; + let new_pos = (*ideal_cursor_column).min(max_cursor_pos); + state.set_current_cursor_pos(new_pos); + *ideal_cursor_column = new_pos; + Ok("".to_string()) + } + "move_last_line" => { + key_sequence_tracker.reset(); + let num_fields = state.fields().len(); + if num_fields == 0 { + return Ok("No fields to navigate to.".to_string()); + } + let last_field_index = num_fields - 1; + state.set_current_field(last_field_index); + let current_input = state.get_current_input(); + let max_cursor_pos = if current_input.is_empty() { + 0 + } else { + current_input.len().saturating_sub(1) + }; + let new_pos = (*ideal_cursor_column).min(max_cursor_pos); + state.set_current_cursor_pos(new_pos); + *ideal_cursor_column = new_pos; + Ok("".to_string()) + } "exit_edit_mode" => { key_sequence_tracker.reset(); command_message.clear(); diff --git a/client/src/modes/canvas/read_only.rs b/client/src/modes/canvas/read_only.rs index 51130a4..ac8d871 100644 --- a/client/src/modes/canvas/read_only.rs +++ b/client/src/modes/canvas/read_only.rs @@ -59,10 +59,6 @@ pub async fn handle_read_only_event( const CONTEXT_ACTIONS_FORM: &[&str] = &[ "previous_entry", "next_entry", - "move_up", - "move_down", - "move_first_line", - "move_last_line", ]; const CONTEXT_ACTIONS_LOGIN: &[&str] = &[ "move_up", diff --git a/client/src/tui/functions/form.rs b/client/src/tui/functions/form.rs index db8d8c2..c1e1fda 100644 --- a/client/src/tui/functions/form.rs +++ b/client/src/tui/functions/form.rs @@ -75,92 +75,6 @@ pub async fn handle_action( Ok("Already at last entry".into()) } } - "move_first_line" => { - // *** GUARD CLAUSE *** - if form_state.fields.is_empty() { - // Or log an error, or do nothing gracefully - return Ok("No fields to navigate to.".to_string()); - } - - // Set the field index - form_state.set_current_field(0); - - // Get input of the *new* current field (index 0) - let current_input = form_state.get_current_input(); - - // Calculate the maximum valid cursor position for read-only mode - // Cursor should be ON the last char, or at 0 if empty. - let max_cursor_pos = if current_input.is_empty() { - 0 - } else { - current_input.len().saturating_sub(1) - }; - - // Set the cursor position, clamped by ideal column and max valid position - form_state.set_current_cursor_pos((*ideal_cursor_column).min(max_cursor_pos)); - - Ok("".to_string()) // Or a confirmation message - } - - "move_last_line" => { - // *** GUARD CLAUSE *** - if form_state.fields.is_empty() { - return Ok("No fields to navigate to.".to_string()); - } - - let last_field_index = form_state.fields.len() - 1; - form_state.set_current_field(last_field_index); - - let current_input = form_state.get_current_input(); - let max_cursor_pos = if current_input.is_empty() { - 0 - } else { - current_input.len().saturating_sub(1) - }; - form_state.set_current_cursor_pos((*ideal_cursor_column).min(max_cursor_pos)); - - Ok("".to_string()) - } - - "move_up" => { - if form_state.fields.is_empty() { - return Ok("No fields to navigate.".to_string()); - } - let current_field = form_state.current_field(); - let new_field = if current_field == 0 { - form_state.fields.len() - 1 - } else { - current_field - 1 - }; - form_state.set_current_field(new_field); - - let current_input = form_state.get_current_input(); - let max_cursor_pos = if current_input.is_empty() { - 0 - } else { - current_input.len().saturating_sub(1) // Adjust for read-only if needed - }; - form_state.set_current_cursor_pos((*ideal_cursor_column).min(max_cursor_pos)); - Ok("".to_string()) - } - - "move_down" => { - if form_state.fields.is_empty() { - return Ok("No fields to navigate.".to_string()); - } - let current_field = form_state.current_field(); - let new_field = (current_field + 1) % form_state.fields.len(); - form_state.set_current_field(new_field); - - let current_input = form_state.get_current_input(); - let max_cursor_pos = if current_input.is_empty() { - 0 - } else { - current_input.len().saturating_sub(1) // Adjust for read-only if needed - }; - form_state.set_current_cursor_pos((*ideal_cursor_column).min(max_cursor_pos)); - Ok("".to_string()) - } _ => Err("Unknown form action".into()) } } diff --git a/client/src/tui/functions/login.rs b/client/src/tui/functions/login.rs index bf8ff8b..31b1e16 100644 --- a/client/src/tui/functions/login.rs +++ b/client/src/tui/functions/login.rs @@ -8,56 +8,11 @@ pub async fn handle_action( ideal_cursor_column: &mut usize, ) -> Result> { match action { - "move_up" => { - if auth_state.return_selected { - // From Return button to last field (password) - auth_state.return_selected = false; - auth_state.current_field = 1; - } else if auth_state.current_field == 1 { - // Password -> Username - auth_state.current_field = 0; - } else if auth_state.current_field == 0 { - // Username -> Password (wrap around fields only) - auth_state.current_field = 1; - } else if auth_state.current_field == 2 { - // From Login button to Password field - auth_state.current_field = 1; - } - - // Update cursor position only when in a field - if auth_state.current_field < 2 { - let current_input = auth_state.get_current_input(); - let max_cursor_pos = current_input.len(); - auth_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos); - } - - Ok(format!("Navigation 'up' from functions/login")) + "previous_entry" => { + Ok("Previous entry at tui/functions/login.rs not implemented".into()) } - "move_down" => { - if auth_state.return_selected { - // From Return button to first field (username) - auth_state.return_selected = false; - auth_state.current_field = 0; - } else if auth_state.current_field == 0 { - // Username -> Password - auth_state.current_field = 1; - } else if auth_state.current_field == 1 { - // Password -> Login button - auth_state.current_field = 2; - auth_state.return_selected = false; - } else if auth_state.current_field == 2 { - // Login button -> Return button - auth_state.return_selected = true; - } - - // Update cursor position only when in a field - if auth_state.current_field < 2 { - let current_input = auth_state.get_current_input(); - let max_cursor_pos = current_input.len(); - auth_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos); - } - - Ok(format!("Navigation 'down' from functions/login")) + "next_entry" => { + Ok("Next entry at tui/functions/login.rs not implemented".into()) } _ => Err("Unknown login action".into()) }