diff options
| author | Rose Hogenson <rosehogenson@posteo.net> | 2023-12-27 10:32:06 -0800 |
|---|---|---|
| committer | Rose Hogenson <rosehogenson@posteo.net> | 2023-12-27 10:32:06 -0800 |
| commit | 38955a421bbf80ea0c2291c3a7b0a5f4034e8077 (patch) | |
| tree | bcba966c6245cbc15abf420415a005132d7c3e33 /src | |
| parent | 82094d878c73532d9ca7b6d2d6fcf91696d59ee8 (diff) | |
| download | editor-38955a421bbf80ea0c2291c3a7b0a5f4034e8077.tar.zst | |
Print each line separately.
Now that the terminal is in raw mode, we need to also include \r after
each line.
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 6 | ||||
| -rw-r--r-- | src/rope.rs | 42 |
2 files changed, 20 insertions, 28 deletions
diff --git a/src/main.rs b/src/main.rs index 63ae538..79bff16 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,7 @@ mod term; use rope::Rope; use std::ffi::{OsStr, OsString}; +use std::io::Write; use std::path::Path; fn edit(file: &OsStr) -> Result<(), String> { @@ -21,8 +22,9 @@ fn edit(file: &OsStr) -> Result<(), String> { } }; let mut stdout = std::io::stdout(); - if let Err(err) = r.print_lines(&mut stdout, 0, usize::from(size.ws_row)) { - return Err(format!("write: {}", err)); + for i in 0..usize::from(size.ws_row) { + let _ = r.print_line(&mut stdout, i); + let _ = stdout.write(b"\r\n"); } return Ok(()); } diff --git a/src/rope.rs b/src/rope.rs index b1b5831..3a01e8b 100644 --- a/src/rope.rs +++ b/src/rope.rs @@ -56,12 +56,7 @@ impl Node { } } - fn print_lines( - &self, - out: &mut dyn Write, - start: usize, - end: usize, - ) -> Result<(), std::io::Error> { + fn print_line(&self, out: &mut dyn Write, n: usize) -> Result<(), std::io::Error> { match self { Node::Leaf(v) => { let mut nl_count = 0; @@ -72,11 +67,12 @@ impl Node { continue; } nl_count += 1; - if nl_count == start { - start_pos = i; + if nl_count == n { + start_pos = i + 1; } - if nl_count == end { - end_pos = i + 1; + if nl_count == n + 1 { + end_pos = i; + break; } } if let Err(err) = out.write(&v[start_pos..end_pos]) { @@ -85,19 +81,18 @@ impl Node { return Ok(()); } Node::Branch(b) => { - let right_start; - if start <= b.left.lines() { - if let Err(err) = b.left.print_lines(out, start, end) { + let left_lines = b.left.lines(); + if n <= left_lines { + if let Err(err) = b.left.print_line(out, n) { return Err(err); } - right_start = 0; - } else { - right_start = start - b.left.lines(); } - if end <= b.left.lines() { - return Ok(()); + if n >= left_lines { + if let Err(err) = b.right.print_line(out, n - left_lines) { + return Err(err); + } } - return b.right.print_lines(out, right_start, end - b.left.lines()); + return Ok(()); } } } @@ -157,14 +152,9 @@ impl Rope { return Ok(rope); } - pub fn print_lines( - &self, - out: &mut dyn Write, - start: usize, - end: usize, - ) -> Result<(), std::io::Error> { + pub fn print_line(&self, out: &mut dyn Write, n: usize) -> Result<(), std::io::Error> { let Rope(me) = self; - return me.print_lines(out, start, end); + return me.print_line(out, n); } } |
