From 396441708196f813ced1e006854dc44382ddd3c3 Mon Sep 17 00:00:00 2001 From: Jordan Orelli Date: Sat, 4 Mar 2023 20:15:40 -0600 Subject: [PATCH] can move left --- src/line.rs | 13 +++++++++++++ src/main.rs | 13 +++++++++---- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/src/line.rs b/src/line.rs index 9d109ae..816bf52 100644 --- a/src/line.rs +++ b/src/line.rs @@ -4,7 +4,10 @@ use anyhow::Result; use windows::Win32::System::Console; pub struct Line { + /// the current contents of the line chars: Vec, + + /// the cursor position of our dit head within the vector of characters that we store as chars cursor: usize, } @@ -44,4 +47,14 @@ impl Line { self.chars.insert(self.cursor, c); self.cursor += 1; } + + pub fn tail(&self) -> String { + let mut start = self.cursor; + if start > 0 { + start -= 1; + } + + let chars = &self.chars[start..]; + chars.iter().collect() + } } diff --git a/src/main.rs b/src/main.rs index e634902..585b79d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -198,18 +198,23 @@ fn main() -> Result<()> { if !event.char.is_control() { line.insert(event.char); - let mut buf = [0 as u8; 8]; - let s = event.char.encode_utf8(&mut buf); + 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. unsafe { Error::check(Console::WriteConsoleA( stdout, - s.as_bytes(), + tail.as_bytes(), None, None, ))?; + + if n > 1 { + let text = format!("\x1b[{}D", n-1); + Error::check(Console::WriteConsoleA(stdout, text.as_bytes(), None, None))?; + } } continue; } @@ -220,7 +225,7 @@ fn main() -> Result<()> { debug!("back!"); line.back(); unsafe { - let text = "\x1b[D"; + let text = "\x1b[D"; // lol this sucks Error::check(Console::WriteConsoleA(stdout, text.as_bytes(), None, None))?; } },