diff --git a/src/input.rs b/src/input.rs index 0e961ba..65c82d6 100644 --- a/src/input.rs +++ b/src/input.rs @@ -179,6 +179,7 @@ impl Reader { self.take_bracket()?; match self.next_escape_char()? { 'D' => Ok(Event::Left), + 'C' => Ok(Event::Right), e => Err(Error::input_error(format!("unexpected escape char: {}", e)).into()), } } @@ -227,6 +228,7 @@ pub enum Event { Mouse { x: i16, y: i16 }, Size, Left, + Right, } const ALT_KEYS: u32 = 0x0002 | 0x0001; diff --git a/src/line.rs b/src/line.rs index 816bf52..8dbc359 100644 --- a/src/line.rs +++ b/src/line.rs @@ -37,9 +37,21 @@ impl Line { Ok(()) } - pub fn back(&mut self) { + pub fn back(&mut self) -> bool{ if self.cursor > 0 { self.cursor -= 1; + true + } else { + false + } + } + + pub fn forward(&mut self) -> bool { + if self.cursor < self.chars.len() { + self.cursor += 1; + true + } else { + false } } diff --git a/src/main.rs b/src/main.rs index 585b79d..546d258 100644 --- a/src/main.rs +++ b/src/main.rs @@ -201,8 +201,7 @@ fn main() -> Result<()> { let tail = line.tail(); let n = tail.chars().count(); - // write everything from the current line cursor out to the output buffer. Then - // rewind the cursor in the output buffer to where it was. + // write everything from the current line cursor out to the output buffer. unsafe { Error::check(Console::WriteConsoleA( stdout, @@ -211,6 +210,8 @@ fn main() -> Result<()> { None, ))?; + // if we wrote more than one character, because we weren't at the end, we + // need to rewind the terminal cursor to where it was. if n > 1 { let text = format!("\x1b[{}D", n-1); Error::check(Console::WriteConsoleA(stdout, text.as_bytes(), None, None))?; @@ -222,13 +223,21 @@ fn main() -> Result<()> { debug!("Unhandled Keyboard Event: {}", event); } input::Event::Left => { - debug!("back!"); - line.back(); - unsafe { - let text = "\x1b[D"; // lol this sucks - Error::check(Console::WriteConsoleA(stdout, text.as_bytes(), None, None))?; + if line.back() { + unsafe { + let text = "\x1b[D"; // lol this sucks + Error::check(Console::WriteConsoleA(stdout, text.as_bytes(), None, None))?; + } } }, + input::Event::Right => { + if line.forward() { + unsafe { + let text = "\x1b[C"; // lol this sucks + Error::check(Console::WriteConsoleA(stdout, text.as_bytes(), None, None))?; + } + } + } input::Event::Focus(true) => {}, input::Event::Focus(false) => {}, input::Event::Menu(_command_id) => {},