can move right

parse-tree
Jordan Orelli 2 years ago
parent 3964417081
commit 893a38f694

@ -179,6 +179,7 @@ impl Reader {
self.take_bracket()?; self.take_bracket()?;
match self.next_escape_char()? { match self.next_escape_char()? {
'D' => Ok(Event::Left), 'D' => Ok(Event::Left),
'C' => Ok(Event::Right),
e => Err(Error::input_error(format!("unexpected escape char: {}", e)).into()), e => Err(Error::input_error(format!("unexpected escape char: {}", e)).into()),
} }
} }
@ -227,6 +228,7 @@ pub enum Event {
Mouse { x: i16, y: i16 }, Mouse { x: i16, y: i16 },
Size, Size,
Left, Left,
Right,
} }
const ALT_KEYS: u32 = 0x0002 | 0x0001; const ALT_KEYS: u32 = 0x0002 | 0x0001;

@ -37,9 +37,21 @@ impl Line {
Ok(()) Ok(())
} }
pub fn back(&mut self) { pub fn back(&mut self) -> bool{
if self.cursor > 0 { if self.cursor > 0 {
self.cursor -= 1; self.cursor -= 1;
true
} else {
false
}
}
pub fn forward(&mut self) -> bool {
if self.cursor < self.chars.len() {
self.cursor += 1;
true
} else {
false
} }
} }

@ -201,8 +201,7 @@ fn main() -> Result<()> {
let tail = line.tail(); let tail = line.tail();
let n = tail.chars().count(); let n = tail.chars().count();
// write everything from the current line cursor out to the output buffer. Then // write everything from the current line cursor out to the output buffer.
// rewind the cursor in the output buffer to where it was.
unsafe { unsafe {
Error::check(Console::WriteConsoleA( Error::check(Console::WriteConsoleA(
stdout, stdout,
@ -211,6 +210,8 @@ fn main() -> Result<()> {
None, 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 { if n > 1 {
let text = format!("\x1b[{}D", n-1); let text = format!("\x1b[{}D", n-1);
Error::check(Console::WriteConsoleA(stdout, text.as_bytes(), None, None))?; Error::check(Console::WriteConsoleA(stdout, text.as_bytes(), None, None))?;
@ -222,13 +223,21 @@ fn main() -> Result<()> {
debug!("Unhandled Keyboard Event: {}", event); debug!("Unhandled Keyboard Event: {}", event);
} }
input::Event::Left => { input::Event::Left => {
debug!("back!"); if line.back() {
line.back(); unsafe {
unsafe { let text = "\x1b[D"; // lol this sucks
let text = "\x1b[D"; // lol this sucks Error::check(Console::WriteConsoleA(stdout, text.as_bytes(), None, None))?;
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(true) => {},
input::Event::Focus(false) => {}, input::Event::Focus(false) => {},
input::Event::Menu(_command_id) => {}, input::Event::Menu(_command_id) => {},

Loading…
Cancel
Save