diff options
Diffstat (limited to 'src/main.rs')
-rw-r--r-- | src/main.rs | 108 |
1 files changed, 15 insertions, 93 deletions
diff --git a/src/main.rs b/src/main.rs index 50f4375..91d91e5 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -1,102 +1,33 @@ | |||
1 | /* | 1 | /* |
2 | * eva - an easy to use calculator REPL similar to bc(1) | 2 | * eva - an easy to use calculator REPL similar to bc(1) |
3 | * Copyright (C) 2019 Akshay Oppiliappan <[email protected]> | 3 | * Copyright (C) 2019 Akshay Oppiliappan <[email protected]> |
4 | * | ||
5 | */ | 4 | */ |
6 | 5 | ||
6 | /* imports */ | ||
7 | // std | 7 | // std |
8 | use std::f64; | 8 | use std::f64; |
9 | use std::borrow::Cow::{self,Owned}; | ||
10 | use std::path::PathBuf; | 9 | use std::path::PathBuf; |
11 | use std::fs::create_dir_all; | 10 | use std::fs::create_dir_all; |
12 | 11 | ||
13 | // modules | 12 | // modules |
14 | mod lex; | 13 | mod lex; |
15 | use crate::lex::*; | ||
16 | mod parse; | 14 | mod parse; |
17 | use crate::parse::*; | ||
18 | mod error; | 15 | mod error; |
19 | use crate::error::{ CalcError, handler }; | ||
20 | mod format; | 16 | mod format; |
17 | mod readline; | ||
18 | use crate::lex::*; | ||
19 | use crate::parse::*; | ||
20 | use crate::error::{ CalcError, handler }; | ||
21 | use crate::format::*; | 21 | use crate::format::*; |
22 | use crate::readline::*; | ||
22 | 23 | ||
23 | // extern crates | 24 | // extern crates |
24 | use rustyline::error::ReadlineError; | 25 | use rustyline::error::ReadlineError; |
25 | use rustyline::{ Editor, Context, Helper }; | ||
26 | use rustyline::config::{ Builder, ColorMode, EditMode, CompletionType }; | ||
27 | use rustyline::hint::Hinter; | ||
28 | use rustyline::completion::{ FilenameCompleter, Completer, Pair }; | ||
29 | use rustyline::highlight::Highlighter; | ||
30 | |||
31 | use clap::{Arg, App}; | 26 | use clap::{Arg, App}; |
32 | use lazy_static::lazy_static; | 27 | use lazy_static::lazy_static; |
33 | |||
34 | use directories::{ ProjectDirs, UserDirs }; | 28 | use directories::{ ProjectDirs, UserDirs }; |
35 | 29 | ||
36 | struct RLHelper { | 30 | /* end of imports */ |
37 | completer: FilenameCompleter, | ||
38 | highlighter: LineHighlighter, | ||
39 | hinter: AnswerHinter, | ||
40 | } | ||
41 | |||
42 | struct AnswerHinter { } | ||
43 | impl Hinter for AnswerHinter { | ||
44 | fn hint(&self, line: &str, _: usize, _: &Context) -> Option<String> { | ||
45 | let input = line.trim(); | ||
46 | let input = input.replace(" ", ""); | ||
47 | if input.len() == 0 { | ||
48 | return Some("".into()) | ||
49 | } | ||
50 | let dry_run = eval_math_expression(&input); | ||
51 | match dry_run { | ||
52 | Ok(ans) => return Some(format!(" = {}", ans)), | ||
53 | Err(_) => return Some(format!("")) | ||
54 | }; | ||
55 | } | ||
56 | } | ||
57 | |||
58 | struct LineHighlighter { } | ||
59 | impl Highlighter for LineHighlighter { | ||
60 | fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { | ||
61 | Owned(format!("\x1b[90m{}\x1b[0m", hint)) | ||
62 | } | ||
63 | fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { | ||
64 | let op = eval_math_expression(line); | ||
65 | match op { | ||
66 | Ok(_) => Owned(line.into()), | ||
67 | Err(_) => Owned(format!("\x1b[31m{}\x1b[0m", line)) | ||
68 | } | ||
69 | } | ||
70 | } | ||
71 | |||
72 | impl Highlighter for RLHelper { | ||
73 | fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { | ||
74 | self.highlighter.highlight_hint(hint) | ||
75 | } | ||
76 | fn highlight<'l>(&self, line: &'l str, pos: usize) -> Cow<'l, str> { | ||
77 | self.highlighter.highlight(line, pos) | ||
78 | } | ||
79 | } | ||
80 | |||
81 | impl Completer for RLHelper { | ||
82 | type Candidate = Pair; | ||
83 | fn complete( | ||
84 | &self, | ||
85 | line: &str, | ||
86 | pos: usize, | ||
87 | ctx: &Context<'_>, | ||
88 | ) -> Result<(usize, Vec<Pair>), ReadlineError> { | ||
89 | self.completer.complete(line, pos, ctx) | ||
90 | } | ||
91 | } | ||
92 | |||
93 | impl Hinter for RLHelper { | ||
94 | fn hint(&self, line: &str, a: usize, b: &Context) -> Option<String> { | ||
95 | self.hinter.hint(line, a, b) | ||
96 | } | ||
97 | } | ||
98 | |||
99 | impl Helper for RLHelper {} | ||
100 | 31 | ||
101 | struct Configuration { | 32 | struct Configuration { |
102 | radian_mode: bool, | 33 | radian_mode: bool, |
@@ -106,11 +37,12 @@ struct Configuration { | |||
106 | } | 37 | } |
107 | 38 | ||
108 | lazy_static! { | 39 | lazy_static! { |
109 | static ref CONFIGURATION: Configuration = parse_arguments(); | 40 | static ref CONFIGURATION: Configuration = parse_arguments(); |
110 | } | 41 | } |
111 | 42 | ||
112 | fn main() { | 43 | fn main() { |
113 | if CONFIGURATION.input.len() > 0 { | 44 | if CONFIGURATION.input.len() > 0 { |
45 | // command mode // | ||
114 | let evaled = eval_math_expression(&CONFIGURATION.input[..]); | 46 | let evaled = eval_math_expression(&CONFIGURATION.input[..]); |
115 | match evaled { | 47 | match evaled { |
116 | Ok(ans) => pprint(ans), | 48 | Ok(ans) => pprint(ans), |
@@ -120,21 +52,11 @@ fn main() { | |||
120 | }, | 52 | }, |
121 | }; | 53 | }; |
122 | } else { | 54 | } else { |
123 | let config_builder = Builder::new(); | 55 | // REPL mode // |
124 | let config = config_builder.color_mode(ColorMode::Enabled) | 56 | // create fancy readline |
125 | .edit_mode(EditMode::Emacs) | 57 | let mut rl = create_readline(); |
126 | .history_ignore_space(true) | ||
127 | .completion_type(CompletionType::Circular) | ||
128 | .max_history_size(1000) | ||
129 | .build(); | ||
130 | let mut rl = Editor::with_config(config); | ||
131 | let h = RLHelper { | ||
132 | completer: FilenameCompleter::new(), | ||
133 | highlighter: LineHighlighter {}, | ||
134 | hinter: AnswerHinter {} | ||
135 | }; | ||
136 | rl.set_helper(Some(h)); | ||
137 | 58 | ||
59 | // handle history storage | ||
138 | let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap(); | 60 | let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap(); |
139 | let eva_data_dir = eva_dirs.data_dir(); | 61 | let eva_data_dir = eva_dirs.data_dir(); |
140 | let mut history_path = PathBuf::from(eva_data_dir); | 62 | let mut history_path = PathBuf::from(eva_data_dir); |
@@ -147,6 +69,7 @@ fn main() { | |||
147 | println!("No previous history.") | 69 | println!("No previous history.") |
148 | }; | 70 | }; |
149 | 71 | ||
72 | // repl loop begins here | ||
150 | loop { | 73 | loop { |
151 | let readline = rl.readline("> "); | 74 | let readline = rl.readline("> "); |
152 | match readline { | 75 | match readline { |
@@ -219,8 +142,7 @@ fn parse_arguments() -> Configuration { | |||
219 | } | 142 | } |
220 | } | 143 | } |
221 | 144 | ||
222 | 145 | pub fn eval_math_expression(input: &str) -> Result<f64, CalcError> { | |
223 | fn eval_math_expression(input: &str) -> Result<f64, CalcError> { | ||
224 | let input = input.trim(); | 146 | let input = input.trim(); |
225 | let input = input.replace(" ", ""); | 147 | let input = input.replace(" ", ""); |
226 | if input.len() == 0 { | 148 | if input.len() == 0 { |