editor.rs (1335B)
1 #[derive(PartialEq)] 2 pub enum Mode { 3 Normal, 4 Insert, 5 } 6 7 pub struct Editor { 8 pub mode: Mode, 9 pub scroll_offset: usize, 10 pub cursor_line: usize, 11 pub cursor_column: usize, 12 } 13 14 impl Editor { 15 pub fn new() -> Self { 16 Self { 17 mode: Mode::Normal, 18 scroll_offset: 0, 19 cursor_line: 0, 20 cursor_column: 0, 21 } 22 } 23 24 pub fn scroll_down(&mut self, total_lines: usize, view_height: usize) { 25 if self.scroll_offset + view_height < total_lines { 26 self.scroll_offset += 1; 27 } 28 } 29 30 pub fn scroll_up(&mut self) { 31 if self.scroll_offset > 0 { 32 self.scroll_offset -= 1; 33 } 34 } 35 36 pub fn move_cursor_down(&mut self, total_lines: usize) { 37 if self.cursor_line + 1 < total_lines { 38 self.cursor_line += 1; 39 } 40 } 41 42 pub fn move_cursor_up(&mut self) { 43 if self.cursor_line > 0 { 44 self.cursor_line -= 1; 45 } 46 } 47 48 pub fn move_cursor_left(&mut self) { 49 if self.cursor_column > 0 { 50 self.cursor_column -= 1; 51 } 52 } 53 54 pub fn move_cursor_right(&mut self, line_len: usize) { 55 if self.cursor_column < line_len { 56 self.cursor_column += 1; 57 } 58 } 59 }