aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2024-01-06 12:34:50 -0800
committerRose Hogenson <rosehogenson@posteo.net>2024-01-06 12:34:50 -0800
commite4b08d847605500c858b048e171a2c2c5c65f469 (patch)
tree864bbe73a43f8969845c0b31a6794ea2f7abbcd4 /src/main.rs
parentbefdde33f29ab435b328c3bc501562e2e9386a8e (diff)
downloadeditor-e4b08d847605500c858b048e171a2c2c5c65f469.tar.zst
Allow scrolling up and down.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs32
1 files changed, 21 insertions, 11 deletions
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<dyn Error>> {
+ fn repaint_all(&mut self) -> Result<(), Box<dyn Error>> {
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<dyn Error>> {
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<dyn Error>> {
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<dyn Error>> {
}
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();