diff options
author | NerdyPepper <[email protected]> | 2019-07-02 14:00:24 +0100 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2019-07-02 14:00:24 +0100 |
commit | 121f0c4c15bbee17a87a289703a8740321e1a652 (patch) | |
tree | 5118b04d51b4301c77746c638e20ba46dcde5507 /src/readline | |
parent | 3e67571383b14fddf32383ab90f74b525ab33ad1 (diff) |
add complete syntax highlighting
Diffstat (limited to 'src/readline')
-rw-r--r-- | src/readline/mod.rs | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/src/readline/mod.rs b/src/readline/mod.rs index 1f9fe98..34bce56 100644 --- a/src/readline/mod.rs +++ b/src/readline/mod.rs | |||
@@ -7,6 +7,8 @@ use rustyline::hint::Hinter; | |||
7 | use rustyline::completion::{ FilenameCompleter, Completer, Pair }; | 7 | use rustyline::completion::{ FilenameCompleter, Completer, Pair }; |
8 | use rustyline::highlight::Highlighter; | 8 | use rustyline::highlight::Highlighter; |
9 | 9 | ||
10 | use regex::Regex; | ||
11 | |||
10 | use crate::eval_math_expression; | 12 | use crate::eval_math_expression; |
11 | 13 | ||
12 | pub struct RLHelper { | 14 | pub struct RLHelper { |
@@ -31,6 +33,7 @@ impl Hinter for AnswerHinter { | |||
31 | } | 33 | } |
32 | } | 34 | } |
33 | 35 | ||
36 | |||
34 | struct LineHighlighter { } | 37 | struct LineHighlighter { } |
35 | impl Highlighter for LineHighlighter { | 38 | impl Highlighter for LineHighlighter { |
36 | fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { | 39 | fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { |
@@ -39,7 +42,26 @@ impl Highlighter for LineHighlighter { | |||
39 | fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { | 42 | fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { |
40 | let op = eval_math_expression(line); | 43 | let op = eval_math_expression(line); |
41 | match op { | 44 | match op { |
42 | Ok(_) => Owned(line.into()), | 45 | Ok(_) => { |
46 | let functions = [ | ||
47 | "sin" , "cos" , "tan" , | ||
48 | "csc" , "sec" , "cot" , | ||
49 | "sinh" , "cosh" , "tanh" , | ||
50 | "ln" , "log" , "sqrt" , | ||
51 | "ceil" , "floor" , "rad" , | ||
52 | "deg" , "abs" , "asin" , | ||
53 | "acos" , "atan" , "acsc" , | ||
54 | "asec" , "acot" | ||
55 | ]; | ||
56 | let ops = Regex::new(r"(?P<o>[\+-/\*%\^!])").unwrap(); | ||
57 | let mut coloured: String = ops.replace_all(line, "\x1b[33m$o\x1b[0m").into(); | ||
58 | |||
59 | for &f in functions.iter() { | ||
60 | let hfn = format!("\x1b[34m{}\x1b[0m", f); | ||
61 | coloured = coloured.replace(f, &hfn[..]); | ||
62 | } | ||
63 | Owned(coloured.into()) | ||
64 | }, | ||
43 | Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line)) | 65 | Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line)) |
44 | } | 66 | } |
45 | } | 67 | } |