feat(tui): support Ctrl+Backspace in text inputs
This commit is contained in:
1 parent
315567a79c
commit
cccf65981a
13 files changed
+99
-16
No files matched your search
@@ -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;
|
||||
Reference in new issue
Block a user