From 4618b2be0e9bf48d90fbb10db60c922c8e27fb47 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Mon, 15 Mar 2021 13:53:30 -0400 Subject: [PATCH] Build up highlighting array --- src/document.rs | 12 +++++++++++- src/highlighting.rs | 15 +++++++++++++++ src/main.rs | 1 + src/row.rs | 18 ++++++++++++++++++ 4 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 src/highlighting.rs diff --git a/src/document.rs b/src/document.rs index d27f3fb..402bf79 100644 --- a/src/document.rs +++ b/src/document.rs @@ -18,7 +18,9 @@ impl Document { let mut rows = Vec::new(); for value in contents.lines() { - rows.push(Row::from(value)); + let mut row = Row::From(value); + row.highlight(); + rows.push(row); } Ok(Self { @@ -56,11 +58,13 @@ impl Document { if at.y == self.rows.len() { let mut row = Row::default(); row.insert(0, c); + row.highlight(); self.rows.push(row); } else { #[allow(clippy::indexing_slicing)] let row = &mut self.rows[at.y]; row.insert(at.x, c); + row.highlight(); } } @@ -78,9 +82,11 @@ impl Document { let next_row = self.rows.remove(at.y + 1); let row = &mut self.rows[at.y]; row.append(&next_row); + row.highlight(); } else { let row = &mut self.rows[at.y]; row.delete(at.x); + row.highlight(); } } @@ -150,8 +156,12 @@ impl Document { } #[allow(clippy::indexing_slicing)] + let current_row = &mut self.rows[at.y]; let new_row = self.rows[at.y].split(at.x); + current_row.highlight(); + new_row.highlight(); + #[allow(clippy::integer_arithmetic)] self.rows.insert(at.y + 1, new_row); } diff --git a/src/highlighting.rs b/src/highlighting.rs new file mode 100644 index 0000000..92261da --- /dev/null +++ b/src/highlighting.rs @@ -0,0 +1,15 @@ +use termion::color; + +pub enum Type { + None, + Number, +} + +impl Type { + pub fn to_color(&self) -> impl color::Color { + match self { + Type::Number => color::Rgb(220, 163, 163), + _ => color::Rgb(255, 255, 255), + } + } +} diff --git a/src/main.rs b/src/main.rs index 1c064a6..80a8e98 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,7 @@ #![allow(clippy::missing_errors_doc, clippy::must_use_candidate, clippy::panic)] mod document; mod editor; +mod highlighting; mod row; mod terminal; diff --git a/src/row.rs b/src/row.rs index c9c0ca0..d02ee9f 100644 --- a/src/row.rs +++ b/src/row.rs @@ -1,3 +1,4 @@ +use crate::highlighting; use crate::SearchDirection; use std::cmp; use termion::color; @@ -6,6 +7,7 @@ use unicode_segmentation::UnicodeSegmentation; #[derive(Default)] pub struct Row { string: String, + highlighting: Vec, len: usize, } @@ -13,6 +15,7 @@ impl From<&str> for Row { fn from(slice: &str) -> Self { Self { string: String::from(slice), + highlighting: Vec::new(), len: slice.graphemes(true).count(), } } @@ -133,6 +136,7 @@ impl Row { Self { string: splitted_row, len: splittend_length, + highlighting: Vec::new(), } } @@ -174,4 +178,18 @@ impl Row { None } + + pub fn highlight(&mut self) { + let mut highlighting = Vec::new(); + + for c in self.string.chars() { + if c.is_ascii_digit() { + highlighting.push(highlighting::Type::Number) + } else { + highlighting.push(highlighting::Type::None) + } + } + + self.highlighting = highlighting; + } }