aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMaximilian Bosch <[email protected]>2021-12-26 14:31:13 +0000
committerMaximilian Bosch <[email protected]>2021-12-30 17:06:49 +0000
commit7d1db014ebc6bf0a8fd3e5a25abdc81aa2270a1d (patch)
tree01bf0cae15d091d85c228212642c03554cd29e08
parentacf947b608db99ff2e46a8020b6e70466c233088 (diff)
Deduplicate references to functions & constants
-rw-r--r--src/readline.rs13
1 files changed, 5 insertions, 8 deletions
diff --git a/src/readline.rs b/src/readline.rs
index 9912af6..6734674 100644
--- a/src/readline.rs
+++ b/src/readline.rs
@@ -15,6 +15,7 @@ use regex::Regex;
15 15
16use crate::error::CalcError; 16use crate::error::CalcError;
17use crate::eval_math_expression; 17use crate::eval_math_expression;
18use crate::lex::{CONSTANTS, FUNCTIONS};
18 19
19pub struct RLHelper { 20pub struct RLHelper {
20 completer: FilenameCompleter, 21 completer: FilenameCompleter,
@@ -52,20 +53,16 @@ impl Highlighter for LineHighlighter {
52 let op = eval_math_expression(line, prev_ans); 53 let op = eval_math_expression(line, prev_ans);
53 match op { 54 match op {
54 Ok(_) => { 55 Ok(_) => {
55 let constants = ["e", "pi"]; 56 let constants = CONSTANTS.keys();
56 let functions = [ 57 let functions = FUNCTIONS.keys();
57 "sin", "cos", "tan", "csc", "sec", "cot", "sinh", "cosh", "tanh", "ln", "log",
58 "sqrt", "ceil", "floor", "rad", "deg", "abs", "asin", "acos", "atan", "acsc",
59 "asec", "acot", "exp2", "exp"
60 ];
61 let ops = Regex::new(r"(?P<o>[\+-/\*%\^!])").unwrap(); 58 let ops = Regex::new(r"(?P<o>[\+-/\*%\^!])").unwrap();
62 let mut coloured: String = ops.replace_all(line, "\x1b[35m$o\x1b[0m").into(); 59 let mut coloured: String = ops.replace_all(line, "\x1b[35m$o\x1b[0m").into();
63 60
64 for c in &constants { 61 for c in constants {
65 let re = Regex::new(format!("(?P<o>{})(?P<r>(\x1b\\[35m)?([\\+-/\\*%\\^! ]|$))", c).as_str()).unwrap(); 62 let re = Regex::new(format!("(?P<o>{})(?P<r>(\x1b\\[35m)?([\\+-/\\*%\\^! ]|$))", c).as_str()).unwrap();
66 coloured = re.replace_all(&coloured, "\x1b[33m$o\x1b[0m$r").into(); 63 coloured = re.replace_all(&coloured, "\x1b[33m$o\x1b[0m$r").into();
67 } 64 }
68 for f in &functions { 65 for f in functions {
69 let re = Regex::new(format!("(?P<o>{})(?P<r>(\\(|$))", f).as_str()).unwrap(); 66 let re = Regex::new(format!("(?P<o>{})(?P<r>(\\(|$))", f).as_str()).unwrap();
70 coloured = re.replace_all(&coloured, "\x1b[34m$o\x1b[0m$r").into(); 67 coloured = re.replace_all(&coloured, "\x1b[34m$o\x1b[0m$r").into();
71 } 68 }