can move left

parse-tree
Jordan Orelli 2 years ago
parent b0138d6473
commit 3964417081

@ -4,7 +4,10 @@ use anyhow::Result;
use windows::Win32::System::Console; use windows::Win32::System::Console;
pub struct Line { pub struct Line {
/// the current contents of the line
chars: Vec<char>, chars: Vec<char>,
/// the cursor position of our dit head within the vector of characters that we store as chars
cursor: usize, cursor: usize,
} }
@ -44,4 +47,14 @@ impl Line {
self.chars.insert(self.cursor, c); self.chars.insert(self.cursor, c);
self.cursor += 1; 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()
}
} }

@ -198,18 +198,23 @@ fn main() -> Result<()> {
if !event.char.is_control() { if !event.char.is_control() {
line.insert(event.char); line.insert(event.char);
let mut buf = [0 as u8; 8]; let tail = line.tail();
let s = event.char.encode_utf8(&mut buf); 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. Then
// rewind the cursor in the output buffer to where it was. // rewind the cursor in the output buffer to where it was.
unsafe { unsafe {
Error::check(Console::WriteConsoleA( Error::check(Console::WriteConsoleA(
stdout, stdout,
s.as_bytes(), tail.as_bytes(),
None, None,
None, None,
))?; ))?;
if n > 1 {
let text = format!("\x1b[{}D", n-1);
Error::check(Console::WriteConsoleA(stdout, text.as_bytes(), None, None))?;
}
} }
continue; continue;
} }
@ -220,7 +225,7 @@ fn main() -> Result<()> {
debug!("back!"); debug!("back!");
line.back(); line.back();
unsafe { unsafe {
let text = "\x1b[D"; 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))?;
} }
}, },

Loading…
Cancel
Save