separated completely modes from each other

This commit is contained in:
filipriec
2025-02-28 18:10:19 +01:00
parent 7403a6399d
commit 940c1abd5e
2 changed files with 116 additions and 190 deletions

View File

@@ -42,27 +42,84 @@ impl EventHandler {
current_position: &mut u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if let Event::Key(key) = event {
// Handle command mode first
// Handle command mode with highest priority
if self.command_mode {
return self.handle_command_mode(
key, config, app_terminal, form_state,
is_saved, current_position, total_count
).await;
let (should_exit, message, exit_command_mode) = command_mode::handle_command_event(
key,
config,
form_state,
&mut self.command_input,
&mut self.command_message,
app_terminal,
is_saved,
current_position,
total_count,
).await?;
if exit_command_mode {
self.command_mode = false;
}
if !message.is_empty() || should_exit {
return Ok((should_exit, message));
}
return Ok((false, String::new()));
}
// Handle mode transitions
if self.handle_mode_transitions(key, config, app_terminal, form_state)? {
return Ok((false, self.command_message.clone()));
// Check for entering command mode from any other mode
if config.is_enter_command_mode(key.code, key.modifiers) {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok((false, String::new()));
}
// Delegate to appropriate mode handler
// Mode transitions between edit mode and read-only mode
if self.is_edit_mode {
return self.handle_edit_mode(
// Check for exiting edit mode
if config.is_exit_edit_mode(key.code, key.modifiers) {
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()));
}
self.is_edit_mode = false;
self.edit_mode_cooldown = true;
self.command_message = "Read-only mode".to_string();
app_terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
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 = current_input.len() - 1;
self.ideal_cursor_column = form_state.current_cursor_pos;
}
return Ok((false, self.command_message.clone()));
}
// Handle edit mode events
return self.handle_edit_mode_internal(
key, config, app_terminal, form_state,
is_saved, current_position, total_count
).await;
} else {
return self.handle_read_only_mode(
// 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) {
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;
self.ideal_cursor_column = form_state.current_cursor_pos;
}
}
self.is_edit_mode = true;
self.edit_mode_cooldown = true;
self.command_message = "Edit mode".to_string();
app_terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
return Ok((false, self.command_message.clone()));
}
// Handle read-only mode events
return self.handle_read_only_mode_internal(
key, config, app_terminal, form_state,
current_position, total_count
).await;
@@ -73,7 +130,7 @@ impl EventHandler {
Ok((false, self.command_message.clone()))
}
async fn handle_command_mode(
async fn handle_edit_mode_internal(
&mut self,
key: KeyEvent,
config: &Config,
@@ -83,120 +140,21 @@ impl EventHandler {
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
// Check for exit_command_mode binding first
if config.is_exit_command_mode(key.code, key.modifiers) {
self.command_mode = false;
self.command_input.clear();
return Ok((false, String::new()));
}
// Existing command mode handling
let (should_exit, message, exit_command_mode) = command_mode::handle_command_event(
// We're calling the edit handler, but we know we're not in command mode
// and we've already handled exit_edit_mode transition above
let result = edit::handle_edit_event_internal(
key,
config,
form_state,
&mut self.command_input,
&mut self.command_message,
app_terminal,
is_saved,
current_position,
total_count,
).await?;
if exit_command_mode {
self.command_mode = false;
}
if !message.is_empty() {
return Ok((should_exit, message));
}
Ok((false, "".to_string()))
}
fn handle_mode_transitions(
&mut self,
key: KeyEvent,
config: &Config,
app_terminal: &mut AppTerminal,
form_state: &mut FormState,
) -> Result<bool, Box<dyn std::error::Error>> {
// Enter command mode
if !self.is_edit_mode && config.is_enter_command_mode(key.code, key.modifiers) {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok(true);
}
// Enter edit mode
if !self.is_edit_mode && config.is_enter_edit_mode(key.code, key.modifiers) {
if config.is_enter_edit_mode_after(key.code, key.modifiers) {
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;
self.ideal_cursor_column = form_state.current_cursor_pos;
}
}
self.is_edit_mode = true;
self.edit_mode_cooldown = true;
self.command_message = "Edit mode".to_string();
app_terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
return Ok(true);
}
// Exit edit mode
if self.is_edit_mode && config.is_exit_edit_mode(key.code, key.modifiers) {
if form_state.has_unsaved_changes {
self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string();
return Ok(true);
}
self.is_edit_mode = false;
self.edit_mode_cooldown = true;
self.command_message = "Read-only mode".to_string();
app_terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
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 = current_input.len() - 1;
self.ideal_cursor_column = form_state.current_cursor_pos;
}
return Ok(true);
}
Ok(false)
}
async fn handle_edit_mode(
&mut self,
key: KeyEvent,
config: &Config,
app_terminal: &mut AppTerminal,
form_state: &mut FormState,
is_saved: &mut bool,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
let result = edit::handle_edit_event(
key,
config,
form_state,
&mut self.is_edit_mode,
&mut self.edit_mode_cooldown,
&mut self.ideal_cursor_column,
&mut self.command_message,
&mut self.command_mode,
&mut self.command_input,
app_terminal,
is_saved,
current_position,
total_count,
).await;
).await?;
self.key_sequence_tracker.reset();
result
Ok((false, result))
}
async fn handle_read_only_mode(
async fn handle_read_only_mode_internal(
&mut self,
key: KeyEvent,
config: &Config,