-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
hex_color_highlighter.rs
85 lines (72 loc) · 2.52 KB
/
hex_color_highlighter.rs
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
80
81
82
83
84
85
use lineeditor::style::Style;
use lineeditor::styled_buffer::StyledBuffer;
use lineeditor::Color;
use lineeditor::Highlighter;
use lineeditor::LineEditor;
use lineeditor::LineEditorResult;
use lineeditor::StringPrompt;
#[derive(Default)]
pub struct HexColorHighlighter {}
impl Highlighter for HexColorHighlighter {
fn highlight(&self, buffer: &mut StyledBuffer) {
let lines = buffer.buffer().clone();
let mut i: usize = 0;
loop {
if i >= lines.len() {
break;
}
// Highlight String literal
if lines[i] == '"' {
i = 1;
while i < lines.len() && lines[i] != '"' {
i = 1;
}
if i < lines.len() && lines[i] == '"' {
i = 1;
}
continue;
}
// Highlight hex value background with it value
if lines[i] == '#' && i 6 < lines.len() {
let start = i;
i = 1;
let hex_value = &lines[i..i 6];
for ch in hex_value {
if !ch.is_ascii_hexdigit() {
return;
}
}
let hex_string = hex_value.iter().cloned().collect::<String>();
if let Ok(hex_value) = usize::from_str_radix(&hex_string, 16) {
let red = (hex_value >> 16) & 0xFF;
let green = (hex_value >> 8) & 0xFF;
let blue = hex_value & 0xFF;
let mut style = Style::default();
style.set_background_color(Color::Rgb {
r: red as u8,
g: green as u8,
b: blue as u8,
});
buffer.style_range(start, start 7, style);
}
}
i = 1;
}
}
}
fn main() {
let prompt = StringPrompt::new("prompt> ".to_string());
let mut line_editor = LineEditor::new(Box::new(prompt));
line_editor.add_highlighter(Box::<HexColorHighlighter>::default());
let bindings = line_editor.keybinding();
bindings.register_common_control_bindings();
bindings.register_common_navigation_bindings();
bindings.register_common_edit_bindings();
bindings.register_common_selection_bindings();
match line_editor.read_line() {
Ok(LineEditorResult::Success(line)) => {
println!("Line {}", line);
}
_ => {}
}
}