optimize highlight rendering

This commit is contained in:
Timothy Warren 2021-03-15 14:37:41 -04:00
parent 101f99c825
commit 689a45de36
3 changed files with 22 additions and 11 deletions

View File

@ -157,7 +157,7 @@ impl Document {
#[allow(clippy::indexing_slicing)] #[allow(clippy::indexing_slicing)]
let current_row = &mut self.rows[at.y]; 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(); current_row.highlight();
new_row.highlight(); new_row.highlight();

View File

@ -1,5 +1,6 @@
use termion::color; use termion::color;
#[derive(PartialEq)]
pub enum Type { pub enum Type {
None, None,
Number, Number,

View File

@ -27,31 +27,41 @@ impl Row {
let start = cmp::min(start, end); let start = cmp::min(start, end);
let mut result = String::new(); let mut result = String::new();
let mut current_highlighting = &highlighting::Type::None;
#[allow(clippy::integer_arithmetic)] #[allow(clippy::integer_arithmetic)]
for grapheme in self.string[..] for (index, grapheme) in self.string[..]
.graphemes(true) .graphemes(true)
.enumerate()
.skip(start) .skip(start)
.take(end - start) .take(end - start)
{ {
if let Some(c) = grapheme.chars().next() { 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' { if c == '\t' {
result.push_str(" "); 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 { } else {
result.push(c); result.push(c);
} }
} }
} }
let end_highlight = format!("{}", termion::color::Fg(color::Reset));
result.push_str(&end_highlight[..]);
result result
} }