From 8c0cef894f00008ff1fe3e76588fca111776f68e Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Thu, 20 Jun 2019 16:04:49 +0530 Subject: add basic syntax highlighting --- src/main.rs | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) (limited to 'src') 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; struct RLHelper { completer: FilenameCompleter, - highlighter: MatchingBracketHighlighter, + highlighter: LineHighlighter, hinter: AnswerHinter, } struct AnswerHinter { } - impl Hinter for AnswerHinter { fn hint(&self, line: &str, _: usize, _: &Context) -> Option { let input = line.trim(); @@ -53,22 +52,27 @@ impl Hinter for AnswerHinter { } } -impl Highlighter for RLHelper { - fn highlight_prompt<'p>(&self, prompt: &'p str) -> Cow<'p, str> { - Owned(String::from(prompt)) +struct LineHighlighter { } +impl Highlighter for LineHighlighter { + fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { + Owned(format!("\x1b[90m{}\x1b[0m", hint)) + } + fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { + let op = eval_math_expression(line); + match op { + Ok(_) => Owned(line.into()), + Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line)) + } } +} +impl Highlighter for RLHelper { fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { - Owned("\x1b[90m".to_owned() + hint + "\x1b[0m") + self.highlighter.highlight_hint(hint) } - fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> { self.highlighter.highlight(line, pos) } - - fn highlight_char(&self, line: &str, pos: usize) -> bool { - self.highlighter.highlight_char(line, pos) - } } impl Completer for RLHelper { @@ -123,7 +127,7 @@ fn main() { let mut rl = Editor::with_config(config); let h = RLHelper { completer: FilenameCompleter::new(), - highlighter: MatchingBracketHighlighter::new(), + highlighter: LineHighlighter {}, hinter: AnswerHinter {} }; rl.set_helper(Some(h)); -- cgit v1.2.3