Implement <Alt-d> to forward delete words in input

Until a non alphanumeric char.
pull/154/head
sebashwa 2 years ago
parent 195dc95a1b
commit 2af554341b

@ -89,6 +89,13 @@ impl Input {
self.value.append(&mut tail);
}
fn delete_right_until(&mut self, index: usize) {
let mut tail = self.value.to_vec().drain(index..).collect();
self.value.truncate(self.cursor_index);
self.value.append(&mut tail);
}
pub fn handle_key(&mut self, key: Key) -> (Option<Key>, bool) {
match key {
Key::Char(c) => {
@ -137,6 +144,15 @@ impl Input {
self.cursor_position = self.width_for(&self.value[0..new_cursor_index]);
return (Some(key), true);
}
Key::Alt('d') => {
if self.cannot_go_right() {
return (Some(key), false);
}
let index = self.cursor_index_forwards_until(&is_nonalphanumeric);
self.delete_right_until(index);
return (Some(key), true);
}
Key::Left | Key::Ctrl('b') => {
if self.cannot_go_left() {
return (Some(key), false);
@ -302,7 +318,7 @@ mod test {
}
#[test]
fn test_deletes_til_nonalphanumeric_for_alt_backspace() {
fn test_deletes_backwards_til_nonalphanumeric_for_alt_backspace() {
let mut input = Input::new();
input.value = vec!['a', '-', 'c', 'd'];
input.cursor_index = 3;
@ -316,6 +332,21 @@ mod test {
assert_eq!(input.cursor_index, 2);
}
#[test]
fn test_deletes_forwards_til_nonalphanumeric_for_alt_d() {
let mut input = Input::new();
input.value = vec!['a', 'b', '-', 'd'];
input.cursor_index = 1;
input.cursor_position = input.value_width();
let (matched_key, input_changed) = input.handle_key(Key::Alt('d'));
assert_eq!(matched_key, Some(Key::Alt('d')));
assert_eq!(input_changed, true);
assert_eq!(input.value, vec!['a', '-', 'd']);
assert_eq!(input.cursor_index, 1);
}
#[test]
fn test_moves_backwards_til_nonalphanumeric_for_alt_b() {
let mut input = Input::new();

Loading…
Cancel
Save