feat(tui): support Ctrl+Backspace in text inputs

This commit is contained in:
objz committed 2026-07-28 16:46:15 +02:00
1 parent 315567a79c
commit cccf65981a
13 files changed
+99 -16

No files matched your search

+2 -2
View File
@@ -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) => {
@@ -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());
}
+24
View File
@@ -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());
}
+1 -1
View File
@@ -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
}
+1 -1
View File
@@ -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),
_ => {}
}
+1 -1
View File
@@ -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();
}
+1 -1
View File
@@ -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();
}
+2 -2
View File
@@ -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();
@@ -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;
}
+13 -1
View File
@@ -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;
}
+1 -1
View File
@@ -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;
}
+27 -2
View File
@@ -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::<Vec<_>>();
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;
+1 -1
View File
@@ -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;
}