handle end of file and empty files properly
This commit is contained in:
parent
b75fb1ae47
commit
c6090b3cc1
1 changed files with 22 additions and 7 deletions
|
@ -9,6 +9,7 @@ use termion::{
|
||||||
event::{Event, Key},
|
event::{Event, Key},
|
||||||
input::TermRead,
|
input::TermRead,
|
||||||
raw::IntoRawMode,
|
raw::IntoRawMode,
|
||||||
|
terminal_size,
|
||||||
};
|
};
|
||||||
|
|
||||||
const TAB_SIZE: usize = 4;
|
const TAB_SIZE: usize = 4;
|
||||||
|
@ -92,7 +93,7 @@ impl Editor {
|
||||||
fn move_right(&mut self) {
|
fn move_right(&mut self) {
|
||||||
if self.cursor.column < self.current_line().len() {
|
if self.cursor.column < self.current_line().len() {
|
||||||
self.cursor.column += 1;
|
self.cursor.column += 1;
|
||||||
} else if self.cursor.line < self.lines.len() {
|
} else if self.cursor.line < self.lines.len() - 1 {
|
||||||
self.cursor.line += 1;
|
self.cursor.line += 1;
|
||||||
self.cursor.column = 0;
|
self.cursor.column = 0;
|
||||||
}
|
}
|
||||||
|
@ -106,7 +107,7 @@ impl Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn move_down(&mut self) {
|
fn move_down(&mut self) {
|
||||||
if self.cursor.line < self.lines.len() {
|
if self.cursor.line < self.lines.len() - 1 {
|
||||||
self.cursor.line += 1;
|
self.cursor.line += 1;
|
||||||
self.cursor.column = self.cursor.column.min(self.current_line().len());
|
self.cursor.column = self.cursor.column.min(self.current_line().len());
|
||||||
}
|
}
|
||||||
|
@ -126,6 +127,8 @@ impl Editor {
|
||||||
this_line.start = index + 1;
|
this_line.start = index + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
this_line.end = self.text.chars().count();
|
||||||
|
self.lines.push(this_line);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn draw(&self) {
|
fn draw(&self) {
|
||||||
|
@ -139,6 +142,13 @@ impl Editor {
|
||||||
text.replace('\t', &" ".repeat(TAB_SIZE))
|
text.replace('\t', &" ".repeat(TAB_SIZE))
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
print!(
|
||||||
|
"{}{}, {}",
|
||||||
|
cursor::Goto(1, terminal_size().unwrap().1),
|
||||||
|
self.cursor.line,
|
||||||
|
self.cursor.column
|
||||||
|
);
|
||||||
|
|
||||||
print!(
|
print!(
|
||||||
"{}",
|
"{}",
|
||||||
cursor::Goto(
|
cursor::Goto(
|
||||||
|
@ -150,21 +160,26 @@ impl Editor {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn insert_char(&mut self, ch: char) {
|
fn insert_char(&mut self, ch: char) {
|
||||||
|
// eprintln!("inserting {ch} at {}", self.index());
|
||||||
self.text.insert(self.index(), ch);
|
self.text.insert(self.index(), ch);
|
||||||
self.find_lines();
|
self.find_lines();
|
||||||
self.move_right();
|
self.move_right();
|
||||||
}
|
}
|
||||||
|
|
||||||
fn backspace(&mut self) {
|
fn backspace(&mut self) {
|
||||||
|
if self.index() > 0 {
|
||||||
self.text.remove(self.index() - 1);
|
self.text.remove(self.index() - 1);
|
||||||
self.find_lines();
|
|
||||||
self.move_left();
|
self.move_left();
|
||||||
|
self.find_lines();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn delete(&mut self) {
|
fn delete(&mut self) {
|
||||||
|
if self.index() < self.text.len() {
|
||||||
self.text.remove(self.index());
|
self.text.remove(self.index());
|
||||||
self.find_lines();
|
self.find_lines();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn index(&self) -> usize {
|
fn index(&self) -> usize {
|
||||||
self.current_line().start + self.cursor.column
|
self.current_line().start + self.cursor.column
|
||||||
|
|
Loading…
Reference in a new issue