spliting the config file completely

This commit is contained in:
filipriec
2025-02-28 20:16:31 +01:00
parent 8f7350ea93
commit 9b290e1ad9
5 changed files with 139 additions and 155 deletions

View File

@@ -14,52 +14,16 @@ pub async fn handle_edit_event_internal(
ideal_cursor_column: &mut usize,
command_message: &mut String,
) -> Result<String, Box<dyn std::error::Error>> {
// Try to match against configured action mappings first
let mut key_sequence_tracker = KeySequenceTracker::new(800);
let mut handled_by_config = false;
if key.modifiers.is_empty() {
key_sequence_tracker.add_key(key.code);
let sequence = key_sequence_tracker.get_sequence();
// Try to match the current sequence against all bindings
if let Some(action) = config.matches_key_sequence_generalized(&sequence) {
return execute_edit_action(
action,
form_state,
ideal_cursor_column,
).await;
}
// Check if this might be a prefix of a longer sequence
if config.is_key_sequence_prefix(&sequence) {
// If it's a prefix, wait for more keys
return Ok(command_message.clone());
}
// Since it's not part of a multi-key sequence, check for a direct action
if sequence.len() == 1 && !config.is_key_sequence_prefix(&sequence) {
// Try to handle it as a single key
if let Some(action) = config.get_action_for_key(key.code, key.modifiers) {
return execute_edit_action(
action,
form_state,
ideal_cursor_column,
).await;
}
}
} else {
// If modifiers are pressed, check for direct key bindings
if let Some(action) = config.get_action_for_key(key.code, key.modifiers) {
return execute_edit_action(
action,
form_state,
ideal_cursor_column,
).await;
}
// Try to match against configured Edit mode action mappings first
if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) {
return execute_edit_action(
action,
form_state,
ideal_cursor_column,
).await;
}
// If we get here, no key mapping was found, so we handle fallback behavior
// If no Edit mode action is found, handle fallback behavior
handle_edit_specific_input(
key,
form_state,
@@ -69,39 +33,6 @@ pub async fn handle_edit_event_internal(
Ok(command_message.clone())
}
// Original handle_edit_event for backward compatibility - not actually used anymore
// but kept for API compatibility
pub async fn handle_edit_event(
key: KeyEvent,
config: &Config,
form_state: &mut FormState,
_is_edit_mode: &mut bool,
edit_mode_cooldown: &mut bool,
ideal_cursor_column: &mut usize,
command_message: &mut String,
_command_mode: &mut bool,
_command_input: &mut String,
_app_terminal: &mut AppTerminal,
_is_saved: &mut bool,
_current_position: &mut u64,
_total_count: u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
// This function should ideally not be called anymore - everything should go through event.rs
// Log a warning that this code path is deprecated
eprintln!("Warning: Deprecated code path in handle_edit_event - use event.rs instead");
let result = handle_edit_event_internal(
key,
config,
form_state,
ideal_cursor_column,
command_message,
).await?;
*edit_mode_cooldown = false;
Ok((false, result))
}
// Handle edit-specific key input as a fallback (character input, backspace, delete)
fn handle_edit_specific_input(
key: KeyEvent,

View File

@@ -78,7 +78,7 @@ impl EventHandler {
// Mode transitions between edit mode and read-only mode
if self.is_edit_mode {
// Check for exiting edit mode
if config.is_exit_edit_mode(key.code, key.modifiers) {
if config.get_edit_action_for_key(key.code, key.modifiers) == Some("exit_edit_mode") {
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()));
@@ -109,8 +109,8 @@ impl EventHandler {
return Ok((false, result));
} else {
// Check for entering edit mode from read-only mode
if config.is_enter_edit_mode(key.code, key.modifiers) {
if config.is_enter_edit_mode_after(key.code, key.modifiers) {
if config.get_read_only_action_for_key(key.code, key.modifiers) == Some("enter_edit_mode_before") {
if config.get_read_only_action_for_key(key.code, key.modifiers) == Some("enter_edit_mode_after") {
let current_input = form_state.get_current_input();
if !current_input.is_empty() && form_state.current_cursor_pos < current_input.len() {
form_state.current_cursor_pos += 1;

View File

@@ -1,5 +1,3 @@
// src/modes/handlers/read_only.rs
use crossterm::event::{KeyEvent};
use crate::config::config::Config;
use crate::ui::handlers::form::FormState;
@@ -13,7 +11,6 @@ enum CharType {
Punctuation,
}
// Replace your current handle_read_only_event with this generalized version
pub async fn handle_read_only_event(
key: KeyEvent,
config: &Config,
@@ -26,13 +23,30 @@ pub async fn handle_read_only_event(
edit_mode_cooldown: &mut bool,
ideal_cursor_column: &mut usize,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
// Always add the key to the sequence tracker if no modifiers
// This tracks ALL keys, not just character keys
// Check for entering Edit mode from Read-Only mode
if config.get_read_only_action_for_key(key.code, key.modifiers) == Some("enter_edit_mode_before") {
*edit_mode_cooldown = true;
*command_message = "Entering Edit mode".to_string();
return Ok((false, command_message.clone()));
}
if config.get_read_only_action_for_key(key.code, key.modifiers) == Some("enter_edit_mode_after") {
let current_input = form_state.get_current_input();
if !current_input.is_empty() && form_state.current_cursor_pos < current_input.len() {
form_state.current_cursor_pos += 1;
*ideal_cursor_column = form_state.current_cursor_pos;
}
*edit_mode_cooldown = true;
*command_message = "Entering Edit mode (after cursor)".to_string();
return Ok((false, command_message.clone()));
}
// Handle Read-Only mode keybindings
if key.modifiers.is_empty() {
key_sequence_tracker.add_key(key.code);
let sequence = key_sequence_tracker.get_sequence();
// Try to match the current sequence against all bindings
// Try to match the current sequence against Read-Only mode bindings
if let Some(action) = config.matches_key_sequence_generalized(&sequence) {
let result = execute_action(
action,
@@ -50,14 +64,12 @@ pub async fn handle_read_only_event(
// Check if this might be a prefix of a longer sequence
if config.is_key_sequence_prefix(&sequence) {
// If it's a prefix, wait for more keys
return Ok((false, command_message.clone()));
}
// Since it's not part of a multi-key sequence, check for a direct action
if sequence.len() == 1 && !config.is_key_sequence_prefix(&sequence) {
// Try to handle it as a single key
if let Some(action) = config.get_action_for_key(key.code, key.modifiers) {
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
let result = execute_action(
action,
form_state,
@@ -76,7 +88,7 @@ pub async fn handle_read_only_event(
// If modifiers are pressed, check for direct key bindings
key_sequence_tracker.reset();
if let Some(action) = config.get_action_for_key(key.code, key.modifiers) {
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
let result = execute_action(
action,
form_state,
@@ -94,7 +106,7 @@ pub async fn handle_read_only_event(
// Show a helpful message when no binding was found
if !*edit_mode_cooldown {
let default_key = "i".to_string();
let edit_key = config.keybindings.get("enter_edit_mode_before")
let edit_key = config.keybindings.read_only.get("enter_edit_mode_before")
.and_then(|keys| keys.first())
.unwrap_or(&default_key);
*command_message = format!("Read-only mode - press {} to edit", edit_key);