From ac352e26b6b0ff9b90004208abf4644f0b2c2f4e Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Tue, 6 Aug 2019 20:59:25 +0530 Subject: improve highlight for `_`, store program state --- src/lex/mod.rs | 7 +++++-- src/readline/mod.rs | 33 +++++++++++++++++++++++++++++---- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/src/lex/mod.rs b/src/lex/mod.rs index 48e07be..fe9c421 100644 --- a/src/lex/mod.rs +++ b/src/lex/mod.rs @@ -123,7 +123,7 @@ fn factorial(n: f64) -> f64 { n.signum() * (1..=n.abs() as u64).fold(1, |p, n| p * n) as f64 } -pub fn lexer(input: &str, prev_ans: f64) -> Result, CalcError> { +pub fn lexer(input: &str, prev_ans: Option) -> Result, CalcError> { let mut num_vec: String = String::new(); let mut char_vec: String = String::new(); let mut result: Vec = vec![]; @@ -149,6 +149,9 @@ pub fn lexer(input: &str, prev_ans: f64) -> Result, CalcError> { last_char_is_op = false; } '_' => { + if prev_ans.is_none() { + return Err(CalcError::Syntax("No previous answer!".into())); + } if !char_vec.is_empty() { if FUNCTIONS.get(&char_vec[..]).is_some() { return Err(CalcError::Syntax(format!( @@ -169,7 +172,7 @@ pub fn lexer(input: &str, prev_ans: f64) -> Result, CalcError> { num_vec.clear(); } last_char_is_op = false; - result.push(Token::Num(prev_ans)); + result.push(Token::Num(prev_ans.unwrap())); } 'a'...'z' | 'A'...'Z' => { let parse_num = num_vec.parse::().ok(); diff --git a/src/readline/mod.rs b/src/readline/mod.rs index 41cd742..8eedbfa 100644 --- a/src/readline/mod.rs +++ b/src/readline/mod.rs @@ -1,4 +1,5 @@ use std::borrow::Cow::{self, Owned}; +use std::path::PathBuf; use rustyline::completion::{Completer, FilenameCompleter, Pair}; use rustyline::config::{Builder, ColorMode, CompletionType, EditMode}; @@ -7,6 +8,8 @@ use rustyline::highlight::Highlighter; use rustyline::hint::{Hinter, HistoryHinter}; use rustyline::{Context, Editor, Helper}; +use directories::ProjectDirs; + use regex::Regex; use crate::eval_math_expression; @@ -17,13 +20,35 @@ pub struct RLHelper { hinter: HistoryHinter, } -struct LineHighlighter {} +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, 0f64); + use std::io::{ BufReader, BufRead }; + use std::fs::OpenOptions; + + let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap(); + let eva_data_dir = eva_dirs.data_dir(); + let mut previous_ans_path= PathBuf::from(eva_data_dir); + previous_ans_path.push("previous_ans.txt"); + + let file = OpenOptions::new() + .create(true) + .read(true) + .append(true) + .open(&previous_ans_path) + .unwrap(); + + let rdr = BufReader::new(file); + let lines = rdr.lines().map(|l| l.unwrap()); + let prev_ans = lines + .last() + .unwrap() + .parse::() + .ok(); + let op = eval_math_expression(line, prev_ans); match op { Ok(_) => { let constants = ["e", "pi"]; @@ -33,7 +58,7 @@ impl Highlighter for LineHighlighter { "asec", "acot", ]; let ops = Regex::new(r"(?P[\+-/\*%\^!])").unwrap(); - let mut coloured: String = ops.replace_all(line, "\x1b[33m$o\x1b[0m").into(); + let mut coloured: String = ops.replace_all(line, "\x1b[35m$o\x1b[0m").into(); for c in &constants { coloured = coloured.replace(c, &format!("\x1b[33m{}\x1b[0m", c)); @@ -89,7 +114,7 @@ pub fn create_readline() -> Editor { let mut rl = Editor::with_config(config); let h = RLHelper { completer: FilenameCompleter::new(), - highlighter: LineHighlighter {}, + highlighter: LineHighlighter { }, hinter: HistoryHinter {}, }; rl.set_helper(Some(h)); -- cgit v1.2.3