From 6d60563ab59e01f2fb290ae06c284d3741ebc05d Mon Sep 17 00:00:00 2001 From: radlinskii Date: Thu, 27 Jul 2023 22:56:05 +0200 Subject: [PATCH] feat: handle alt+backspace for deleting current word --- src/runner.rs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/src/runner.rs b/src/runner.rs index 14e8ff5..9863984 100644 --- a/src/runner.rs +++ b/src/runner.rs @@ -88,6 +88,44 @@ impl<'a> Runner<'a> { ); stdout.flush().unwrap(); } + Key::Alt(c) => { + // on alt + backspace delete current word + if c == '\x7F' { + let mut chars_to_delete = 0; + let mut found_non_space_char = false; + for c in self.actual_input.chars().rev() { + if c == ' ' && found_non_space_char { + break; + } else { + found_non_space_char = true; + } + chars_to_delete += 1; + } + + for _ in 0..chars_to_delete { + print!("{}", termion::cursor::Left(1)); + self.actual_input.pop(); + print!("{}", termion::clear::AfterCursor); + } + + print!( + "{}", + &self.expected_input.to_str()[self.actual_input.len()..] + ); + + print!( + "{}", + termion::cursor::Left( + self.expected_input.to_str()[self.actual_input.len()..] + .len() + .try_into() + .unwrap() + ) + ); + + stdout.flush().unwrap(); + } + } Key::Ctrl('c') => { break; // Exit the loop when the user presses Ctrl+C }