From e4b08d847605500c858b048e171a2c2c5c65f469 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Sat, 6 Jan 2024 12:34:50 -0800 Subject: Allow scrolling up and down. --- src/main.rs | 32 +++++++++++++++++++++----------- src/rope.rs | 4 ++-- 2 files changed, 23 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index c9e8928..4c93cb7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use term::Key; struct State { buf: Rope, + row_start: usize, cursor_row: usize, cursor_col: usize, line_offset: usize, @@ -46,11 +47,11 @@ impl State { return Ok(pos); } - fn repaint_full(&mut self) -> Result<(), Box> { + fn repaint_all(&mut self) -> Result<(), Box> { let size = term::size()?; write!(stdout(), "\x1b[H\x1b[J")?; - for i in 0..usize::from(size.ws_row) { + for i in self.row_start..self.row_start+usize::from(size.ws_row) { if i > 0 { write!(stdout(), "\r\n")?; } @@ -71,7 +72,7 @@ impl State { let term_size = term::size()?; write!(stdout(), "\x1b[{}H\x1b[K", self.cursor_row+1)?; - let mut line = self.buf.line(self.cursor_row); + let mut line = self.buf.line(self.row_start+self.cursor_row); let max_len = line.char(usize::from(term_size.ws_col-1)); let mut truncated = false; if max_len < line.len() { @@ -122,12 +123,13 @@ fn edit(file: &OsStr) -> Result<(), Box> { let size = term::size()?; let mut state = State{ buf: r, + row_start: 0, cursor_row: 0, cursor_col: 0, line_offset: 0, line_cols: 0, }; - let _ = state.repaint_full(); + let _ = state.repaint_all(); let _ = write!(stdout(), "\x1b[H"); let _ = stdout().flush(); @@ -141,20 +143,28 @@ fn edit(file: &OsStr) -> Result<(), Box> { let _ = state.buf.save(Path::new(file)); } Key::Up => { - if state.cursor_row == 0 { + if state.cursor_row == 0 && state.row_start == 0 { continue; } - state.cursor_row -= 1; - + if state.cursor_row == 0 { + state.row_start -= 1; + let _ = state.repaint_all(); + } else { + state.cursor_row -= 1; + } let _ = state.repaint_line_full(); let _ = stdout().flush(); } Key::Down => { - if state.cursor_row == usize::from(size.ws_row - 1) || state.cursor_row == state.buf.lines() { + if state.row_start+state.cursor_row == state.buf.lines() { continue; } - state.cursor_row += 1; - + if state.cursor_row == usize::from(size.ws_row - 1) { + state.row_start += 1; + let _ = state.repaint_all(); + } else { + state.cursor_row += 1; + } let _ = state.repaint_line_full(); let _ = stdout().flush(); } @@ -183,7 +193,7 @@ fn edit(file: &OsStr) -> Result<(), Box> { } Key::Char(c) => { state.cursor_col = std::cmp::min(state.cursor_col, state.line_cols); - state.buf = state.buf.insert(state.buf.line_idx(state.cursor_row)+state.line_offset, c); + state.buf = state.buf.insert(state.buf.line_idx(state.row_start+state.cursor_row)+state.line_offset, c); state.cursor_col += 1; let _ = state.repaint_line_full(); let _ = stdout().flush(); diff --git a/src/rope.rs b/src/rope.rs index 5f4fca8..11a030b 100644 --- a/src/rope.rs +++ b/src/rope.rs @@ -242,9 +242,9 @@ impl Rope { let start = self.line_idx(n); let mut end = self.len(); if n < self.lines() { - end = self.line_idx(n+1); + end = self.line_idx(n+1)-1; } - return self.slice(start, end-1); + return self.slice(start, end); } fn is_char_boundary(&self, index: usize) -> bool { -- cgit v1.3.1