aboutsummaryrefslogtreecommitdiff
path: root/src/readline/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/readline/mod.rs')
-rw-r--r--src/readline/mod.rs25
1 files changed, 4 insertions, 21 deletions
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};
3use rustyline::error::ReadlineError; 3use rustyline::error::ReadlineError;
4use rustyline::{ Editor, Context, Helper }; 4use rustyline::{ Editor, Context, Helper };
5use rustyline::config::{ Builder, ColorMode, EditMode, CompletionType }; 5use rustyline::config::{ Builder, ColorMode, EditMode, CompletionType };
6use rustyline::hint::Hinter; 6use rustyline::hint::{ Hinter, HistoryHinter };
7use rustyline::completion::{ FilenameCompleter, Completer, Pair }; 7use rustyline::completion::{ FilenameCompleter, Completer, Pair };
8use rustyline::highlight::Highlighter; 8use rustyline::highlight::Highlighter;
9 9
@@ -14,33 +14,16 @@ use crate::eval_math_expression;
14pub struct RLHelper { 14pub struct RLHelper {
15 completer: FilenameCompleter, 15 completer: FilenameCompleter,
16 highlighter: LineHighlighter, 16 highlighter: LineHighlighter,
17 hinter: AnswerHinter, 17 hinter: HistoryHinter,
18} 18}
19 19
20struct AnswerHinter { }
21impl Hinter for AnswerHinter {
22 fn hint(&self, line: &str, _: usize, _: &Context) -> Option<String> {
23 let input = line.trim();
24 let input = input.replace(" ", "");
25 if input.len() == 0 {
26 return Some("".into())
27 }
28 let dry_run = eval_math_expression(&input);
29 match dry_run {
30 Ok(ans) => return Some(format!(" = {}", ans)),
31 Err(_) => return Some(format!(""))
32 };
33 }
34}
35
36
37struct LineHighlighter { } 20struct LineHighlighter { }
38impl Highlighter for LineHighlighter { 21impl Highlighter for LineHighlighter {
39 fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { 22 fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
40 Owned(format!("\x1b[90m{}\x1b[0m", hint)) 23 Owned(format!("\x1b[90m{}\x1b[0m", hint))
41 } 24 }
42 fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { 25 fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> {
43 let op = eval_math_expression(line); 26 let op = eval_math_expression(line, &mut 0f64);
44 match op { 27 match op {
45 Ok(_) => { 28 Ok(_) => {
46 let functions = [ 29 let functions = [
@@ -108,7 +91,7 @@ pub fn create_readline() -> Editor<RLHelper> {
108 let h = RLHelper { 91 let h = RLHelper {
109 completer: FilenameCompleter::new(), 92 completer: FilenameCompleter::new(),
110 highlighter: LineHighlighter {}, 93 highlighter: LineHighlighter {},
111 hinter: AnswerHinter {} 94 hinter: HistoryHinter {}
112 }; 95 };
113 rl.set_helper(Some(h)); 96 rl.set_helper(Some(h));
114 return rl; 97 return rl;