From d3f52cf5dc1e06a81fb75a8631ac00e8adc04ac7 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sat, 13 Jan 2024 17:45:37 -0800 Subject: Escape control characters. --- src/main.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 0cc9ad4..3663052 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,7 +21,7 @@ fn line_offset(stdin: &mut Reader, line: &Rope, col: u16) -> Result 127 { + if !(c == b'\t' || b' ' <= c && c <= b'~') { break; } if n == col { @@ -231,12 +231,12 @@ impl State { } fn keypress(&mut self, key: Key) -> Result> { - const CTRL_Q: char = (b'Q' - b'@') as char; - const CTRL_S: char = (b'S' - b'@') as char; - const CTRL_Y: char = (b'Y' - b'@') as char; - const CTRL_Z: char = (b'Z' - b'@') as char; - const BACKSPACE: char = (b'H' - b'@') as char; - const OTHER_BACKSPACE: char = '\x7f'; + const CTRL_Q: char = (b'Q' ^ b'@') as char; + const CTRL_S: char = (b'S' ^ b'@') as char; + const CTRL_Y: char = (b'Y' ^ b'@') as char; + const CTRL_Z: char = (b'Z' ^ b'@') as char; + const BACKSPACE: char = (b'H' ^ b'@') as char; + const OTHER_BACKSPACE: char = (b'?' ^ b'@') as char; let inserting = self.inserting; self.inserting = false; @@ -448,7 +448,7 @@ impl State { } Key::Char(CTRL_S) => { let mut f = File::create(&self.path)?; - self.buf.print(&mut f)?; + self.buf.write(&mut f)?; f.sync_all()?; } Key::Char(CTRL_Z) => { @@ -546,15 +546,15 @@ impl State { .line_start(self.row_start + usize::from(self.cursor_row)) + self.line_offset; let mut buf = [0; 4]; - let s = c.encode_utf8(&mut buf); + let r = Rope::new(c.encode_utf8(&mut buf)); self.buf = self .buf .slice(0, offset) - .concat(&Rope::new(s)) + .concat(&r) .concat(&self.buf.slice(offset, self.buf.len())); - self.line_offset += s.len(); + self.line_offset += r.len(); self.need_repaint_line = true; - stdout().write_all(s.as_bytes())?; + r.print(&mut stdout())?; stdout().flush()?; } } -- cgit v1.3.1