diff options
author | Jesse Luehrs <doy@tozt.net> | 2019-06-09 09:22:37 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2019-06-09 09:22:37 -0400 |
commit | 8238346dc745319bf5289be24176915940da96f6 (patch) | |
tree | 19a6839acafca5891b075f0c77521bbe13a90773 | |
parent | e9c8b96dc6d50a94f1bc1b6434cf02f2f9291361 (diff) | |
download | nbsh-old-8238346dc745319bf5289be24176915940da96f6.tar.gz nbsh-old-8238346dc745319bf5289be24176915940da96f6.zip |
implement cursor moving left and right
-rw-r--r-- | src/readline.rs | 25 |
1 files changed, 24 insertions, 1 deletions
diff --git a/src/readline.rs b/src/readline.rs index c66099b..af2b4fa 100644 --- a/src/readline.rs +++ b/src/readline.rs @@ -59,6 +59,9 @@ impl ReadlineState { ) -> std::result::Result<futures::Async<String>, Error> { match event { crossterm::KeyEvent::Char(c) => { + if self.cursor != self.buffer.len() && c != '\n' { + self.echo(b"\x1b[@").map_err(|e| Error::IOError(e))?; + } self.echo_char(c).map_err(|e| Error::IOError(e))?; if c == '\n' { @@ -86,7 +89,27 @@ impl ReadlineState { if self.cursor != 0 { self.cursor -= 1; self.buffer.remove(self.cursor); - self.echo(b"\x08 \x08").map_err(|e| Error::IOError(e))?; + if self.cursor == self.buffer.len() { + self.echo(b"\x08 \x08") + .map_err(|e| Error::IOError(e))?; + } else { + self.echo(b"\x08\x1b[P") + .map_err(|e| Error::IOError(e))?; + } + } + } + crossterm::KeyEvent::Left => { + let cursor = 0.max(self.cursor - 1); + if cursor != self.cursor { + self.write(b"\x1b[D").map_err(|e| Error::IOError(e))?; + self.cursor = cursor; + } + } + crossterm::KeyEvent::Right => { + let cursor = self.buffer.len().min(self.cursor + 1); + if cursor != self.cursor { + self.write(b"\x1b[C").map_err(|e| Error::IOError(e))?; + self.cursor = cursor; } } _ => {} |