Basic insertion and deletion
This commit is contained in:
parent
62e3a3dba7
commit
a46c474a00
@ -1,3 +1,4 @@
|
|||||||
|
use crate::Position;
|
||||||
use crate::Row;
|
use crate::Row;
|
||||||
use std::fs;
|
use std::fs;
|
||||||
|
|
||||||
@ -34,4 +35,24 @@ impl Document {
|
|||||||
pub fn len(&self) -> usize {
|
pub fn len(&self) -> usize {
|
||||||
self.rows.len()
|
self.rows.len()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn insert(&mut self, at: &Position, c: char) {
|
||||||
|
if at.y == self.len() {
|
||||||
|
let mut row = Row::default();
|
||||||
|
row.insert(0, c);
|
||||||
|
self.rows.push(row);
|
||||||
|
} else if at.y < self.len() {
|
||||||
|
let row = self.rows.get_mut(at.y).unwrap();
|
||||||
|
row.insert(at.x, c);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete(&mut self, at: &Position) {
|
||||||
|
if at.y >= self.len() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
let row = self.rows.get_mut(at.y).unwrap();
|
||||||
|
row.delete(at.x);
|
||||||
|
}
|
||||||
}
|
}
|
@ -145,6 +145,17 @@ impl Editor {
|
|||||||
let pressed_key = Terminal::read_key()?;
|
let pressed_key = Terminal::read_key()?;
|
||||||
match pressed_key {
|
match pressed_key {
|
||||||
Key::Ctrl('q') => self.should_quit = true,
|
Key::Ctrl('q') => self.should_quit = true,
|
||||||
|
Key::Char(c) => {
|
||||||
|
self.document.insert(&self.cursor_position, c);
|
||||||
|
self.move_cursor(Key::Right);
|
||||||
|
},
|
||||||
|
Key::Delete => self.document.delete(&self.cursor_position),
|
||||||
|
Key::Backspace => {
|
||||||
|
if self.cursor_position.x > 0 || self.cursor_position.y > 0 {
|
||||||
|
self.move_cursor(Key::Left);
|
||||||
|
self.document.delete(&self.cursor_position);
|
||||||
|
}
|
||||||
|
}
|
||||||
Key::Up
|
Key::Up
|
||||||
| Key::Down
|
| Key::Down
|
||||||
| Key::Left
|
| Key::Left
|
||||||
|
32
src/row.rs
32
src/row.rs
@ -1,6 +1,7 @@
|
|||||||
use std::cmp;
|
use std::cmp;
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
pub struct Row {
|
pub struct Row {
|
||||||
string: String,
|
string: String,
|
||||||
len: usize,
|
len: usize,
|
||||||
@ -51,4 +52,35 @@ impl Row {
|
|||||||
fn update_len(&mut self) {
|
fn update_len(&mut self) {
|
||||||
self.len = self.string[..].graphemes(true).count();
|
self.len = self.string[..].graphemes(true).count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn insert(&mut self, at: usize, c: char) {
|
||||||
|
if at >= self.len() {
|
||||||
|
self.string.push(c);
|
||||||
|
} else {
|
||||||
|
let mut result: String = self.string[..].graphemes(true).take(at).collect();
|
||||||
|
let remainder: String = self.string[..].graphemes(true).skip(at).collect();
|
||||||
|
|
||||||
|
result.push(c);
|
||||||
|
result.push_str(&remainder);
|
||||||
|
|
||||||
|
self.string = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.update_len();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn delete(&mut self, at: usize) {
|
||||||
|
if at >= self.len() {
|
||||||
|
return
|
||||||
|
} else {
|
||||||
|
let mut result: String = self.string[..].graphemes(true).take(at).collect();
|
||||||
|
let remainder: String = self.string[..].graphemes(true).skip(at + 1).collect();
|
||||||
|
|
||||||
|
result.push_str(&remainder);
|
||||||
|
|
||||||
|
self.string = result;
|
||||||
|
}
|
||||||
|
|
||||||
|
self.update_len();
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user