add page up/down faster scroll

This commit is contained in:
Crispy 2023-03-05 21:24:15 +01:00
parent 306ee70381
commit 79e4364f5a

View file

@ -144,6 +144,8 @@ impl Editor {
| KeyCode::Up | KeyCode::Up
| KeyCode::Down | KeyCode::Down
| KeyCode::Home | KeyCode::Home
| KeyCode::PageUp
| KeyCode::PageDown
| KeyCode::End = event.code | KeyCode::End = event.code
{ {
if event.modifiers.contains(KeyModifiers::SHIFT) { if event.modifiers.contains(KeyModifiers::SHIFT) {
@ -151,11 +153,14 @@ impl Editor {
} else { } else {
self.marker = None; self.marker = None;
} }
let height = terminal::size().unwrap().1 as usize;
match event.code { match event.code {
KeyCode::Left => self.move_left(), KeyCode::Left => self.move_left(),
KeyCode::Right => self.move_right(), KeyCode::Right => self.move_right(),
KeyCode::Up => self.move_up(), KeyCode::Up => self.move_up(1),
KeyCode::Down => self.move_down(), KeyCode::Down => self.move_down(1),
KeyCode::PageUp => self.move_up(height),
KeyCode::PageDown => self.move_down(height),
KeyCode::Home => self.move_home(), KeyCode::Home => self.move_home(),
KeyCode::End => self.move_end(), KeyCode::End => self.move_end(),
_ => (), _ => (),
@ -234,6 +239,7 @@ impl Editor {
self.cursor.line -= 1; self.cursor.line -= 1;
self.cursor.column = self.current_line().len(); self.cursor.column = self.current_line().len();
} }
self.scroll_to_cursor()
} }
fn move_right(&mut self) { fn move_right(&mut self) {
@ -243,36 +249,43 @@ impl Editor {
self.cursor.line += 1; self.cursor.line += 1;
self.cursor.column = 0; self.cursor.column = 0;
} }
self.scroll_to_cursor()
} }
fn move_up(&mut self) { fn move_up(&mut self, lines: usize) {
if self.cursor.line > 0 { let physical_column = self.text
let physical_column = self.text [self.current_line().start..(self.current_line().start + self.cursor.column)]
[self.current_line().start..(self.current_line().start + self.cursor.column)] .chars()
.chars() .count();
.count(); self.cursor.line = self.cursor.line.saturating_sub(lines);
self.cursor.line -= 1; self.cursor.column = physical_column.min(self.current_line().len());
self.cursor.column = physical_column.min(self.current_line().len()); self.ensure_char_boundary();
self.ensure_char_boundary(); self.scroll_to_cursor();
if self.cursor.line < self.scroll {
self.scroll -= 1;
}
}
} }
fn move_down(&mut self) { fn move_down(&mut self, lines: usize) {
if self.cursor.line < self.lines.len() - 1 { let physical_column = self.text
let physical_column = self.text [self.current_line().start..(self.current_line().start + self.cursor.column)]
[self.current_line().start..(self.current_line().start + self.cursor.column)] .chars()
.chars() .count();
.count(); self.cursor.line = (self.cursor.line + lines).min(self.lines.len() - 1);
self.cursor.line += 1; self.cursor.column = physical_column.min(self.current_line().len());
self.cursor.column = physical_column.min(self.current_line().len()); self.ensure_char_boundary();
self.ensure_char_boundary(); self.scroll_to_cursor();
if self.cursor.line > (self.scroll + terminal::size().unwrap().1 as usize - 2) { }
self.scroll += 1;
} fn scroll_to_cursor(&mut self) {
} // while self.cursor.line < self.scroll {
// self.scroll -= 1;
// }
// while self.cursor.line > (self.scroll + terminal::size().unwrap().1 as usize - 2) {
// self.scroll += 1;
// }
self.scroll = self.scroll.min(self.cursor.line);
let height = terminal::size().unwrap().1 as usize - 2;
self.scroll = self
.scroll
.max(self.scroll + self.cursor.line.saturating_sub(self.scroll + height));
} }
fn move_home(&mut self) { fn move_home(&mut self) {