trying restoring functionality we are not in there correctly yet

This commit is contained in:
filipriec
2025-04-02 21:06:49 +02:00
parent d577ff6715
commit 43edadde0c
2 changed files with 168 additions and 169 deletions

View File

@@ -1,6 +1,7 @@
// src/tui/functions/form.rs
use crate::state::pages::form::FormState;
use crate::services::grpc_client::GrpcClient;
use crate::state::canvas_state::CanvasState;
pub async fn handle_action(
action: &str,
@@ -74,36 +75,90 @@ pub async fn handle_action(
Ok("Already at last entry".into())
}
}
"move_up" => {
// Change field first
if form_state.current_field == 0 {
form_state.current_field = form_state.fields.len() - 1;
} else {
form_state.current_field = form_state.current_field.saturating_sub(1);
"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());
}
// Get current input AFTER changing field
// 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();
let max_cursor_pos = if !current_input.is_empty() {
current_input.len() - 1
} else {
// 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)
};
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
// 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_down" => {
// Change field first
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
// Get current input AFTER changing field
let current_input = form_state.get_current_input();
let max_cursor_pos = if !current_input.is_empty() {
current_input.len() - 1
"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 {
0
current_field - 1
};
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
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())