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;
pub struct Line {
/// the current contents of the line
chars: Vec<char>,
/// 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()
}
}

@ -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))?;
}
},

Loading…
Cancel
Save