From 28dffed640adf8815294faa7a1f4b4d8a50c9707 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Thu, 11 Jan 2024 19:04:27 -0800 Subject: Keep the cursor position on error. Now it handles errors so well! You can barely tell that nearly all of the calls to cursor_pos are failing. --- src/main.rs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main.rs b/src/main.rs index 7b9b477..b696fbe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -153,23 +153,24 @@ impl State { fn update_cursor_col(&mut self, stdin: &mut BufReader) -> Result<(), Box> { let size = term::size()?; let line = self.line(); - write!(stdout(), "\x1b[{}H\x1b[K", self.cursor_row + 1)?; + write!(stdout(), "\x1b[{}H", self.cursor_row + 1)?; line.slice(0, self.line_offset).print(&mut stdout())?; - let Ok((_, col)) = term::cursor_pos(stdin) else { - let _ = line - .slice(self.line_offset, line.len()) - .print(&mut stdout()); - return Err(Box::from("cursor_pos")); - }; + let (_, col) = term::cursor_pos(stdin)?; if col == size.ws_col - 1 { self.cursor_col = col - 1; - write!(stdout(), "\x1b[D\x1b[K")?; + write!( + stdout(), + "\x1b[{};{}H\x1b[K", + self.cursor_row + 1, + self.cursor_col + 1 + )?; return Ok(()); } if self.line_offset == line.len() { if col > self.cursor_col { self.cursor_col = col; } + write!(stdout(), "\x1b[K")?; return Ok(()); } self.cursor_col = col; -- cgit v1.3.1