Skip to content

Commit

Permalink
add colourful search results
Browse files Browse the repository at this point in the history
  • Loading branch information
artem-streltsov committed Feb 22, 2024
1 parent e300535 commit f458dcd
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 11 deletions.
19 changes: 12 additions & 7 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl Document {
let mut rows = Vec::new();
for value in contents.lines() {
let mut row = Row::from(value);
row.highlight();
row.highlight(None);
rows.push(row);
}
Ok(Self {
Expand Down Expand Up @@ -47,13 +47,13 @@ impl Document {
if at.y == self.rows.len() {
let mut row = Row::default();
row.insert(0, c);
row.highlight();
row.highlight(None);
self.rows.push(row);
} else {
#[allow(clippy::indexing_slicing)]
let row = &mut self.rows[at.y];
row.insert(at.x, c);
row.highlight();
row.highlight(None);
}
}
#[allow(clippy::indexing_slicing)]
Expand All @@ -68,11 +68,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();
row.highlight(None);
} else {
let row = &mut self.rows[at.y];
row.delete(at.x);
row.highlight();
row.highlight(None);
}
}
fn insert_newline(&mut self, at: &Position) {
Expand All @@ -86,8 +86,8 @@ impl Document {
#[allow(clippy::indexing_slicing)]
let current_row = &mut self.rows[at.y];
let mut new_row = current_row.split(at.x);
current_row.highlight();
new_row.highlight();
current_row.highlight(None);
new_row.highlight(None);
self.rows.insert(at.y + 1, new_row);
}
pub fn save(&mut self) -> Result<(), Error> {
Expand Down Expand Up @@ -142,4 +142,9 @@ impl Document {
}
None
}
pub fn highlight(&mut self, word: Option<&str>) {
for row in &mut self.rows {
row.highlight(word);
}
}
}
2 changes: 2 additions & 0 deletions src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,13 +387,15 @@ impl Editor {
} else if moved {
editor.move_cursor(Key::Left);
}
editor.document.highlight(Some(query));
}
).unwrap_or(None);

if query.is_none() {
self.cursor_position = old_position;
self.scroll();
}
self.document.highlight(None);
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/highlighting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ use termion::color;
#[derive(PartialEq)]
pub enum Type {
None,
Number
Number,
Match
}

impl Type {
pub fn to_color(&self) -> impl color::Color {
match self {
Type::Number => color::Rgb(220, 163, 163),
Type::Match => color::Rgb(38, 139, 210),
_ => color::Rgb(255, 255, 255)
}
}
Expand Down
33 changes: 30 additions & 3 deletions src/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ impl Row {
self.string.as_bytes()
}
pub fn find(&self, query: &str, at: usize, direction: SearchDirection) -> Option<usize> {
if at > self.len {
if at > self.len || query.is_empty() {
return None;
}
let start = if direction == SearchDirection::Forward {
Expand Down Expand Up @@ -151,14 +151,41 @@ impl Row {
}
None
}
pub fn highlight(&mut self) {
pub fn highlight(&mut self, word: Option<&str>) {
let mut highlighting = Vec::new();
for c in self.string.chars() {
let chars: Vec<char> = self.string.chars().collect();
let mut matches = Vec::new();
let mut search_index = 0;

if let Some(word) = word {
while let Some(search_match) = self.find(word, search_index, SearchDirection::Forward) {
matches.push(search_match);
if let Some(next_index) = search_match.checked_add(word[..].graphemes(true).count()) {
search_index = next_index;
} else {
break;
}
}
}

let mut index = 0;
while let Some(c) = chars.get(index) {
if let Some(word) = word {
if matches.contains(&index) {
for _ in word[..].graphemes(true) {
index += 1;
highlighting.push(highlighting::Type::Match);
}
continue;
}
}

if c.is_ascii_digit() {
highlighting.push(highlighting::Type::Number);
} else {
highlighting.push(highlighting::Type::None);
}
index += 1;
}
self.highlighting = highlighting;
}
Expand Down

0 comments on commit f458dcd

Please sign in to comment.