diff options
Diffstat (limited to 'src/readline')
-rw-r--r-- | src/readline/mod.rs | 66 |
1 files changed, 31 insertions, 35 deletions
diff --git a/src/readline/mod.rs b/src/readline/mod.rs index e9d4b33..41cd742 100644 --- a/src/readline/mod.rs +++ b/src/readline/mod.rs | |||
@@ -1,11 +1,11 @@ | |||
1 | use std::borrow::Cow::{self,Owned}; | 1 | use std::borrow::Cow::{self, Owned}; |
2 | 2 | ||
3 | use rustyline::completion::{Completer, FilenameCompleter, Pair}; | ||
4 | use rustyline::config::{Builder, ColorMode, CompletionType, EditMode}; | ||
3 | use rustyline::error::ReadlineError; | 5 | use rustyline::error::ReadlineError; |
4 | use rustyline::{ Editor, Context, Helper }; | ||
5 | use rustyline::config::{ Builder, ColorMode, EditMode, CompletionType }; | ||
6 | use rustyline::hint::{ Hinter, HistoryHinter }; | ||
7 | use rustyline::completion::{ FilenameCompleter, Completer, Pair }; | ||
8 | use rustyline::highlight::Highlighter; | 6 | use rustyline::highlight::Highlighter; |
7 | use rustyline::hint::{Hinter, HistoryHinter}; | ||
8 | use rustyline::{Context, Editor, Helper}; | ||
9 | 9 | ||
10 | use regex::Regex; | 10 | use regex::Regex; |
11 | 11 | ||
@@ -17,7 +17,7 @@ pub struct RLHelper { | |||
17 | hinter: HistoryHinter, | 17 | hinter: HistoryHinter, |
18 | } | 18 | } |
19 | 19 | ||
20 | struct LineHighlighter { } | 20 | struct LineHighlighter {} |
21 | impl Highlighter for LineHighlighter { | 21 | impl Highlighter for LineHighlighter { |
22 | fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { | 22 | fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { |
23 | Owned(format!("\x1b[90m{}\x1b[0m", hint)) | 23 | Owned(format!("\x1b[90m{}\x1b[0m", hint)) |
@@ -28,14 +28,9 @@ impl Highlighter for LineHighlighter { | |||
28 | Ok(_) => { | 28 | Ok(_) => { |
29 | let constants = ["e", "pi"]; | 29 | let constants = ["e", "pi"]; |
30 | let functions = [ | 30 | let functions = [ |
31 | "sin" , "cos" , "tan" , | 31 | "sin", "cos", "tan", "csc", "sec", "cot", "sinh", "cosh", "tanh", "ln", "log", |
32 | "csc" , "sec" , "cot" , | 32 | "sqrt", "ceil", "floor", "rad", "deg", "abs", "asin", "acos", "atan", "acsc", |
33 | "sinh" , "cosh" , "tanh" , | 33 | "asec", "acot", |
34 | "ln" , "log" , "sqrt" , | ||
35 | "ceil" , "floor" , "rad" , | ||
36 | "deg" , "abs" , "asin" , | ||
37 | "acos" , "atan" , "acsc" , | ||
38 | "asec" , "acot" | ||
39 | ]; | 34 | ]; |
40 | let ops = Regex::new(r"(?P<o>[\+-/\*%\^!])").unwrap(); | 35 | let ops = Regex::new(r"(?P<o>[\+-/\*%\^!])").unwrap(); |
41 | let mut coloured: String = ops.replace_all(line, "\x1b[33m$o\x1b[0m").into(); | 36 | let mut coloured: String = ops.replace_all(line, "\x1b[33m$o\x1b[0m").into(); |
@@ -46,14 +41,14 @@ impl Highlighter for LineHighlighter { | |||
46 | for f in &functions { | 41 | for f in &functions { |
47 | coloured = coloured.replace(f, &format!("\x1b[34m{}\x1b[0m", f)); | 42 | coloured = coloured.replace(f, &format!("\x1b[34m{}\x1b[0m", f)); |
48 | } | 43 | } |
49 | Owned(coloured.into()) | 44 | Owned(coloured) |
50 | }, | 45 | } |
51 | Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line)) | 46 | Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line)), |
52 | } | 47 | } |
53 | } | 48 | } |
54 | } | 49 | } |
55 | 50 | ||
56 | impl Highlighter for RLHelper { | 51 | impl Highlighter for RLHelper { |
57 | fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { | 52 | fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { |
58 | self.highlighter.highlight_hint(hint) | 53 | self.highlighter.highlight_hint(hint) |
59 | } | 54 | } |
@@ -69,11 +64,11 @@ impl Completer for RLHelper { | |||
69 | line: &str, | 64 | line: &str, |
70 | pos: usize, | 65 | pos: usize, |
71 | ctx: &Context<'_>, | 66 | ctx: &Context<'_>, |
72 | ) -> Result<(usize, Vec<Pair>), ReadlineError> { | 67 | ) -> Result<(usize, Vec<Pair>), ReadlineError> { |
73 | self.completer.complete(line, pos, ctx) | 68 | self.completer.complete(line, pos, ctx) |
74 | } | 69 | } |
75 | } | 70 | } |
76 | 71 | ||
77 | impl Hinter for RLHelper { | 72 | impl Hinter for RLHelper { |
78 | fn hint(&self, line: &str, a: usize, b: &Context) -> Option<String> { | 73 | fn hint(&self, line: &str, a: usize, b: &Context) -> Option<String> { |
79 | self.hinter.hint(line, a, b) | 74 | self.hinter.hint(line, a, b) |
@@ -83,19 +78,20 @@ impl Hinter for RLHelper { | |||
83 | impl Helper for RLHelper {} | 78 | impl Helper for RLHelper {} |
84 | 79 | ||
85 | pub fn create_readline() -> Editor<RLHelper> { | 80 | pub fn create_readline() -> Editor<RLHelper> { |
86 | let config_builder = Builder::new(); | 81 | let config_builder = Builder::new(); |
87 | let config = config_builder.color_mode(ColorMode::Enabled) | 82 | let config = config_builder |
88 | .edit_mode(EditMode::Emacs) | 83 | .color_mode(ColorMode::Enabled) |
89 | .history_ignore_space(true) | 84 | .edit_mode(EditMode::Emacs) |
90 | .completion_type(CompletionType::Circular) | 85 | .history_ignore_space(true) |
91 | .max_history_size(1000) | 86 | .completion_type(CompletionType::Circular) |
92 | .build(); | 87 | .max_history_size(1000) |
93 | let mut rl = Editor::with_config(config); | 88 | .build(); |
94 | let h = RLHelper { | 89 | let mut rl = Editor::with_config(config); |
95 | completer: FilenameCompleter::new(), | 90 | let h = RLHelper { |
96 | highlighter: LineHighlighter {}, | 91 | completer: FilenameCompleter::new(), |
97 | hinter: HistoryHinter {} | 92 | highlighter: LineHighlighter {}, |
98 | }; | 93 | hinter: HistoryHinter {}, |
99 | rl.set_helper(Some(h)); | 94 | }; |
100 | return rl; | 95 | rl.set_helper(Some(h)); |
96 | rl | ||
101 | } | 97 | } |