diff options
author | NerdyPepper <[email protected]> | 2019-07-09 08:14:35 +0100 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2019-07-09 08:14:35 +0100 |
commit | 8b49603f0c6d06bcb444bd0ee14aa5c1fbc4648c (patch) | |
tree | ff5fd41b489538f80b22930e4d8365505b610b8a /src/readline | |
parent | c6b03d82daa4d6b4856c4b33244f5261b0bbdaeb (diff) |
add previous answer access
Diffstat (limited to 'src/readline')
-rw-r--r-- | src/readline/mod.rs | 25 |
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}; | |||
3 | use rustyline::error::ReadlineError; | 3 | use rustyline::error::ReadlineError; |
4 | use rustyline::{ Editor, Context, Helper }; | 4 | use rustyline::{ Editor, Context, Helper }; |
5 | use rustyline::config::{ Builder, ColorMode, EditMode, CompletionType }; | 5 | use rustyline::config::{ Builder, ColorMode, EditMode, CompletionType }; |
6 | use rustyline::hint::Hinter; | 6 | use rustyline::hint::{ Hinter, HistoryHinter }; |
7 | use rustyline::completion::{ FilenameCompleter, Completer, Pair }; | 7 | use rustyline::completion::{ FilenameCompleter, Completer, Pair }; |
8 | use rustyline::highlight::Highlighter; | 8 | use rustyline::highlight::Highlighter; |
9 | 9 | ||
@@ -14,33 +14,16 @@ use crate::eval_math_expression; | |||
14 | pub struct RLHelper { | 14 | pub struct RLHelper { |
15 | completer: FilenameCompleter, | 15 | completer: FilenameCompleter, |
16 | highlighter: LineHighlighter, | 16 | highlighter: LineHighlighter, |
17 | hinter: AnswerHinter, | 17 | hinter: HistoryHinter, |
18 | } | 18 | } |
19 | 19 | ||
20 | struct AnswerHinter { } | ||
21 | impl 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 | |||
37 | struct LineHighlighter { } | 20 | struct LineHighlighter { } |
38 | impl Highlighter for LineHighlighter { | 21 | impl 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; |