1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
use crate::highlighter::Highlighter;
use crate::StyledText;
use nu_ansi_term::{Color, Style};
pub struct SimpleMatchHighlighter {
neutral_style: Style,
match_style: Style,
query: String,
}
impl Default for SimpleMatchHighlighter {
fn default() -> Self {
Self {
neutral_style: Style::default(),
match_style: Style::new().fg(Color::Green),
query: String::default(),
}
}
}
impl Highlighter for SimpleMatchHighlighter {
fn highlight(&self, line: &str, _cursor: usize) -> StyledText {
let mut styled_text = StyledText::new();
if self.query.is_empty() {
styled_text.push((self.neutral_style, line.to_owned()));
} else {
let mut next_idx: usize = 0;
for (idx, mat) in line.match_indices(&self.query) {
if idx != next_idx {
styled_text.push((self.neutral_style, line[next_idx..idx].to_owned()));
}
styled_text.push((self.match_style, mat.to_owned()));
next_idx = idx + mat.len();
}
if next_idx != line.len() {
styled_text.push((self.neutral_style, line[next_idx..].to_owned()));
}
}
styled_text
}
}
impl SimpleMatchHighlighter {
pub fn new(query: String) -> Self {
Self {
query,
..Self::default()
}
}
#[must_use]
pub fn with_query(mut self, query: String) -> Self {
self.query = query;
self
}
#[must_use]
pub fn with_match_style(mut self, match_style: Style) -> Self {
self.match_style = match_style;
self
}
#[must_use]
pub fn with_neutral_style(mut self, neutral_style: Style) -> Self {
self.neutral_style = neutral_style;
self
}
}