aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs84
1 files changed, 75 insertions, 9 deletions
diff --git a/src/main.rs b/src/main.rs
index 63e886e..1a05904 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -29,7 +29,67 @@ use rustyline::highlight::{ Highlighter, MatchingBracketHighlighter };
29use clap::{Arg, App}; 29use clap::{Arg, App};
30use lazy_static::lazy_static; 30use lazy_static::lazy_static;
31 31
32struct LineHelper (MatchingBracketHighlighter, HistoryHinter); 32
33struct RLHelper {
34 completer: FilenameCompleter,
35 highlighter: MatchingBracketHighlighter,
36 hinter: AnswerHinter,
37}
38
39struct AnswerHinter { }
40
41impl Hinter for AnswerHinter {
42 fn hint(&self, line: &str, _: usize, _: &Context) -> Option<String> {
43 let input = line.trim();
44 let input = input.replace(" ", "");
45 if input.len() == 0 {
46 return Some("".into())
47 }
48 let dry_run = eval_math_expression(&input);
49 match dry_run {
50 Ok(ans) => return Some(format!("\x1b[90m = {}\x1b[0m", ans)),
51 Err(_) => return Some(format!(""))
52 };
53 }
54}
55
56impl Highlighter for RLHelper {
57 fn highlight_prompt<'p>(&self, prompt: &'p str) -> Cow<'p, str> {
58 Owned(String::from(prompt))
59 }
60
61 fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
62 Owned("\x1b[1m".to_owned() + hint + "\x1b[m")
63 }
64
65 fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> {
66 self.highlighter.highlight(line, pos)
67 }
68
69 fn highlight_char(&self, line: &str, pos: usize) -> bool {
70 self.highlighter.highlight_char(line, pos)
71 }
72}
73
74impl Completer for RLHelper {
75 type Candidate = Pair;
76 fn complete(
77 &self,
78 line: &str,
79 pos: usize,
80 ctx: &Context<'_>,
81 ) -> Result<(usize, Vec<Pair>), ReadlineError> {
82 self.completer.complete(line, pos, ctx)
83 }
84}
85
86impl Hinter for RLHelper {
87 fn hint(&self, line: &str, a: usize, b: &Context) -> Option<String> {
88 self.hinter.hint(line, a, b)
89 }
90}
91
92impl Helper for RLHelper {}
33 93
34struct Configuration { 94struct Configuration {
35 radian_mode: bool, 95 radian_mode: bool,
@@ -60,10 +120,16 @@ fn main() {
60 .completion_type(CompletionType::Circular) 120 .completion_type(CompletionType::Circular)
61 .max_history_size(1000) 121 .max_history_size(1000)
62 .build(); 122 .build();
63 let mut rl = Editor::<()>::with_config(config); 123 let mut rl = Editor::with_config(config);
124 let h = RLHelper {
125 completer: FilenameCompleter::new(),
126 highlighter: MatchingBracketHighlighter::new(),
127 hinter: AnswerHinter {}
128 };
129 rl.set_helper(Some(h));
64 if rl.load_history("history.txt").is_err() { 130 if rl.load_history("history.txt").is_err() {
65 println!("No previous history."); 131 println!("No previous history.")
66 } 132 };
67 133
68 loop { 134 loop {
69 let readline = rl.readline("> "); 135 let readline = rl.readline("> ");
@@ -130,11 +196,11 @@ fn parse_arguments() -> Configuration {
130 .unwrap_or("10") 196 .unwrap_or("10")
131 .parse() 197 .parse()
132 .unwrap(), 198 .unwrap(),
133 base: config.value_of("base") 199 base: config.value_of("base")
134 .unwrap_or("10") 200 .unwrap_or("10")
135 .parse() 201 .parse()
136 .unwrap(), 202 .unwrap(),
137 input, 203 input,
138 } 204 }
139} 205}
140 206