aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2024-01-13 12:31:42 -0800
committerRose Hogenson <rosehogenson@posteo.net>2024-01-13 12:31:42 -0800
commit1e32f5d480ecefa3a1c8fd1c43ac6b4260459266 (patch)
tree2c268543df0f19fa424a8d01b642e23c3d38771e /src/main.rs
parentd8df9a6e5f09a0f9649ef16d15f86a22c4720ade (diff)
downloadeditor-1e32f5d480ecefa3a1c8fd1c43ac6b4260459266.tar.zst
Reject files with invalid utf-8.
It's easier to only support files with valid utf-8.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs43
1 files changed, 21 insertions, 22 deletions
diff --git a/src/main.rs b/src/main.rs
index 5b9d2f5..0cc9ad4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -8,7 +8,7 @@ use std::fs::File;
use std::io::{stderr, stdout, Write};
use std::path::{Path, PathBuf};
-fn open(file: &Path) -> Result<Rope, std::io::Error> {
+fn open(file: &Path) -> Result<Rope, Box<dyn Error>> {
let mut f = File::open(file)?;
let r = Rope::read(&mut f)?;
Ok(r)
@@ -38,10 +38,7 @@ fn line_offset(stdin: &mut Reader, line: &Rope, col: u16) -> Result<usize, Box<d
let mut lo = 0;
let mut hi = line.len() + 1;
while lo < hi {
- let mut h = lo + (hi - lo) / 2;
- if h < line.len() {
- h = line.floor_char_boundary(h);
- }
+ let h = line.floor_char_boundary(lo + (hi - lo) / 2);
write!(stdout(), "\x1b[G")?;
line.slice(0, h).print(&mut stdout())?;
let (_, c) = stdin.cursor_pos()?;
@@ -234,12 +231,12 @@ impl State {
}
fn keypress(&mut self, key: Key) -> Result<bool, Box<dyn Error>> {
- const CTRL_Q: u8 = b'Q' - b'@';
- const CTRL_S: u8 = b'S' - b'@';
- const CTRL_Y: u8 = b'Y' - b'@';
- const CTRL_Z: u8 = b'Z' - b'@';
- const BACKSPACE: u8 = b'H' - b'@';
- const OTHER_BACKSPACE: u8 = 127;
+ const CTRL_Q: char = (b'Q' - b'@') as char;
+ const CTRL_S: char = (b'S' - b'@') as char;
+ const CTRL_Y: char = (b'Y' - b'@') as char;
+ const CTRL_Z: char = (b'Z' - b'@') as char;
+ const BACKSPACE: char = (b'H' - b'@') as char;
+ const OTHER_BACKSPACE: char = '\x7f';
let inserting = self.inserting;
self.inserting = false;
@@ -446,15 +443,15 @@ impl State {
self.update_cursor_col()?;
stdout().flush()?;
}
- Key::Byte(CTRL_Q) => {
+ Key::Char(CTRL_Q) => {
return Ok(false);
}
- Key::Byte(CTRL_S) => {
+ Key::Char(CTRL_S) => {
let mut f = File::create(&self.path)?;
self.buf.print(&mut f)?;
f.sync_all()?;
}
- Key::Byte(CTRL_Z) => {
+ Key::Char(CTRL_Z) => {
if let Some(n) = self.n {
if n > 0 {
self.n = Some(n - 1);
@@ -468,7 +465,7 @@ impl State {
self.update_cursor_col()?;
stdout().flush()?;
}
- Key::Byte(CTRL_Y) => {
+ Key::Char(CTRL_Y) => {
let Some(n) = self.n else {
return Ok(true);
};
@@ -481,7 +478,7 @@ impl State {
self.update_cursor_col()?;
stdout().flush()?;
}
- Key::Byte(BACKSPACE) | Key::Byte(OTHER_BACKSPACE) => {
+ Key::Char(BACKSPACE) | Key::Char(OTHER_BACKSPACE) => {
self.insert(inserting);
if self.line_offset == 0 && self.cursor_row == 0 && self.row_start == 0 {
return Ok(true);
@@ -519,7 +516,7 @@ impl State {
self.update_cursor_col()?;
stdout().flush()?;
}
- Key::Byte(b'\r') | Key::Byte(b'\n') => {
+ Key::Char('\r') | Key::Char('\n') => {
self.insert(inserting);
let size = term::size()?;
let offset = self
@@ -529,7 +526,7 @@ impl State {
self.buf = self
.buf
.slice(0, offset)
- .concat(&Rope::new(&[b'\n']))
+ .concat(&Rope::new("\n"))
.concat(&self.buf.slice(offset, self.buf.len()));
if self.cursor_row < size.ws_row - 1 {
self.cursor_row += 1;
@@ -542,20 +539,22 @@ impl State {
write!(stdout(), "\r\n")?;
stdout().flush()?;
}
- Key::Byte(c) => {
+ Key::Char(c) => {
self.insert(inserting);
let offset = self
.buf
.line_start(self.row_start + usize::from(self.cursor_row))
+ self.line_offset;
+ let mut buf = [0; 4];
+ let s = c.encode_utf8(&mut buf);
self.buf = self
.buf
.slice(0, offset)
- .concat(&Rope::new(&[c]))
+ .concat(&Rope::new(s))
.concat(&self.buf.slice(offset, self.buf.len()));
- self.line_offset += 1;
+ self.line_offset += s.len();
self.need_repaint_line = true;
- stdout().write_all(&[c])?;
+ stdout().write_all(s.as_bytes())?;
stdout().flush()?;
}
}