From 689a45de369940a00b4b2503cb3ec2de53125374 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 15 Mar 2021 14:37:41 -0400 Subject: [PATCH] optimize highlight rendering --- src/document.rs | 2 +- src/highlighting.rs | 1 + src/row.rs | 30 ++++++++++++++++++++---------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/src/document.rs b/src/document.rs index 0adb5a3..5686848 100644 --- a/src/document.rs +++ b/src/document.rs @@ -157,7 +157,7 @@ impl Document { #[allow(clippy::indexing_slicing)] let current_row = &mut self.rows[at.y]; - let new_row = self.rows[at.y].split(at.x); + let mut new_row = current_row.split(at.x); current_row.highlight(); new_row.highlight(); diff --git a/src/highlighting.rs b/src/highlighting.rs index 92261da..e7255e4 100644 --- a/src/highlighting.rs +++ b/src/highlighting.rs @@ -1,5 +1,6 @@ use termion::color; +#[derive(PartialEq)] pub enum Type { None, Number, diff --git a/src/row.rs b/src/row.rs index d02ee9f..9ecc186 100644 --- a/src/row.rs +++ b/src/row.rs @@ -27,31 +27,41 @@ impl Row { let start = cmp::min(start, end); let mut result = String::new(); + let mut current_highlighting = &highlighting::Type::None; #[allow(clippy::integer_arithmetic)] - for grapheme in self.string[..] + for (index, grapheme) in self.string[..] .graphemes(true) + .enumerate() .skip(start) .take(end - start) { if let Some(c) = grapheme.chars().next() { + let highlighting_type = self + .highlighting + .get(index) + .unwrap_or(&highlighting::Type::None); + + if highlighting_type != current_highlighting { + current_highlighting = highlighting_type; + + let start_highlight = + format!("{}", termion::color::Fg(highlighting_type.to_color())); + + result.push_str(&start_highlight[..]); + } + if c == '\t' { result.push_str(" "); - } else if c.is_ascii_digit() { - result.push_str( - &format!( - "{}{}{}", - color::Fg(color::Rgb(220, 163, 163)), - c, - color::Fg(color::Reset) - )[..], - ); } else { result.push(c); } } } + let end_highlight = format!("{}", termion::color::Fg(color::Reset)); + result.push_str(&end_highlight[..]); + result }