From 8238346dc745319bf5289be24176915940da96f6 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sun, 9 Jun 2019 09:22:37 -0400 Subject: implement cursor moving left and right --- src/readline.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) 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, 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; } } _ => {} -- cgit v1.2.3-54-g00ecf