jk in a rows instead of columns

This commit is contained in:
filipriec
2025-02-21 00:08:07 +01:00
parent 946d643774
commit d105d06230

View File

@@ -45,10 +45,8 @@ impl EventHandler {
self.edit_mode_cooldown = true;
self.command_message = "Edit mode".to_string();
app_terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
// Don't change cursor position - current_cursor_pos should be kept from read-only mode
return Ok((false, self.command_message.clone()));
} else if self.is_edit_mode && config.is_exit_edit_mode(key.code, key.modifiers) {
// Prevent exiting edit mode if there are unsaved changes
if form_state.has_unsaved_changes {
self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string();
return Ok((false, self.command_message.clone()));
@@ -61,7 +59,7 @@ impl EventHandler {
}
if !self.is_edit_mode {
// Check if key is for navigation
// Handle navigation between entries
if key.code == KeyCode::Left {
let new_position = current_position.saturating_sub(1);
if new_position >= 1 {
@@ -98,11 +96,9 @@ impl EventHandler {
return Ok((false, self.command_message.clone()));
}
} else if key.code == KeyCode::Right {
// Allow navigation to total_count + 1 for new entries
if *current_position <= total_count {
*current_position += 1;
// Only load data if position is valid
if *current_position <= total_count {
match app_terminal.get_adresar_by_position(*current_position).await {
Ok(response) => {
@@ -143,7 +139,7 @@ impl EventHandler {
return Ok((false, self.command_message.clone()));
}
} else {
// Check for movement keybindings
// Handle movement keybindings
if let Some(action) = config.get_action_for_key(key.code, key.modifiers) {
match action {
"move_left" => {
@@ -317,22 +313,18 @@ impl EventHandler {
// Handle arrow keys in edit mode
match key.code {
KeyCode::Left => {
// Move cursor left
form_state.current_cursor_pos = form_state.current_cursor_pos.saturating_sub(1);
// Update ideal position to match actual position
self.ideal_cursor_column = form_state.current_cursor_pos;
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;
// Update ideal position to match actual position
self.ideal_cursor_column = form_state.current_cursor_pos;
}
return Ok((false, "".to_string()));
},
}
KeyCode::Char(':') => {
self.command_mode = true;
self.command_input.clear();
@@ -352,31 +344,21 @@ impl EventHandler {
}
KeyCode::Down => {
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
// Get the new field's content and set cursor using ideal position
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
},
// Fix Up key handling
}
KeyCode::Up => {
if form_state.current_field == 0 {
// Wrap to the last field when at the top
form_state.current_field = form_state.fields.len() - 1;
} else {
form_state.current_field = form_state.current_field.saturating_sub(1);
}
// Get the new field's content and set cursor using ideal position
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
},
// Fix Tab key handling
}
KeyCode::Tab => {
if key.modifiers.contains(KeyModifiers::SHIFT) {
if form_state.current_field == 0 {
// Wrap to the last field when at the top
form_state.current_field = form_state.fields.len() - 1;
} else {
form_state.current_field = form_state.current_field.saturating_sub(1);
@@ -384,93 +366,58 @@ impl EventHandler {
} else {
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
}
// Use ideal column position instead of saved_cursor_column
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
},
// Fix BackTab key handling
}
KeyCode::BackTab => {
if form_state.current_field == 0 {
// Wrap to the last field when at the top
form_state.current_field = form_state.fields.len() - 1;
} else {
form_state.current_field = form_state.current_field.saturating_sub(1);
}
// Use ideal column position instead of saved_cursor_column
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
},
// Fix Enter key handling
}
KeyCode::Enter => {
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
// Use ideal column position instead of saved_cursor_column
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
},
}
KeyCode::Char(c) => {
// 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();
// Insert character at cursor position instead of just appending
let mut chars: Vec<char> = field_value.chars().collect();
if cursor_pos <= chars.len() {
chars.insert(cursor_pos, c);
*field_value = chars.into_iter().collect();
// Move cursor forward after updating the field
form_state.current_cursor_pos = cursor_pos + 1;
// Update ideal cursor position too
self.ideal_cursor_column = form_state.current_cursor_pos;
form_state.has_unsaved_changes = true; // Mark as unsaved
form_state.has_unsaved_changes = true;
}
},
}
KeyCode::Backspace => {
// Only delete if cursor is not at the beginning
if form_state.current_cursor_pos > 0 {
// 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();
// Remove character at cursor position - 1
let mut chars: Vec<char> = field_value.chars().collect();
if cursor_pos <= chars.len() && cursor_pos > 0 {
chars.remove(cursor_pos - 1);
*field_value = chars.into_iter().collect();
// Update cursor position after modifying the field
form_state.current_cursor_pos = cursor_pos - 1;
// Update ideal cursor position too
self.ideal_cursor_column = form_state.current_cursor_pos;
form_state.has_unsaved_changes = true; // Mark as unsaved
form_state.has_unsaved_changes = true;
}
}
},
// 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
form_state.has_unsaved_changes = true;
}
}
_ => {}