From 7d1db014ebc6bf0a8fd3e5a25abdc81aa2270a1d Mon Sep 17 00:00:00 2001 From: Maximilian Bosch Date: Sun, 26 Dec 2021 15:31:13 +0100 Subject: Deduplicate references to functions & constants --- src/readline.rs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) (limited to 'src') 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; use crate::error::CalcError; use crate::eval_math_expression; +use crate::lex::{CONSTANTS, FUNCTIONS}; pub struct RLHelper { completer: FilenameCompleter, @@ -52,20 +53,16 @@ impl Highlighter for LineHighlighter { let op = eval_math_expression(line, prev_ans); match op { Ok(_) => { - let constants = ["e", "pi"]; - let functions = [ - "sin", "cos", "tan", "csc", "sec", "cot", "sinh", "cosh", "tanh", "ln", "log", - "sqrt", "ceil", "floor", "rad", "deg", "abs", "asin", "acos", "atan", "acsc", - "asec", "acot", "exp2", "exp" - ]; + let constants = CONSTANTS.keys(); + let functions = FUNCTIONS.keys(); let ops = Regex::new(r"(?P[\+-/\*%\^!])").unwrap(); let mut coloured: String = ops.replace_all(line, "\x1b[35m$o\x1b[0m").into(); - for c in &constants { + for c in constants { let re = Regex::new(format!("(?P{})(?P(\x1b\\[35m)?([\\+-/\\*%\\^! ]|$))", c).as_str()).unwrap(); coloured = re.replace_all(&coloured, "\x1b[33m$o\x1b[0m$r").into(); } - for f in &functions { + for f in functions { let re = Regex::new(format!("(?P{})(?P(\\(|$))", f).as_str()).unwrap(); coloured = re.replace_all(&coloured, "\x1b[34m$o\x1b[0m$r").into(); } -- cgit v1.2.3