working movement within the text

This commit is contained in:
filipriec
2025-02-20 13:30:12 +01:00
parent f4abb7b883
commit 6b58c26009
4 changed files with 163 additions and 115 deletions

View File

@@ -9,7 +9,7 @@ exit_edit_mode = ["esc", "ctrl+e"]
previous_position = ["Left", "9"] previous_position = ["Left", "9"]
next_position = ["Right", "8"] next_position = ["Right", "8"]
# move_left = ["h", "Left"] move_left = ["h"]
# move_right = ["l", "Right"] move_right = ["l"]
# move_up = ["k", "Up"] # move_up = ["k", "Up"]
# move_down = ["j", "Down"] # move_down = ["j", "Down"]

View File

@@ -128,8 +128,8 @@ pub fn render_form(
} }
if is_active { if is_active {
if is_edit_mode { if is_edit_mode {
// Edit mode: cursor at the end // Edit mode: cursor at current_cursor_pos instead of end
let cursor_x = input_rows[i].x + input.len() as u16; let cursor_x = input_rows[i].x + form_state.current_cursor_pos as u16;
let cursor_y = input_rows[i].y; let cursor_y = input_rows[i].y;
f.set_cursor(cursor_x, cursor_y); f.set_cursor(cursor_x, cursor_y);
} else { } else {

View File

@@ -41,6 +41,9 @@ impl EventHandler {
self.edit_mode_cooldown = true; self.edit_mode_cooldown = true;
self.command_message = "Edit mode".to_string(); self.command_message = "Edit mode".to_string();
app_terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?; // Add this line app_terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?; // Add this line
// Initialize cursor position when entering edit mode
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = current_input.len();
return Ok((false, self.command_message.clone())); return Ok((false, self.command_message.clone()));
} else if self.is_edit_mode && config.is_exit_edit_mode(key.code, key.modifiers) { } else if self.is_edit_mode && config.is_exit_edit_mode(key.code, key.modifiers) {
// Prevent exiting edit mode if there are unsaved changes // Prevent exiting edit mode if there are unsaved changes
@@ -193,7 +196,7 @@ impl EventHandler {
} }
} }
} else { } else {
// Existing edit mode handling // Edit mode handling
if self.command_mode { if self.command_mode {
match key.code { match key.code {
KeyCode::Enter => { KeyCode::Enter => {
@@ -304,33 +307,21 @@ impl EventHandler {
_ => {} _ => {}
} }
} else { } else {
// Check for keybindings // Handle arrow keys in edit mode
if let Some(action) = config.get_action_for_key(key.code, key.modifiers) {
let form_data = PostAdresarRequest {
firma: form_state.firma.clone(),
kz: form_state.kz.clone(),
drc: form_state.drc.clone(),
ulica: form_state.ulica.clone(),
psc: form_state.psc.clone(),
mesto: form_state.mesto.clone(),
stat: form_state.stat.clone(),
banka: form_state.banka.clone(),
ucet: form_state.ucet.clone(),
skladm: form_state.skladm.clone(),
ico: form_state.ico.clone(),
kontakt: form_state.kontakt.clone(),
telefon: form_state.telefon.clone(),
skladu: form_state.skladu.clone(),
fax: form_state.fax.clone(),
};
let (should_exit, message) = app_terminal
.handle_command(action, is_saved, &form_data)
.await?;
self.command_message = message;
return Ok((should_exit, self.command_message.clone()));
} else {
match key.code { match key.code {
KeyCode::Left => {
// Move cursor left
form_state.current_cursor_pos = form_state.current_cursor_pos.saturating_sub(1);
return Ok((false, "".to_string()));
}
KeyCode::Right => {
// Move cursor right
let current_input = form_state.get_current_input();
if form_state.current_cursor_pos < current_input.len() {
form_state.current_cursor_pos += 1;
}
return Ok((false, "".to_string()));
}
KeyCode::Char(':') => { KeyCode::Char(':') => {
self.command_mode = true; self.command_mode = true;
self.command_input.clear(); self.command_input.clear();
@@ -347,6 +338,9 @@ impl EventHandler {
} else { } else {
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len(); form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
} }
// Reset cursor position to end of field when changing fields
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = current_input.len();
} }
KeyCode::Esc => { KeyCode::Esc => {
if config.is_exit_edit_mode(key.code, key.modifiers) { if config.is_exit_edit_mode(key.code, key.modifiers) {
@@ -367,8 +361,16 @@ impl EventHandler {
} else { } else {
form_state.current_field = form_state.current_field.saturating_sub(1); form_state.current_field = form_state.current_field.saturating_sub(1);
} }
// Reset cursor position to end of field when changing fields
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = current_input.len();
},
KeyCode::Down => {
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
// Reset cursor position to end of field when changing fields
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = current_input.len();
}, },
KeyCode::Down => form_state.current_field = (form_state.current_field + 1) % form_state.fields.len(),
KeyCode::Up => { KeyCode::Up => {
if form_state.current_field == 0 { if form_state.current_field == 0 {
// Wrap to the last field when at the top // Wrap to the last field when at the top
@@ -376,56 +378,81 @@ impl EventHandler {
} else { } else {
form_state.current_field = form_state.current_field.saturating_sub(1); form_state.current_field = form_state.current_field.saturating_sub(1);
} }
// Reset cursor position to end of field when changing fields
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = current_input.len();
},
KeyCode::Enter => {
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
// Reset cursor position to end of field when changing fields
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = current_input.len();
}, },
KeyCode::Enter => form_state.current_field = (form_state.current_field + 1) % form_state.fields.len(),
KeyCode::Char(c) => { KeyCode::Char(c) => {
match form_state.current_field { // Save cursor position before mutable borrow
0 => form_state.firma.push(c), let cursor_pos = form_state.current_cursor_pos;
1 => form_state.kz.push(c),
2 => form_state.drc.push(c), // Get the current field value
3 => form_state.ulica.push(c), let field_value = form_state.get_current_input_mut();
4 => form_state.psc.push(c),
5 => form_state.mesto.push(c), // Insert character at cursor position instead of just appending
6 => form_state.stat.push(c), let mut chars: Vec<char> = field_value.chars().collect();
7 => form_state.banka.push(c), if cursor_pos <= chars.len() {
8 => form_state.ucet.push(c), chars.insert(cursor_pos, c);
9 => form_state.skladm.push(c), *field_value = chars.into_iter().collect();
10 => form_state.ico.push(c),
11 => form_state.kontakt.push(c), // Move cursor forward after updating the field
12 => form_state.telefon.push(c), form_state.current_cursor_pos = cursor_pos + 1;
13 => form_state.skladu.push(c),
14 => form_state.fax.push(c),
_ => (),
}
form_state.has_unsaved_changes = true; // Mark as unsaved form_state.has_unsaved_changes = true; // Mark as unsaved
} }
}
// Fix for the Backspace handler
KeyCode::Backspace => { KeyCode::Backspace => {
match form_state.current_field { // Only delete if cursor is not at the beginning
0 => form_state.firma.pop(), if form_state.current_cursor_pos > 0 {
1 => form_state.kz.pop(), // Save cursor position before mutable borrow
2 => form_state.drc.pop(), let cursor_pos = form_state.current_cursor_pos;
3 => form_state.ulica.pop(),
4 => form_state.psc.pop(), // Get the current field value
5 => form_state.mesto.pop(), let field_value = form_state.get_current_input_mut();
6 => form_state.stat.pop(),
7 => form_state.banka.pop(), // Remove character at cursor position - 1
8 => form_state.ucet.pop(), let mut chars: Vec<char> = field_value.chars().collect();
9 => form_state.skladm.pop(), if cursor_pos <= chars.len() && cursor_pos > 0 {
10 => form_state.ico.pop(), chars.remove(cursor_pos - 1);
11 => form_state.kontakt.pop(), *field_value = chars.into_iter().collect();
12 => form_state.telefon.pop(),
13 => form_state.skladu.pop(), // Update cursor position after modifying the field
14 => form_state.fax.pop(), form_state.current_cursor_pos = cursor_pos - 1;
_ => None,
};
form_state.has_unsaved_changes = true; // Mark as unsaved form_state.has_unsaved_changes = true; // Mark as unsaved
} }
}
}
// Fix for the Delete handler
KeyCode::Delete => {
// Save cursor position before mutable borrow
let cursor_pos = form_state.current_cursor_pos;
// Get the current field value
let field_value = form_state.get_current_input_mut();
// Check if there's a character at cursor position to delete
let chars: Vec<char> = field_value.chars().collect();
if cursor_pos < chars.len() {
let mut new_chars = chars.clone();
new_chars.remove(cursor_pos);
*field_value = new_chars.into_iter().collect();
form_state.has_unsaved_changes = true; // Mark as unsaved
// Cursor position doesn't change with delete
}
}
_ => {} _ => {}
} }
} }
} }
} }
}
self.edit_mode_cooldown = false; self.edit_mode_cooldown = false;
Ok((false, self.command_message.clone())) Ok((false, self.command_message.clone()))

View File

@@ -117,4 +117,25 @@ impl FormState {
_ => "", _ => "",
} }
} }
pub fn get_current_input_mut(&mut self) -> &mut String {
match self.current_field {
0 => &mut self.firma,
1 => &mut self.kz,
2 => &mut self.drc,
3 => &mut self.ulica,
4 => &mut self.psc,
5 => &mut self.mesto,
6 => &mut self.stat,
7 => &mut self.banka,
8 => &mut self.ucet,
9 => &mut self.skladm,
10 => &mut self.ico,
11 => &mut self.kontakt,
12 => &mut self.telefon,
13 => &mut self.skladu,
14 => &mut self.fax,
_ => &mut self.firma, // Default to first field
}
}
} }