diff options
| author | Jonas Maier <> | 2026-05-24 19:25:45 +0200 |
|---|---|---|
| committer | Jonas Maier <> | 2026-05-24 19:25:45 +0200 |
| commit | 76ce59936d6d1c03ea291e6631db26339d310c19 (patch) | |
| tree | b0cbff9bf3fac0bb3b9ad9580f6e83bcc51b0056 /src/syntax_highlighting.rs | |
| parent | 01a198b8ad42680c9333039c75317d1787120c78 (diff) | |
| download | pish-76ce59936d6d1c03ea291e6631db26339d310c19.tar.gz | |
less broken multiline editing
Diffstat (limited to 'src/syntax_highlighting.rs')
| -rw-r--r-- | src/syntax_highlighting.rs | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/src/syntax_highlighting.rs b/src/syntax_highlighting.rs index 043509a..19f44ab 100644 --- a/src/syntax_highlighting.rs +++ b/src/syntax_highlighting.rs @@ -1,7 +1,7 @@ use std::collections::HashMap; use crate::{ - BString, ansi, bstr, + BString, ansi, bstr, cursor, parse::{Highlight, HighlightKind}, }; @@ -15,6 +15,8 @@ pub enum SetColorError { NoSuchKeyword, } +const CLEAR_TO_END: &bstr = b"\x1b[K"; + impl Highlighter { pub fn new() -> Self { let mut this = Self { @@ -51,6 +53,7 @@ impl Highlighter { pub fn pretty_print( &self, bytes: &[u8], + cursor_offset: usize, colors: Vec<Highlight>, stdout: &mut dyn std::io::Write, ) -> std::io::Result<()> { @@ -104,7 +107,16 @@ impl Highlighter { let mut current_color = self.color(HighlightKind::None); + let mut saved_pos = false; + + stdout.write_all(CLEAR_TO_END)?; + for (i, x) in bytes.iter().cloned().enumerate() { + if i == cursor_offset { + cursor::f_save(stdout)?; + saved_pos = true; + } + while let Some(color_boundary) = coloring.first().cloned() && color_boundary.loc <= i { @@ -126,6 +138,7 @@ impl Highlighter { if x == b'\n' { stdout.write_all(b"\r\n")?; + stdout.write_all(CLEAR_TO_END)?; } else { stdout.write_all(&[x])?; } @@ -133,6 +146,10 @@ impl Highlighter { stdout.write_all(self.color(HighlightKind::None))?; + if saved_pos { + cursor::f_restore(stdout)?; + } + Ok(()) } } |
