From 9ac429aa26529af2f753d01fe6e4fe5046bdf30f Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sat, 6 Jan 2024 13:57:32 -0800 Subject: Implement backspace. --- src/main.rs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 54b644d..139b4c3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -140,6 +140,7 @@ impl State { } else { self.cursor_row -= 1; } + self.repaint_line_full()?; return Ok(()); } @@ -154,6 +155,7 @@ impl State { } else { self.cursor_row += 1; } + self.repaint_line_full()?; return Ok(()); } @@ -166,11 +168,11 @@ impl State { } if self.cursor_col == 0 { self.up()?; - self.repaint_line_full()?; self.cursor_col = self.line_cols; } else { self.cursor_col -= 1; } + self.repaint_line_full()?; return Ok(()); } @@ -185,6 +187,7 @@ impl State { } else { self.cursor_col += 1; } + self.repaint_line_full()?; return Ok(()); } } @@ -217,21 +220,31 @@ fn edit(file: &OsStr) -> Result<(), Box> { } Key::Up => { let _ = state.up(); - let _ = state.repaint_line_full(); let _ = stdout().flush(); } Key::Down => { let _ = state.down(); - let _ = state.repaint_line_full(); let _ = stdout().flush(); } Key::Left => { let _ = state.left(); - let _ = state.repaint_line_full(); let _ = stdout().flush(); } Key::Right => { let _ = state.right(); + let _ = stdout().flush(); + } + Key::Backspace => { + if state.row_start == 0 && state.cursor_row == 0 && state.cursor_col == 0 { + continue; + } + let offset = state.buf.line_idx(state.row_start+state.cursor_row)+state.line_offset; + let need_refresh_all = state.cursor_col == 0; + let _ = state.left(); + state.buf = state.buf.slice(0, state.buf.floor_char_boundary(offset-1)).concat(&state.buf.slice(offset, state.buf.len())); + if need_refresh_all { + let _ = state.repaint_all(); + } let _ = state.repaint_line_full(); let _ = stdout().flush(); } -- cgit v1.3.1