aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/main.rs12
-rw-r--r--src/rope.rs37
2 files changed, 8 insertions, 41 deletions
diff --git a/src/main.rs b/src/main.rs
index 139b4c3..6251ace 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -59,12 +59,9 @@ impl State {
if i > self.buf.lines() {
break;
}
- let mut line = self.buf.line(i);
+ let line = self.buf.line(i);
let max_len = line.char(usize::from(size.ws_col-1));
- if max_len < line.len() {
- line = line.slice(0, max_len);
- }
- line.print(&mut stdout)?;
+ line.slice(0, max_len).print(&mut stdout)?;
if max_len < line.len() {
write!(stdout, "\x1b[30m\x1b[47m>\x1b[m")?;
}
@@ -103,6 +100,11 @@ impl State {
if col == self.cursor_col {
// One codepoint per column, like God intended.
self.line_offset = offset_guess;
+ line.slice(offset_guess, line.len()).print(&mut stdout)?;
+ if truncated {
+ write!(stdout, "\x1b[30m\x1b[47m>\x1b[m")?;
+ }
+ write!(stdout, "\x1b[{}G", self.cursor_col+1)?;
return Ok(());
}
diff --git a/src/rope.rs b/src/rope.rs
index 25da1fc..34c459b 100644
--- a/src/rope.rs
+++ b/src/rope.rs
@@ -142,42 +142,7 @@ impl Rope {
}
pub fn insert(&self, pos: usize, c: u8) -> Rope {
- match self {
- Rope(Node::Leaf(l)) => {
- if self.len() < MAX_NODE_SIZE {
- let mut new_buf = vec![0; self.len() + 1];
- new_buf[..pos].copy_from_slice(&l.bytes()[..pos]);
- new_buf[pos] = c;
- new_buf[pos + 1..].copy_from_slice(&l.bytes()[pos..]);
- return Rope::leaf(new_buf);
- }
- let half = MAX_NODE_SIZE / 2;
- let mut buf_left;
- let mut buf_right;
- if pos <= half {
- buf_left = vec![0; half+1];
- buf_right = vec![0; half];
- buf_left[..pos].copy_from_slice(&l.bytes()[..pos]);
- buf_left[pos] = c;
- buf_left[pos+1..].copy_from_slice(&l.bytes()[pos..half]);
- buf_right.copy_from_slice(&l.bytes()[half..]);
- } else {
- buf_left = vec![0; half];
- buf_right = vec![0; half+1];
- buf_left.copy_from_slice(&l.bytes()[..half]);
- buf_right[..pos-half].copy_from_slice(&l.bytes()[half..pos]);
- buf_right[pos-half] = c;
- buf_right[pos-half+1..].copy_from_slice(&l.bytes()[pos..]);
- }
- return Rope::leaf(buf_left).concat(&Rope::leaf(buf_right));
- }
- Rope(Node::Branch(b)) => {
- if pos < b.left.len() {
- return b.left.insert(pos, c).concat(&b.right);
- }
- return b.left.concat(&b.right.insert(pos - b.left.len(), c));
- }
- }
+ return self.slice(0, pos).concat(&Rope::leaf(vec![c])).concat(&self.slice(pos, self.len()));
}
pub fn open(path: &Path) -> Result<Rope, Box<dyn Error>> {