diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-06-09 14:01:08 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-06-09 14:01:08 -0400 |
commit | 3846e0aadda1c7eec96a8004d439a438ed4538a7 (patch) | |
tree | 81608d1bcedc19453e1051b88a02e52a238ebb55 | |
parent | 54fa28994924803e2e4ead34521493e75286d1cc (diff) | |
download | nbsh-old-3846e0aadda1c7eec96a8004d439a438ed4538a7.tar.gz nbsh-old-3846e0aadda1c7eec96a8004d439a438ed4538a7.zip |
implement a few more keys
-rw-r--r-- | src/readline.rs | 60 |
1 files changed, 53 insertions, 7 deletions
diff --git a/src/readline.rs b/src/readline.rs index d8d24ba..aee69c4 100644 --- a/src/readline.rs +++ b/src/readline.rs @@ -104,20 +104,60 @@ impl ReadlineState { self.buffer.insert(self.cursor, c); self.cursor += 1; } - crossterm::KeyEvent::Ctrl(c) => { - if c == 'd' { - if self.buffer.is_empty() { - self.echo_char('\n').context(WriteToTerminal)?; - ensure!(false, EOF); + crossterm::KeyEvent::Ctrl(c) => match c { + 'a' => { + if self.cursor != 0 { + self.echo( + &format!("\x1b[{}D", self.cursor).into_bytes(), + ) + .context(WriteToTerminal)?; + self.cursor = 0; } } - if c == 'c' { + 'c' => { self.buffer = String::new(); self.cursor = 0; self.echo_char('\n').context(WriteToTerminal)?; self.prompt().context(WriteToTerminal)?; } - } + 'd' => { + if self.buffer.is_empty() { + self.echo_char('\n').context(WriteToTerminal)?; + ensure!(false, EOF); + } + } + 'e' => { + if self.cursor != self.buffer.len() { + self.echo( + &format!( + "\x1b[{}C", + self.buffer.len() - self.cursor + ) + .into_bytes(), + ) + .context(WriteToTerminal)?; + self.cursor = self.buffer.len(); + } + } + 'u' => { + if self.cursor != 0 { + self.echo( + std::iter::repeat(b'\x08') + .take(self.cursor) + .chain( + format!("\x1b[{}P", self.cursor) + .into_bytes(), + ) + .collect::<Vec<_>>() + .as_ref(), + ) + .context(WriteToTerminal)?; + self.buffer = self.buffer.split_off(self.cursor); + self.cursor = 0; + } + } + _ => {} + }, crossterm::KeyEvent::Backspace => { if self.cursor != 0 { self.cursor -= 1; @@ -141,6 +181,12 @@ impl ReadlineState { self.write(b"\x1b[C").context(WriteToTerminal)?; } } + crossterm::KeyEvent::Delete => { + if self.cursor != self.buffer.len() { + self.buffer.remove(self.cursor); + self.echo(b"\x1b[P").context(WriteToTerminal)?; + } + } _ => {} } return Ok(futures::Async::NotReady); |