Make the enter key work

This commit is contained in:
Timothy Warren 2019-08-30 15:10:19 -04:00
parent 8e125b8c7c
commit aadbccaf9d

View File

@ -123,6 +123,11 @@ impl Editor {
// ------------------------------------------------------------------------
fn read_key(&mut self) -> Option<EditorKey<char>> {
/*
TODO: Read 1 byte by default, and read additional bytes
if the first byte is an escape character, to resolve the
unintentional input throttling
*/
let stdin = io::stdin();
let stdin = stdin.lock();
let mut in_str = String::new();
@ -294,9 +299,7 @@ impl Editor {
match char {
Backspace => self._del_or_backspace(Backspace),
DeleteKey => self._del_or_backspace(DeleteKey),
Enter => {
// TODO
}
Enter => self.insert_new_line(),
Escape => (),
ArrowUp => self.move_cursor(&ArrowUp),
ArrowDown => self.move_cursor(&ArrowDown),
@ -585,9 +588,25 @@ impl Editor {
row.render = str;
}
fn append_row(&mut self, row: &str) {
self.rows.push(EditorRow::new(row));
self.update_row(self.rows.len() - 1);
fn insert_row(&mut self, at: usize, row: &str) {
if at > self.rows.len() {
return;
}
let row = EditorRow::new(row);
self.rows.insert(at, row);
self.update_row(at);
self.dirty += 1;
}
fn delete_row(&mut self, row_index: usize) {
if row_index > self.rows.len() {
return;
}
self.rows.remove(row_index);
self.dirty += 1;
}
@ -607,6 +626,15 @@ impl Editor {
self.dirty += 1;
}
fn row_append_string(&mut self, row_index: usize, strng: &str) {
let row = &mut self.rows[row_index];
row.chars += strng;
self.update_row(row_index);
self.dirty += 1;
}
fn row_delete_char(&mut self, row_index: usize, char_index: usize) {
let row = &mut self.rows[row_index];
if char_index >= row.chars.len() {
@ -625,21 +653,55 @@ impl Editor {
fn insert_char(&mut self, ch: char) {
if self.cursor_y == self.rows.len() {
self.append_row("");
self.insert_row(self.rows.len(), "");
}
self.row_insert_char(self.cursor_y, self.cursor_x, ch);
self.cursor_x += 1;
}
fn insert_new_line(&mut self) {
if self.cursor_x == 0 {
self.insert_row(self.cursor_y, "");
} else {
let row = &mut self.rows[self.cursor_y];
let row_chars = row.chars.clone();
let len = row.chars.len() - self.cursor_x;
// Truncate the old row if you aren't at the end of a line
if self.cursor_x < row.chars.len() {
row.chars.truncate(self.cursor_x);
}
let slice = &row_chars[self.cursor_x..];
self.insert_row(self.cursor_y + 1, slice);
self.update_row(self.cursor_y);
}
self.cursor_y += 1;
self.cursor_x = 0;
}
fn delete_char(&mut self) {
if self.cursor_y == self.rows.len() {
return;
}
if self.cursor_x == 0 && self.cursor_y == 0 {
return;
}
if self.cursor_x > 0 {
self.row_delete_char(self.cursor_y, self.cursor_x - 1);
self.cursor_x -= 1;
} else {
// When deleting the first character in the row, collapse that row into the previous one
self.cursor_x = self.rows[self.cursor_y - 1].chars.len();
self.row_append_string(self.cursor_y - 1, &self.rows[self.cursor_y].chars.clone());
self.delete_row(self.cursor_y);
self.cursor_y -= 1;
}
}
@ -669,7 +731,7 @@ impl Editor {
let lines = buf_reader.lines().map(|l| l.unwrap());
for line in lines {
self.append_row(&line);
self.insert_row(self.rows.len(), &line);
}
self.dirty = 0;