diff options
author | NerdyPepper <[email protected]> | 2019-06-18 11:55:01 +0100 |
---|---|---|
committer | NerdyPepper <[email protected]> | 2019-06-18 11:55:01 +0100 |
commit | d31247992286bef8e808a8777c2c8b9bdb23a783 (patch) | |
tree | 9b64f08a6bab75ba8e721f82406ad568a0bd43d7 /src | |
parent | 6ed405d71be1f0887101f4a444b0307c2ca74e56 (diff) |
add hinter, completer, matching bracket highlighter
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 84 |
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 }; | |||
29 | use clap::{Arg, App}; | 29 | use clap::{Arg, App}; |
30 | use lazy_static::lazy_static; | 30 | use lazy_static::lazy_static; |
31 | 31 | ||
32 | struct LineHelper (MatchingBracketHighlighter, HistoryHinter); | 32 | |
33 | struct RLHelper { | ||
34 | completer: FilenameCompleter, | ||
35 | highlighter: MatchingBracketHighlighter, | ||
36 | hinter: AnswerHinter, | ||
37 | } | ||
38 | |||
39 | struct AnswerHinter { } | ||
40 | |||
41 | impl 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 | |||
56 | impl 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 | |||
74 | impl 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 | |||
86 | impl 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 | |||
92 | impl Helper for RLHelper {} | ||
33 | 93 | ||
34 | struct Configuration { | 94 | struct 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 | ||