diff options
author | NerdyPepper <[email protected]> | 2019-06-20 11:34:49 +0100 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2019-06-20 11:34:49 +0100 |
commit | 8c0cef894f00008ff1fe3e76588fca111776f68e (patch) | |
tree | 5cb6d6c70e6b49c4084bcdd7b87fe307e5b55ba1 /src | |
parent | f98a2796b7c1379b204a24586e598d7f8da9da9f (diff) |
add basic syntax highlighting
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/src/main.rs b/src/main.rs index 8a66c4d..a8fbcf2 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -32,12 +32,11 @@ use lazy_static::lazy_static; | |||
32 | 32 | ||
33 | struct RLHelper { | 33 | struct RLHelper { |
34 | completer: FilenameCompleter, | 34 | completer: FilenameCompleter, |
35 | highlighter: MatchingBracketHighlighter, | 35 | highlighter: LineHighlighter, |
36 | hinter: AnswerHinter, | 36 | hinter: AnswerHinter, |
37 | } | 37 | } |
38 | 38 | ||
39 | struct AnswerHinter { } | 39 | struct AnswerHinter { } |
40 | |||
41 | impl Hinter for AnswerHinter { | 40 | impl Hinter for AnswerHinter { |
42 | fn hint(&self, line: &str, _: usize, _: &Context) -> Option<String> { | 41 | fn hint(&self, line: &str, _: usize, _: &Context) -> Option<String> { |
43 | let input = line.trim(); | 42 | let input = line.trim(); |
@@ -53,22 +52,27 @@ impl Hinter for AnswerHinter { | |||
53 | } | 52 | } |
54 | } | 53 | } |
55 | 54 | ||
56 | impl Highlighter for RLHelper { | 55 | struct LineHighlighter { } |
57 | fn highlight_prompt<'p>(&self, prompt: &'p str) -> Cow<'p, str> { | 56 | impl Highlighter for LineHighlighter { |
58 | Owned(String::from(prompt)) | 57 | fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { |
58 | Owned(format!("\x1b[90m{}\x1b[0m", hint)) | ||
59 | } | ||
60 | fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { | ||
61 | let op = eval_math_expression(line); | ||
62 | match op { | ||
63 | Ok(_) => Owned(line.into()), | ||
64 | Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line)) | ||
65 | } | ||
59 | } | 66 | } |
67 | } | ||
60 | 68 | ||
69 | impl Highlighter for RLHelper { | ||
61 | fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { | 70 | fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { |
62 | Owned("\x1b[90m".to_owned() + hint + "\x1b[0m") | 71 | self.highlighter.highlight_hint(hint) |
63 | } | 72 | } |
64 | |||
65 | fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> { | 73 | fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> { |
66 | self.highlighter.highlight(line, pos) | 74 | self.highlighter.highlight(line, pos) |
67 | } | 75 | } |
68 | |||
69 | fn highlight_char(&self, line: &str, pos: usize) -> bool { | ||
70 | self.highlighter.highlight_char(line, pos) | ||
71 | } | ||
72 | } | 76 | } |
73 | 77 | ||
74 | impl Completer for RLHelper { | 78 | impl Completer for RLHelper { |
@@ -123,7 +127,7 @@ fn main() { | |||
123 | let mut rl = Editor::with_config(config); | 127 | let mut rl = Editor::with_config(config); |
124 | let h = RLHelper { | 128 | let h = RLHelper { |
125 | completer: FilenameCompleter::new(), | 129 | completer: FilenameCompleter::new(), |
126 | highlighter: MatchingBracketHighlighter::new(), | 130 | highlighter: LineHighlighter {}, |
127 | hinter: AnswerHinter {} | 131 | hinter: AnswerHinter {} |
128 | }; | 132 | }; |
129 | rl.set_helper(Some(h)); | 133 | rl.set_helper(Some(h)); |