aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDominik Süß <[email protected]>2019-10-14 19:28:54 +0100
committerDominik Süß <[email protected]>2019-10-14 19:28:54 +0100
commit432129fcfe47abe83f5f20d544a4f14686921c7e (patch)
tree89bc4262f49967b6b9fe050f9f2e8e506a90c785
parentd74ac7e20061cc353590b9b61977ec3aae334aba (diff)
fix warnings as well as issues with the cache dir
-rw-r--r--src/lex/mod.rs4
-rw-r--r--src/main.rs49
-rw-r--r--src/readline/mod.rs17
3 files changed, 40 insertions, 30 deletions
diff --git a/src/lex/mod.rs b/src/lex/mod.rs
index fe9c421..b21bafb 100644
--- a/src/lex/mod.rs
+++ b/src/lex/mod.rs
@@ -131,7 +131,7 @@ pub fn lexer(input: &str, prev_ans: Option<f64>) -> Result<Vec<Token>, CalcError
131 131
132 for letter in input.chars() { 132 for letter in input.chars() {
133 match letter { 133 match letter {
134 '0'...'9' | '.' => { 134 '0'..='9' | '.' => {
135 if !char_vec.is_empty() { 135 if !char_vec.is_empty() {
136 if FUNCTIONS.get(&char_vec[..]).is_some() { 136 if FUNCTIONS.get(&char_vec[..]).is_some() {
137 return Err(CalcError::Syntax(format!( 137 return Err(CalcError::Syntax(format!(
@@ -174,7 +174,7 @@ pub fn lexer(input: &str, prev_ans: Option<f64>) -> Result<Vec<Token>, CalcError
174 last_char_is_op = false; 174 last_char_is_op = false;
175 result.push(Token::Num(prev_ans.unwrap())); 175 result.push(Token::Num(prev_ans.unwrap()));
176 } 176 }
177 'a'...'z' | 'A'...'Z' => { 177 'a'..='z' | 'A'..='Z' => {
178 let parse_num = num_vec.parse::<f64>().ok(); 178 let parse_num = num_vec.parse::<f64>().ok();
179 if let Some(x) = parse_num { 179 if let Some(x) = parse_num {
180 result.push(Token::Num(x)); 180 result.push(Token::Num(x));
diff --git a/src/main.rs b/src/main.rs
index 38f4837..63b07c9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -66,18 +66,20 @@ fn main() {
66 let mut history_path = PathBuf::from(eva_data_dir); 66 let mut history_path = PathBuf::from(eva_data_dir);
67 let mut previous_ans_path = PathBuf::from(eva_cache_dir); 67 let mut previous_ans_path = PathBuf::from(eva_cache_dir);
68 68
69 match create_dir_all(eva_data_dir) { 69 if let Err(_) = create_dir_all(eva_data_dir) {
70 Ok(_) => { 70 history_path = PathBuf::from(UserDirs::new().unwrap().home_dir());
71 history_path.push("history.txt"); 71 }
72 previous_ans_path.push("previous_ans.txt"); 72 if let Err(_) = create_dir_all(eva_cache_dir) {
73 }, 73 previous_ans_path = PathBuf::from(UserDirs::new().unwrap().home_dir());
74 Err(_) => { 74 }
75 history_path = PathBuf::from(UserDirs::new().unwrap().home_dir()); 75 history_path.push("history.txt");
76 previous_ans_path = PathBuf::from(UserDirs::new().unwrap().home_dir()); 76 previous_ans_path.push("previous_ans.txt");
77 }
78 };
79 77
80 std::fs::write(&previous_ans_path, "0"); 78 if let Err(err) = std::fs::write(&previous_ans_path, "0") {
79 println!("Could not write to previous_ans_path");
80 println!("{:?}", err);
81 std::process::exit(1);
82 }
81 83
82 if rl.load_history(history_path.as_path()).is_err() { 84 if rl.load_history(history_path.as_path()).is_err() {
83 println!("No previous history.") 85 println!("No previous history.")
@@ -92,18 +94,27 @@ fn main() {
92 let evaled = eval_math_expression(&line[..], prev_ans); 94 let evaled = eval_math_expression(&line[..], prev_ans);
93 match evaled { 95 match evaled {
94 Ok(ans) => { 96 Ok(ans) => {
95 use std::io::Write;
96 use std::fs::OpenOptions; 97 use std::fs::OpenOptions;
97 let mut file = OpenOptions::new() 98 use std::io::Write;
99 prev_ans = Some(ans);
100 pprint(ans);
101 match OpenOptions::new()
98 .write(true) 102 .write(true)
99 .create(true) 103 .create(true)
100 .open(&previous_ans_path) 104 .open(&previous_ans_path)
101 .unwrap(); 105 {
102 106 Ok(mut file) => {
103 prev_ans = Some(ans); 107 if let Err(err) = writeln!(file, "{}", ans) {
104 writeln!(file, "{}", ans); 108 println!(
105 109 "Error while writing previous answer to file: {}",
106 pprint(ans); 110 err
111 )
112 }
113 }
114 Err(err) => {
115 println!("Error while writing previous answer to file: {}", err)
116 }
117 }
107 } 118 }
108 Err(e) => println!("{}", handler(e)), 119 Err(e) => println!("{}", handler(e)),
109 }; 120 };
diff --git a/src/readline/mod.rs b/src/readline/mod.rs
index 8eedbfa..ea195ee 100644
--- a/src/readline/mod.rs
+++ b/src/readline/mod.rs
@@ -20,18 +20,18 @@ pub struct RLHelper {
20 hinter: HistoryHinter, 20 hinter: HistoryHinter,
21} 21}
22 22
23struct LineHighlighter { } 23struct LineHighlighter {}
24impl Highlighter for LineHighlighter { 24impl Highlighter for LineHighlighter {
25 fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> { 25 fn highlight_hint<'h>(&self, hint: &'h str) -> Cow<'h, str> {
26 Owned(format!("\x1b[90m{}\x1b[0m", hint)) 26 Owned(format!("\x1b[90m{}\x1b[0m", hint))
27 } 27 }
28 fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { 28 fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> {
29 use std::io::{ BufReader, BufRead };
30 use std::fs::OpenOptions; 29 use std::fs::OpenOptions;
30 use std::io::{BufRead, BufReader};
31 31
32 let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap(); 32 let eva_dirs = ProjectDirs::from("com", "NerdyPepper", "eva").unwrap();
33 let eva_data_dir = eva_dirs.data_dir(); 33 let eva_data_dir = eva_dirs.data_dir();
34 let mut previous_ans_path= PathBuf::from(eva_data_dir); 34 let mut previous_ans_path = PathBuf::from(eva_data_dir);
35 previous_ans_path.push("previous_ans.txt"); 35 previous_ans_path.push("previous_ans.txt");
36 36
37 let file = OpenOptions::new() 37 let file = OpenOptions::new()
@@ -43,11 +43,10 @@ impl Highlighter for LineHighlighter {
43 43
44 let rdr = BufReader::new(file); 44 let rdr = BufReader::new(file);
45 let lines = rdr.lines().map(|l| l.unwrap()); 45 let lines = rdr.lines().map(|l| l.unwrap());
46 let prev_ans = lines 46 let prev_ans = match lines.last() {
47 .last() 47 Some(val) => val.parse::<f64>().ok(),
48 .unwrap() 48 None => None,
49 .parse::<f64>() 49 };
50 .ok();
51 let op = eval_math_expression(line, prev_ans); 50 let op = eval_math_expression(line, prev_ans);
52 match op { 51 match op {
53 Ok(_) => { 52 Ok(_) => {
@@ -114,7 +113,7 @@ pub fn create_readline() -> Editor<RLHelper> {
114 let mut rl = Editor::with_config(config); 113 let mut rl = Editor::with_config(config);
115 let h = RLHelper { 114 let h = RLHelper {
116 completer: FilenameCompleter::new(), 115 completer: FilenameCompleter::new(),
117 highlighter: LineHighlighter { }, 116 highlighter: LineHighlighter {},
118 hinter: HistoryHinter {}, 117 hinter: HistoryHinter {},
119 }; 118 };
120 rl.set_helper(Some(h)); 119 rl.set_helper(Some(h));