diff --git a/src/tui/input.rs b/src/tui/input.rs index 2b2325a..8b5a39e 100644 --- a/src/tui/input.rs +++ b/src/tui/input.rs @@ -117,7 +117,7 @@ impl App { self.log_overlay_search.deactivate(); } KeyCode::Backspace => { - self.log_overlay_search.pop(); + self.log_overlay_search.backspace(key_event.modifiers); } KeyCode::Char(c) => { self.log_overlay_search.push(c); @@ -507,7 +507,7 @@ impl App { } KeyCode::Backspace => { if let Some(ref mut name) = self.instances_state.renaming { - name.pop(); + widgets::search::backspace(name, key_event.modifiers); } } KeyCode::Char(c) => { diff --git a/src/tui/tests/widgets/popups/new_instance/state.rs b/src/tui/tests/widgets/popups/new_instance/state.rs index 572b214..4af2ff5 100644 --- a/src/tui/tests/widgets/popups/new_instance/state.rs +++ b/src/tui/tests/widgets/popups/new_instance/state.rs @@ -1,5 +1,5 @@ use crossterm::event::{KeyCode, KeyEvent, KeyModifiers}; -use tui_prompts::TextState; +use tui_prompts::{State as PromptState, TextState}; use super::*; use crate::tests::TEST_LOCK; @@ -70,3 +70,25 @@ fn version_filter_clamps_a_stale_selection() { state.show_snapshots = true; assert_eq!(visible_versions(&state).len(), 2); } + +#[test] +fn ctrl_backspace_deletes_the_word_before_the_name_cursor() { + let _guard = TEST_LOCK.lock().unwrap_or_else(|error| error.into_inner()); + let mut instances = instances::State::default(); + + { + let mut state = WIZARD_STATE.lock().unwrap(); + *state = WizardState::default(); + state.name_state = TextState::new().with_value("hello brave world"); + *state.name_state.position_mut() = "hello brave".chars().count(); + } + + handle_key( + &KeyEvent::new(KeyCode::Backspace, KeyModifiers::CONTROL), + &mut instances, + ); + + let state = WIZARD_STATE.lock().unwrap(); + assert_eq!(state.name_state.value(), "hello world"); + assert_eq!(state.name_state.position(), "hello ".chars().count()); +} diff --git a/src/tui/tests/widgets/search.rs b/src/tui/tests/widgets/search.rs index bdfab01..2f5265e 100644 --- a/src/tui/tests/widgets/search.rs +++ b/src/tui/tests/widgets/search.rs @@ -1,4 +1,5 @@ use super::*; +use crossterm::event::KeyModifiers; #[test] fn confirm_keeps_query_but_deactivates() { @@ -50,3 +51,26 @@ fn highlight_spans_marks_each_case_insensitive_match() { assert!(spans[2].style.add_modifier.contains(Modifier::BOLD)); assert!(spans[2].style.add_modifier.contains(Modifier::UNDERLINED)); } + +#[test] +fn ctrl_backspace_deletes_the_previous_word() { + let mut search = SearchState { + query: "alpha βeta ".to_owned(), + ..SearchState::default() + }; + + search.backspace(KeyModifiers::CONTROL); + + assert_eq!(search.query, "alpha "); +} + +#[test] +fn deleting_a_word_before_the_cursor_preserves_the_suffix() { + let mut text = "hello brave world".to_owned(); + let cursor = "hello brave".chars().count(); + + let cursor = delete_previous_word(&mut text, cursor); + + assert_eq!(text, "hello world"); + assert_eq!(cursor, "hello ".chars().count()); +} diff --git a/src/tui/widgets/account.rs b/src/tui/widgets/account.rs index 5e9c35e..1f47599 100644 --- a/src/tui/widgets/account.rs +++ b/src/tui/widgets/account.rs @@ -138,7 +138,7 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut AccountState) -> bool { } KeyCode::Backspace => { let mut new_name = name.clone(); - new_name.pop(); + super::search::backspace(&mut new_name, key_event.modifiers); state.add_mode = AddMode::OfflineNameInput(new_name); true } diff --git a/src/tui/widgets/content/discovery.rs b/src/tui/widgets/content/discovery.rs index 91fe916..2f56887 100644 --- a/src/tui/widgets/content/discovery.rs +++ b/src/tui/widgets/content/discovery.rs @@ -934,7 +934,7 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut DiscoveryState) -> bool { match key_event.code { KeyCode::Enter => state.search.confirm(), KeyCode::Esc => state.search.deactivate(), - KeyCode::Backspace => state.search.pop(), + KeyCode::Backspace => state.search.backspace(key_event.modifiers), KeyCode::Char(c) => state.search.push(c), _ => {} } diff --git a/src/tui/widgets/content/list.rs b/src/tui/widgets/content/list.rs index 6f71a29..13a0a25 100644 --- a/src/tui/widgets/content/list.rs +++ b/src/tui/widgets/content/list.rs @@ -1268,7 +1268,7 @@ fn handle_search_keys(key_event: &KeyEvent, state: &mut ContentListState) -> boo state.update_scrollbar(); } KeyCode::Backspace => { - state.search.pop(); + state.search.backspace(key_event.modifiers); state.list_state.selected = Some(0); state.update_scrollbar(); } diff --git a/src/tui/widgets/instances.rs b/src/tui/widgets/instances.rs index dd24bee..660dc58 100644 --- a/src/tui/widgets/instances.rs +++ b/src/tui/widgets/instances.rs @@ -180,7 +180,7 @@ impl WidgetKey for State { self.update_scrollbar(); } KeyCode::Backspace => { - self.search.pop(); + self.search.backspace(key_event.modifiers); self.list_state.selected = Some(0); self.update_scrollbar(); } diff --git a/src/tui/widgets/logs_viewer.rs b/src/tui/widgets/logs_viewer.rs index 04e9395..0887fae 100644 --- a/src/tui/widgets/logs_viewer.rs +++ b/src/tui/widgets/logs_viewer.rs @@ -313,7 +313,7 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut LogsState) -> bool { state.viewer_scroll = 0; } KeyCode::Backspace => { - state.viewer_search.pop(); + state.viewer_search.backspace(key_event.modifiers); state.viewer_scroll = 0; } KeyCode::Char(c) => { @@ -384,7 +384,7 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut LogsState) -> bool { state.update_scrollbar(); } KeyCode::Backspace => { - state.search.pop(); + state.search.backspace(key_event.modifiers); state.list_state.selected = Some(0); state.load_selected_content(); state.update_scrollbar(); diff --git a/src/tui/widgets/popups/import_modpack/state.rs b/src/tui/widgets/popups/import_modpack/state.rs index fc41bf5..cd13671 100644 --- a/src/tui/widgets/popups/import_modpack/state.rs +++ b/src/tui/widgets/popups/import_modpack/state.rs @@ -235,7 +235,7 @@ fn handle_input_key( match key_event.code { KeyCode::Esc => state.step = ImportStep::Discover, KeyCode::Backspace => { - state.input.pop(); + crate::tui::widgets::search::backspace(&mut state.input, key_event.modifiers); } KeyCode::Enter => { if state.input.trim().is_empty() { @@ -273,7 +273,7 @@ fn handle_version_key( return; } KeyCode::Backspace => { - state.version_search.pop(); + state.version_search.backspace(key_event.modifiers); clamp_version_index(state); return; } diff --git a/src/tui/widgets/popups/new_instance/state.rs b/src/tui/widgets/popups/new_instance/state.rs index c53a9d0..446e217 100644 --- a/src/tui/widgets/popups/new_instance/state.rs +++ b/src/tui/widgets/popups/new_instance/state.rs @@ -147,6 +147,18 @@ fn handle_name_key( } state.step = WizardStep::Loader; } + KeyCode::Backspace + if key_event + .modifiers + .contains(crossterm::event::KeyModifiers::CONTROL) => + { + let position = state.name_state.position(); + let position = crate::tui::widgets::search::delete_previous_word( + state.name_state.value_mut(), + position, + ); + *state.name_state.position_mut() = position; + } _ => { state.name_state.handle_key_event(*key_event); } @@ -167,7 +179,7 @@ fn handle_version_key( return; } KeyCode::Backspace => { - state.version_search.pop(); + state.version_search.backspace(key_event.modifiers); clamp_version_index(state); return; } diff --git a/src/tui/widgets/screenshots_grid.rs b/src/tui/widgets/screenshots_grid.rs index 3f5fe47..4af8879 100644 --- a/src/tui/widgets/screenshots_grid.rs +++ b/src/tui/widgets/screenshots_grid.rs @@ -248,7 +248,7 @@ pub fn handle_key(key_event: &KeyEvent, state: &mut ScreenshotsState) -> bool { state.scroll_row = 0; } KeyCode::Backspace => { - state.search.pop(); + state.search.backspace(key_event.modifiers); state.selected = 0; state.scroll_row = 0; } diff --git a/src/tui/widgets/search.rs b/src/tui/widgets/search.rs index b20fe99..52a750d 100644 --- a/src/tui/widgets/search.rs +++ b/src/tui/widgets/search.rs @@ -1,6 +1,7 @@ // reusable incremental search state used across multiple widgets. // handles case-insensitive filtering and inline match highlighting. +use crossterm::event::KeyModifiers; use ratatui::{ style::{Modifier, Style}, text::{Line, Span}, @@ -34,8 +35,8 @@ impl SearchState { self.query.push(c); } - pub fn pop(&mut self) { - self.query.pop(); + pub fn backspace(&mut self, modifiers: KeyModifiers) { + backspace(&mut self.query, modifiers); } pub fn is_empty(&self) -> bool { @@ -117,6 +118,30 @@ impl SearchState { } } +pub fn backspace(text: &mut String, modifiers: KeyModifiers) { + if modifiers.contains(KeyModifiers::CONTROL) { + let cursor = text.chars().count(); + delete_previous_word(text, cursor); + } else { + text.pop(); + } +} + +pub fn delete_previous_word(text: &mut String, cursor: usize) -> usize { + let mut chars = text.chars().collect::>(); + let cursor = cursor.min(chars.len()); + let mut start = cursor; + while start > 0 && chars[start - 1].is_whitespace() { + start -= 1; + } + while start > 0 && !chars[start - 1].is_whitespace() { + start -= 1; + } + chars.drain(start..cursor); + *text = chars.into_iter().collect(); + start +} + #[cfg(test)] #[path = "../tests/widgets/search.rs"] mod tests; diff --git a/src/tui/widgets/settings.rs b/src/tui/widgets/settings.rs index 532cea6..c62e4d6 100644 --- a/src/tui/widgets/settings.rs +++ b/src/tui/widgets/settings.rs @@ -491,7 +491,7 @@ pub fn handle_key( } KeyCode::Backspace => { let mut new_name = name.clone(); - new_name.pop(); + super::search::backspace(&mut new_name, key_event.modifiers); state.add_mode = AddMode::ProfileName(new_name); return SettingsAction::None; }