From 4bf6d8098885d86c4a8f437e43e4aaffce0004c8 Mon Sep 17 00:00:00 2001 From: Joscha Date: Sat, 6 Aug 2022 23:54:53 +0200 Subject: [PATCH] Move to start/end of editor line --- src/ui/util.rs | 4 ++++ src/ui/widgets/editor.rs | 22 ++++++++++++++++++++++ 2 files changed, 26 insertions(+) diff --git a/src/ui/util.rs b/src/ui/util.rs index d4fde12..67e258f 100644 --- a/src/ui/util.rs +++ b/src/ui/util.rs @@ -54,6 +54,8 @@ pub fn list_editor_key_bindings( // Cursor movement bindings.binding("ctrl+b, ←", "move cursor left"); bindings.binding("ctrl+f, →", "move cursor right"); + bindings.binding("ctrl+a, home", "move cursor to start of line"); + bindings.binding("ctrl+e, end", "move cursor to end of line"); bindings.binding("↑/↓", "move cursor up/down"); } @@ -84,6 +86,8 @@ pub fn handle_editor_key_event( // Cursor movement key!(Ctrl + 'b') | key!(Left) => editor.move_cursor_left(terminal.frame()), key!(Ctrl + 'f') | key!(Right) => editor.move_cursor_right(terminal.frame()), + key!(Ctrl + 'a') | key!(Home) => editor.move_cursor_to_start_of_line(terminal.frame()), + key!(Ctrl + 'e') | key!(End) => editor.move_cursor_to_end_of_line(terminal.frame()), key!(Up) => editor.move_cursor_up(terminal.frame()), key!(Down) => editor.move_cursor_down(terminal.frame()), diff --git a/src/ui/widgets/editor.rs b/src/ui/widgets/editor.rs index 53db6ec..e8a3d6e 100644 --- a/src/ui/widgets/editor.rs +++ b/src/ui/widgets/editor.rs @@ -234,6 +234,20 @@ impl InnerEditorState { } } + fn move_cursor_to_start_of_line(&mut self, frame: &mut Frame) { + let boundaries = self.line_boundaries(); + let (line, _, _) = self.cursor_line(&boundaries); + self.move_cursor_to_line_col(frame, line, 0); + self.record_cursor_col(frame); + } + + fn move_cursor_to_end_of_line(&mut self, frame: &mut Frame) { + let boundaries = self.line_boundaries(); + let (line, _, _) = self.cursor_line(&boundaries); + self.move_cursor_to_line_col(frame, line, usize::MAX); + self.record_cursor_col(frame); + } + fn move_cursor_up(&mut self, frame: &mut Frame) { let boundaries = self.line_boundaries(); let (line, _, _) = self.cursor_line(&boundaries); @@ -312,6 +326,14 @@ impl EditorState { self.0.lock().move_cursor_right(frame); } + pub fn move_cursor_to_start_of_line(&self, frame: &mut Frame) { + self.0.lock().move_cursor_to_start_of_line(frame); + } + + pub fn move_cursor_to_end_of_line(&self, frame: &mut Frame) { + self.0.lock().move_cursor_to_end_of_line(frame); + } + pub fn move_cursor_up(&self, frame: &mut Frame) { self.0.lock().move_cursor_up(frame); }