From 8b49603f0c6d06bcb444bd0ee14aa5c1fbc4648c Mon Sep 17 00:00:00 2001 From: NerdyPepper Date: Tue, 9 Jul 2019 12:44:35 +0530 Subject: add previous answer access --- src/readline/mod.rs | 25 ++++--------------------- 1 file changed, 4 insertions(+), 21 deletions(-) (limited to 'src/readline/mod.rs') diff --git a/src/readline/mod.rs b/src/readline/mod.rs index 34bce56..9ae14ba 100644 --- a/src/readline/mod.rs +++ b/src/readline/mod.rs @@ -3,7 +3,7 @@ use std::borrow::Cow::{self,Owned}; use rustyline::error::ReadlineError; use rustyline::{ Editor, Context, Helper }; use rustyline::config::{ Builder, ColorMode, EditMode, CompletionType }; -use rustyline::hint::Hinter; +use rustyline::hint::{ Hinter, HistoryHinter }; use rustyline::completion::{ FilenameCompleter, Completer, Pair }; use rustyline::highlight::Highlighter; @@ -14,33 +14,16 @@ use crate::eval_math_expression; pub struct RLHelper { completer: FilenameCompleter, highlighter: LineHighlighter, - hinter: AnswerHinter, + hinter: HistoryHinter, } -struct AnswerHinter { } -impl Hinter for AnswerHinter { - fn hint(&self, line: &str, _: usize, _: &Context) -> Option { - let input = line.trim(); - let input = input.replace(" ", ""); - if input.len() == 0 { - return Some("".into()) - } - let dry_run = eval_math_expression(&input); - match dry_run { - Ok(ans) => return Some(format!(" = {}", ans)), - Err(_) => return Some(format!("")) - }; - } -} - - 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); + let op = eval_math_expression(line, &mut 0f64); match op { Ok(_) => { let functions = [ @@ -108,7 +91,7 @@ pub fn create_readline() -> Editor { let h = RLHelper { completer: FilenameCompleter::new(), highlighter: LineHighlighter {}, - hinter: AnswerHinter {} + hinter: HistoryHinter {} }; rl.set_helper(Some(h)); return rl; -- cgit v1.2.3