aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2023-12-29 16:13:59 -0800
committerRose Hogenson <rosehogenson@posteo.net>2023-12-29 16:13:59 -0800
commitda54c7c2c103c9b1c98f0c5179945b0122a1eef7 (patch)
treec11c0a4595e7569bd99e946250ed01575cec27f9 /src/main.rs
parent52591143ae4211555c955a7f9304684f75a59660 (diff)
downloadeditor-da54c7c2c103c9b1c98f0c5179945b0122a1eef7.tar.zst
Support inserting text.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs19
1 files changed, 15 insertions, 4 deletions
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);
}
}
}