diff options
| -rw-r--r-- | src/main.rs | 21 | ||||
| -rw-r--r-- | src/rope.rs | 2 | ||||
| -rw-r--r-- | src/term.rs | 5 |
3 files changed, 23 insertions, 5 deletions
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<dyn Error>> { } 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(); } diff --git a/src/rope.rs b/src/rope.rs index 7d00dcd..25da1fc 100644 --- a/src/rope.rs +++ b/src/rope.rs @@ -124,7 +124,7 @@ impl Rope { } } - fn concat(&self, other: &Rope) -> Rope { + pub fn concat(&self, other: &Rope) -> Rope { if self.len() == 0 { return other.clone(); } diff --git a/src/term.rs b/src/term.rs index c23dc4e..8f44b23 100644 --- a/src/term.rs +++ b/src/term.rs @@ -65,12 +65,14 @@ pub enum Key { Down, Left, Right, + Backspace, Char(u8), } pub fn read_key(stdin: &mut Stdin) -> Result<Key, Box<dyn Error>> { const CTRL_Q: u8 = 17; const CTRL_S: u8 = 19; + const BACKSPACE: u8 = 127; const ESC: u8 = 27; loop { @@ -82,6 +84,9 @@ pub fn read_key(stdin: &mut Stdin) -> Result<Key, Box<dyn Error>> { if buf[0] == CTRL_S { return Ok(Key::CtrlS); } + if buf[0] == BACKSPACE { + return Ok(Key::Backspace); + } if buf[0] != ESC { return Ok(Key::Char(buf[0])); } |
