From f50c6f210a353d8b12552f6a20bf58c2a7bdd3df Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Fri, 5 Jan 2024 19:13:48 -0800 Subject: Clean up the rope code. --- src/main.rs | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index d376c2d..2b359f8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -38,12 +38,10 @@ fn edit(file: &OsStr) -> Result<(), String> { if i > 0 { let _ = stdout.write(b"\r\n"); } - let line = match r.line(i) { - Some(line) => line, - None => { - break; - } - }; + if i > r.lines() { + break; + } + let line = r.line(i); // TODO: handle line-wrapping. let _ = line.print(&mut stdout); } @@ -78,7 +76,6 @@ fn edit(file: &OsStr) -> Result<(), String> { let len = r .line(cursor_row) - .expect("cursor_row should always be valid") .len(); if cursor_col > len { move_cursor(&mut stdout, cursor_row, len); @@ -94,7 +91,6 @@ fn edit(file: &OsStr) -> Result<(), String> { let len = r .line(cursor_row) - .expect("cursor_row should always be valid") .len(); if cursor_col > len { move_cursor(&mut stdout, cursor_row, len); @@ -105,7 +101,6 @@ fn edit(file: &OsStr) -> Result<(), String> { Key::Left => { let len = r .line(cursor_row) - .expect("cursor_row should always be valid") .len(); cursor_col = std::cmp::min(cursor_col, len); if cursor_col == 0 { @@ -117,7 +112,6 @@ fn edit(file: &OsStr) -> Result<(), String> { Key::Right => { let len = r .line(cursor_row) - .expect("cursor_row should always be valid") .len(); if cursor_col >= len { continue; @@ -126,15 +120,11 @@ fn edit(file: &OsStr) -> Result<(), String> { move_cursor(&mut stdout, cursor_row, cursor_col); } Key::Char(c) => { - let line = r - .line(cursor_row) - .expect("cursor_row should always be valid"); - cursor_col = std::cmp::min(cursor_col, line.len()); - r = line.insert(cursor_col, c); + cursor_col = std::cmp::min(cursor_col, r.line(cursor_row).len()); + r = r.insert(r.line_idx(cursor_row)+cursor_col, c); move_cursor(&mut stdout, cursor_row, 0); let _ = r .line(cursor_row) - .expect("cursor_row should always be valid") .print(&mut stdout); cursor_col += 1; move_cursor(&mut stdout, cursor_row, cursor_col); -- cgit v1.3.1