Add support for some additional keys in interactive mode (#634)

* Ignore tab key in interactive mode

* Support home and end keys in interactive mode

* Support delete key in interactive mode
This commit is contained in:
Patrick Decat 2022-12-03 11:51:15 +01:00 committed by GitHub
parent 478af1fa0f
commit 1d9ce94f96
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 5 deletions

View file

@ -57,13 +57,17 @@ impl Cursor {
self.index += c.len_utf8(); self.index += c.len_utf8();
} }
pub fn remove(&mut self) -> char { pub fn remove(&mut self) -> Option<char> {
self.source.remove(self.index) if self.index < self.source.len() {
Some(self.source.remove(self.index))
} else {
None
}
} }
pub fn back(&mut self) -> Option<char> { pub fn back(&mut self) -> Option<char> {
if self.left() { if self.left() {
Some(self.remove()) self.remove()
} else { } else {
None None
} }

View file

@ -69,6 +69,7 @@ impl State {
len: usize, len: usize,
) -> Option<usize> { ) -> Option<usize> {
match input { match input {
TermEvent::Key(Key::Char('\t')) => {}
TermEvent::Key(Key::Ctrl('c' | 'd' | 'g')) => return Some(RETURN_ORIGINAL), TermEvent::Key(Key::Ctrl('c' | 'd' | 'g')) => return Some(RETURN_ORIGINAL),
TermEvent::Key(Key::Esc) => { TermEvent::Key(Key::Esc) => {
return Some(match settings.exit_mode { return Some(match settings.exit_mode {
@ -87,12 +88,15 @@ impl State {
self.input.left(); self.input.left();
} }
TermEvent::Key(Key::Right | Key::Ctrl('l')) => self.input.right(), TermEvent::Key(Key::Right | Key::Ctrl('l')) => self.input.right(),
TermEvent::Key(Key::Ctrl('a')) => self.input.start(), TermEvent::Key(Key::Ctrl('a') | Key::Home) => self.input.start(),
TermEvent::Key(Key::Ctrl('e')) => self.input.end(), TermEvent::Key(Key::Ctrl('e') | Key::End) => self.input.end(),
TermEvent::Key(Key::Char(c)) => self.input.insert(*c), TermEvent::Key(Key::Char(c)) => self.input.insert(*c),
TermEvent::Key(Key::Backspace) => { TermEvent::Key(Key::Backspace) => {
self.input.back(); self.input.back();
} }
TermEvent::Key(Key::Delete) => {
self.input.remove();
}
TermEvent::Key(Key::Ctrl('w')) => { TermEvent::Key(Key::Ctrl('w')) => {
// remove the first batch of whitespace // remove the first batch of whitespace
while matches!(self.input.back(), Some(c) if c.is_whitespace()) {} while matches!(self.input.back(), Some(c) if c.is_whitespace()) {}