From c112195b9dc257d64a5af71bfdbeeba3aa3b8061 Mon Sep 17 00:00:00 2001 From: CrispyPin Date: Sun, 26 Feb 2023 00:39:12 +0100 Subject: [PATCH] backspace & delete --- src/editor.rs | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/editor.rs b/src/editor.rs index 4a28ff6..f180181 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -57,6 +57,8 @@ impl Editor { match key { Key::Esc => self.quit = true, Key::Char(char) => self.insert_char(char), + Key::Backspace => self.backspace(), + Key::Delete => self.delete(), Key::Left => self.move_left(), Key::Right => self.move_right(), Key::Up => self.move_up(), @@ -134,9 +136,23 @@ impl Editor { } fn insert_char(&mut self, ch: char) { - let index = self.current_line().start + self.cursor.column; - self.text.insert(index, ch); + self.text.insert(self.index(), ch); + self.find_lines(); self.move_right(); + } + + fn backspace(&mut self) { + self.text.remove(self.index() - 1); + self.find_lines(); + self.move_left(); + } + + fn delete(&mut self) { + self.text.remove(self.index()); self.find_lines(); } + + fn index(&self) -> usize { + self.current_line().start + self.cursor.column + } }