aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorNerdyPepper <[email protected]>2019-06-20 11:41:55 +0100
committerNerdyPepper <[email protected]>2019-06-20 11:41:55 +0100
commit784da4e3e3a106a08d9bc91926ae5a2f3a3286d4 (patch)
tree5cb6d6c70e6b49c4084bcdd7b87fe307e5b55ba1 /src/main.rs
parent5352cc254c04e0f295c59a7c022cc075e8963119 (diff)
parent8c0cef894f00008ff1fe3e76588fca111776f68e (diff)
add syntax hl, hint features from dev branch
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs95
1 files changed, 84 insertions, 11 deletions
diff --git a/src/main.rs b/src/main.rs
index ffdb690..a8fbcf2 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,6 +6,7 @@
6 6
7// std 7// std
8use std::f64; 8use std::f64;
9use std::borrow::Cow::{self, Borrowed, Owned};
9 10
10// modules 11// modules
11mod lex; 12mod lex;
@@ -19,14 +20,80 @@ use crate::format::*;
19 20
20// extern crates 21// extern crates
21use rustyline::error::ReadlineError; 22use rustyline::error::ReadlineError;
22use rustyline::Editor; 23use rustyline::{ Editor, Context, Helper };
23use rustyline::config::{ Builder, ColorMode, EditMode, CompletionType }; 24use rustyline::config::{ Builder, ColorMode, EditMode, CompletionType };
25use rustyline::hint::Hinter;
26use rustyline::completion::{ FilenameCompleter, Completer, Pair };
24use rustyline::highlight::{ Highlighter, MatchingBracketHighlighter }; 27use rustyline::highlight::{ Highlighter, MatchingBracketHighlighter };
25use rustyline::hint::{ Hinter, HistoryHinter }; 28
26use clap::{Arg, App}; 29use clap::{Arg, App};
27use lazy_static::lazy_static; 30use lazy_static::lazy_static;
28 31
29struct LineHelper (MatchingBracketHighlighter, HistoryHinter); 32
33struct RLHelper {
34 completer: FilenameCompleter,
35 highlighter: LineHighlighter,
36 hinter: AnswerHinter,
37}
38
39struct AnswerHinter { }
40impl Hinter for AnswerHinter {
41 fn hint(&self, line: &str, _: usize, _: &Context) -> Option<String> {
42 let input = line.trim();
43 let input = input.replace(" ", "");
44 if input.len() == 0 {
45 return Some("".into())
46 }
47 let dry_run = eval_math_expression(&input);
48 match dry_run {
49 Ok(ans) => return Some(format!(" = {}", ans)),
50 Err(_) => return Some(format!(""))
51 };
52 }
53}
54
55struct LineHighlighter { }
56impl Highlighter for LineHighlighter {
57 fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
58 Owned(format!("\x1b[90m{}\x1b[0m", hint))
59 }
60 fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> {
61 let op = eval_math_expression(line);
62 match op {
63 Ok(_) => Owned(line.into()),
64 Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line))
65 }
66 }
67}
68
69impl Highlighter for RLHelper {
70 fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
71 self.highlighter.highlight_hint(hint)
72 }
73 fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> {
74 self.highlighter.highlight(line, pos)
75 }
76}
77
78impl Completer for RLHelper {
79 type Candidate = Pair;
80 fn complete(
81 &self,
82 line: &str,
83 pos: usize,
84 ctx: &Context<'_>,
85 ) -> Result<(usize, Vec<Pair>), ReadlineError> {
86 self.completer.complete(line, pos, ctx)
87 }
88}
89
90impl Hinter for RLHelper {
91 fn hint(&self, line: &str, a: usize, b: &Context) -> Option<String> {
92 self.hinter.hint(line, a, b)
93 }
94}
95
96impl Helper for RLHelper {}
30 97
31struct Configuration { 98struct Configuration {
32 radian_mode: bool, 99 radian_mode: bool,
@@ -57,10 +124,16 @@ fn main() {
57 .completion_type(CompletionType::Circular) 124 .completion_type(CompletionType::Circular)
58 .max_history_size(1000) 125 .max_history_size(1000)
59 .build(); 126 .build();
60 let mut rl = Editor::<()>::with_config(config); 127 let mut rl = Editor::with_config(config);
128 let h = RLHelper {
129 completer: FilenameCompleter::new(),
130 highlighter: LineHighlighter {},
131 hinter: AnswerHinter {}
132 };
133 rl.set_helper(Some(h));
61 if rl.load_history("history.txt").is_err() { 134 if rl.load_history("history.txt").is_err() {
62 println!("No previous history."); 135 println!("No previous history.")
63 } 136 };
64 137
65 loop { 138 loop {
66 let readline = rl.readline("> "); 139 let readline = rl.readline("> ");
@@ -127,11 +200,11 @@ fn parse_arguments() -> Configuration {
127 .unwrap_or("10") 200 .unwrap_or("10")
128 .parse() 201 .parse()
129 .unwrap(), 202 .unwrap(),
130 base: config.value_of("base") 203 base: config.value_of("base")
131 .unwrap_or("10") 204 .unwrap_or("10")
132 .parse() 205 .parse()
133 .unwrap(), 206 .unwrap(),
134 input, 207 input,
135 } 208 }
136} 209}
137 210