From da54c7c2c103c9b1c98f0c5179945b0122a1eef7 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Fri, 29 Dec 2023 16:13:59 -0800 Subject: Support inserting text. --- src/main.rs | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index de25e9c..dd20c3f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -20,7 +20,7 @@ fn edit(file: &OsStr) -> Result<(), String> { } }; - let r = match Rope::open(Path::new(file)) { + let mut r = match Rope::open(Path::new(file)) { Ok(r) => r, Err(err) => { return Err(format!("open file {}: {}", file.to_string_lossy(), err)); @@ -44,6 +44,7 @@ fn edit(file: &OsStr) -> Result<(), String> { break; } }; + // TODO: handle line-wrapping. let _ = line.print(&mut stdout); } let _ = stdout.write(b"\x1b[H"); @@ -113,15 +114,25 @@ fn edit(file: &OsStr) -> Result<(), String> { .line(cursor_row) .expect("cursor_row should always be valid") .len(); - if cursor_col == usize::from(size.ws_col - 1) || cursor_col >= len { + if cursor_col >= len { continue; } cursor_col += 1; move_cursor(&mut stdout, cursor_row, cursor_col); } Key::Char(c) => { - print!("{} ", c); - let _ = stdout.flush(); + 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); + 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