aboutsummaryrefslogtreecommitdiffstats
path: root/src/rope.rs
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2024-01-13 17:45:37 -0800
committerRose Hogenson <rosehogenson@posteo.net>2024-01-13 17:45:37 -0800
commitd3f52cf5dc1e06a81fb75a8631ac00e8adc04ac7 (patch)
tree5d4c71c7e2b2d8b6490a8afb57d7ba209637ce1e /src/rope.rs
parent1e32f5d480ecefa3a1c8fd1c43ac6b4260459266 (diff)
downloadeditor-d3f52cf5dc1e06a81fb75a8631ac00e8adc04ac7.tar.zst
Escape control characters.
Diffstat (limited to 'src/rope.rs')
-rw-r--r--src/rope.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/src/rope.rs b/src/rope.rs
index ab9a7bc..ec25f61 100644
--- a/src/rope.rs
+++ b/src/rope.rs
@@ -342,7 +342,16 @@ impl Rope {
pub fn print(&self, w: &mut dyn Write) -> Result<(), std::io::Error> {
match self {
Rope(Node::Leaf(l)) => {
- w.write_all(l.buf().as_bytes())?;
+ for c in l.buf().chars() {
+ if c < ' ' || c == '\x7f' {
+ let b = u8::try_from(c).expect("c should be less than \\x7f");
+ write!(w, "\x1b[36m^{}\x1b[m", char::from(b ^ 0x40))?;
+ } else if '\u{80}' <= c && c <= '\u{9f}' {
+ write!(w, "\x1b[36m<{:x}>\x1b[m", u32::from(c))?;
+ } else {
+ write!(w, "{}", c)?;
+ }
+ }
Ok(())
}
Rope(Node::Branch(b)) => {
@@ -354,6 +363,20 @@ impl Rope {
}
}
+ pub fn write(&self, w: &mut dyn Write) -> Result<(), std::io::Error> {
+ match self {
+ Rope(Node::Leaf(l)) => {
+ w.write_all(l.buf().as_bytes())?;
+ }
+ Rope(Node::Branch(b)) => {
+ for c in b.children.iter() {
+ c.write(w)?;
+ }
+ }
+ }
+ Ok(())
+ }
+
pub fn slice(&self, start: usize, end: usize) -> Rope {
if start > self.len() {
panic!(