Skip to content

Commit

Permalink
backspace delete char added
Browse files Browse the repository at this point in the history
  • Loading branch information
Kacperacy committed Mar 14, 2024
1 parent c071d28 commit 269f59c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ impl Editor {
self.filename = Some(filename.clone());
self.screen.set_filename(Some(filename.clone()));
} else {
File::create(filename).unwrap();
self.filename = Some(filename.clone());
self.screen.set_filename(Some(filename.clone()));
}
Expand Down Expand Up @@ -151,6 +150,8 @@ impl Editor {
|| c.code == KeyCode::End
{
self.move_cursor(c);
} else if c.code == KeyCode::Backspace {
self.delete_char();
} else if let KeyCode::Char(c) = c.code {
self.insert_char(c);
}
Expand Down Expand Up @@ -244,6 +245,23 @@ impl Editor {
self.dirty = true;
}

fn delete_char(&mut self) {
if self.cursor.y >= self.rows.len() {
return;
}
if self.cursor.x > 0 {
self.rows[self.cursor.y].remove(self.cursor.x - 1);
self.cursor.x -= 1;
self.dirty = true;
} else if self.cursor.y > 0 {
let row = self.rows.remove(self.cursor.y);
self.cursor.y -= 1;
self.cursor.x = self.rows[self.cursor.y].len();
self.rows[self.cursor.y].push_str(&row);
self.dirty = true;
}
}

fn save(&mut self) {
if let Some(filename) = &self.filename {
let mut file = File::create(filename).unwrap();
Expand Down
Empty file removed test.txt
Empty file.

0 comments on commit 269f59c

Please sign in to comment.