use crate::highlighting; use crate::HighlightingOptions; use crate::SearchDirection; use std::cmp; use termion::color; use unicode_segmentation::UnicodeSegmentation; #[derive(Default)] pub struct Row { string: String, highlighting: Vec, len: usize, } impl From<&str> for Row { fn from(slice: &str) -> Self { Self { string: String::from(slice), highlighting: Vec::new(), len: slice.graphemes(true).count(), } } } impl Row { pub fn render(&self, start: usize, end: usize) -> String { let end = cmp::min(end, self.string.len()); let start = cmp::min(start, end); let mut result = String::new(); let mut current_highlighting = &highlighting::Type::None; #[allow(clippy::integer_arithmetic)] 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 { result.push(c); } } } let end_highlight = format!("{}", termion::color::Fg(color::Reset)); result.push_str(&end_highlight[..]); result } pub fn len(&self) -> usize { self.len } pub fn is_empty(&self) -> bool { self.len == 0 } pub fn insert(&mut self, at: usize, c: char) { if at >= self.len() { self.string.push(c); self.len += 1; return; } let mut result = String::new(); let mut length = 0; for (index, grapheme) in self.string[..].graphemes(true).enumerate() { length += 1; if index == at { length += 1; result.push(c); } result.push_str(grapheme); } self.len = length; self.string = result; } #[allow(clippy::integer_arithmetic)] pub fn delete(&mut self, at: usize) { if at >= self.len() { return; } let mut result = String::new(); let mut length = 0; for (index, grapheme) in self.string[..].graphemes(true).enumerate() { if index != at { length += 1; result.push_str(grapheme); } } self.len = length; self.string = result; } pub fn append(&mut self, new: &Self) { self.string = format!("{}{}", self.string, new.string); self.len += new.len; } pub fn split(&mut self, at: usize) -> Self { let mut row = String::new(); let mut length = 0; let mut splitted_row = String::new(); let mut splittend_length = 0; for (index, grapheme) in self.string[..].graphemes(true).enumerate() { if index < at { length += 1; row.push_str(grapheme); } else { splittend_length += 1; splitted_row.push_str(grapheme); } } self.string = row; self.len = length; Self { string: splitted_row, len: splittend_length, highlighting: Vec::new(), } } pub fn as_bytes(&self) -> &[u8] { self.string.as_bytes() } pub fn find(&self, query: &str, at: usize, direction: SearchDirection) -> Option { if at > self.len || query.is_empty() { return None; } let (start, end) = match direction { SearchDirection::Forward => (at, self.len), SearchDirection::Backward => (0, at), }; let substring: String = self.string[..] .graphemes(true) .skip(start) .take(end - start) .collect(); let matching_byte_index = if direction == SearchDirection::Forward { substring.find(query) } else { substring.rfind(query) }; if let Some(matching_byte_index) = matching_byte_index { for (grapheme_index, (byte_index, _)) in substring[..].grapheme_indices(true).enumerate() { if matching_byte_index == byte_index { return Some(start + grapheme_index); } } } None } pub fn highlight(&mut self, opts: HighlightingOptions, word: Option<&str>) { let mut highlighting = Vec::new(); let chars: Vec = 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 prev_is_separator = true; 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; } } let previous_highlight = if index > 0 { highlighting .get(index - 1) .unwrap_or(&highlighting::Type::None) } else { &highlighting::Type::None }; if opts.numbers() { if (c.is_ascii_digit() && (prev_is_separator || previous_highlight == &highlighting::Type::Number)) || (c == &'.' && previous_highlight == &highlighting::Type::Number) { highlighting.push(highlighting::Type::Number) } else { highlighting.push(highlighting::Type::None) } } else { highlighting.push(highlighting::Type::None) } prev_is_separator = c.is_ascii_punctuation() || c.is_ascii_whitespace(); index += 1; } self.highlighting = highlighting; } }