diff options
author | Akshay <[email protected]> | 2020-07-18 16:36:40 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-07-18 16:36:40 +0100 |
commit | 0b4af96a515d51c409c6dafef406542dee9da3d4 (patch) | |
tree | 21acafc98b48988d43627b8b8374ad02358f25d1 /src/app/impl_self.rs | |
parent | 29d49dddaae57d59a2c99c376a632a0d9560dcfc (diff) |
add smooth error handling
Diffstat (limited to 'src/app/impl_self.rs')
-rw-r--r-- | src/app/impl_self.rs | 83 |
1 files changed, 43 insertions, 40 deletions
diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs index efed4e0..7bf9ab0 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs | |||
@@ -11,12 +11,12 @@ use cursive::direction::Absolute; | |||
11 | use cursive::Vec2; | 11 | use cursive::Vec2; |
12 | use notify::{watcher, RecursiveMode, Watcher}; | 12 | use notify::{watcher, RecursiveMode, Watcher}; |
13 | 13 | ||
14 | use crate::command::{Command, CommandLineError}; | ||
14 | use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode}; | 15 | use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode}; |
15 | use crate::utils; | 16 | use crate::utils; |
16 | use crate::Command; | ||
17 | use crate::CONFIGURATION; | 17 | use crate::CONFIGURATION; |
18 | 18 | ||
19 | use crate::app::{App, StatusLine}; | 19 | use crate::app::{App, Message, MessageKind, StatusLine}; |
20 | 20 | ||
21 | impl App { | 21 | impl App { |
22 | pub fn new() -> Self { | 22 | pub fn new() -> Self { |
@@ -33,6 +33,7 @@ impl App { | |||
33 | _file_watcher: watcher, | 33 | _file_watcher: watcher, |
34 | file_event_recv: rx, | 34 | file_event_recv: rx, |
35 | view_month_offset: 0, | 35 | view_month_offset: 0, |
36 | message: Message::default(), | ||
36 | }; | 37 | }; |
37 | } | 38 | } |
38 | 39 | ||
@@ -200,47 +201,49 @@ impl App { | |||
200 | write_to_file(auto, auto_f); | 201 | write_to_file(auto, auto_f); |
201 | } | 202 | } |
202 | 203 | ||
203 | pub fn parse_command(&mut self, c: Command) { | 204 | pub fn parse_command(&mut self, result: Result<Command, CommandLineError>) { |
204 | match c { | 205 | match result { |
205 | Command::Add(name, goal, auto) => { | 206 | Ok(c) => match c { |
206 | let kind = if goal == Some(1) { "bit" } else { "count" }; | 207 | Command::Add(name, goal, auto) => { |
207 | if kind == "count" { | 208 | let kind = if goal == Some(1) { "bit" } else { "count" }; |
208 | self.add_habit(Box::new(Count::new( | 209 | if kind == "count" { |
209 | name, | 210 | self.add_habit(Box::new(Count::new(name, goal.unwrap_or(0), auto))); |
210 | goal.unwrap_or(0), | 211 | } else if kind == "bit" { |
211 | auto.unwrap_or(false), | 212 | self.add_habit(Box::new(Bit::new(name, auto))); |
212 | ))); | 213 | } |
213 | } else if kind == "bit" { | ||
214 | self.add_habit(Box::new(Bit::new(name, auto.unwrap_or(false)))); | ||
215 | } | 214 | } |
216 | } | 215 | Command::Delete(name) => { |
217 | Command::Delete(name) => { | 216 | self.delete_by_name(&name); |
218 | self.delete_by_name(&name); | 217 | self.focus = 0; |
219 | self.focus = 0; | ||
220 | } | ||
221 | Command::TrackUp(name) => { | ||
222 | let target_habit = self | ||
223 | .habits | ||
224 | .iter_mut() | ||
225 | .find(|x| x.name() == name && x.is_auto()); | ||
226 | if let Some(h) = target_habit { | ||
227 | h.modify(Local::now().naive_utc().date(), TrackEvent::Increment); | ||
228 | } | 218 | } |
229 | } | 219 | Command::TrackUp(name) => { |
230 | Command::TrackDown(name) => { | 220 | let target_habit = self |
231 | let target_habit = self | 221 | .habits |
232 | .habits | 222 | .iter_mut() |
233 | .iter_mut() | 223 | .find(|x| x.name() == name && x.is_auto()); |
234 | .find(|x| x.name() == name && x.is_auto()); | 224 | if let Some(h) = target_habit { |
235 | if let Some(h) = target_habit { | 225 | h.modify(Local::now().naive_utc().date(), TrackEvent::Increment); |
236 | h.modify(Local::now().naive_utc().date(), TrackEvent::Decrement); | 226 | } |
237 | } | 227 | } |
238 | } | 228 | Command::TrackDown(name) => { |
239 | Command::Quit => self.save_state(), | 229 | let target_habit = self |
240 | Command::MonthNext => self.sift_forward(), | 230 | .habits |
241 | Command::MonthPrev => self.sift_backward(), | 231 | .iter_mut() |
242 | _ => { | 232 | .find(|x| x.name() == name && x.is_auto()); |
243 | eprintln!("UNKNOWN COMMAND!"); | 233 | if let Some(h) = target_habit { |
234 | h.modify(Local::now().naive_utc().date(), TrackEvent::Decrement); | ||
235 | } | ||
236 | } | ||
237 | Command::Quit => self.save_state(), | ||
238 | Command::MonthNext => self.sift_forward(), | ||
239 | Command::MonthPrev => self.sift_backward(), | ||
240 | _ => { | ||
241 | eprintln!("UNKNOWN COMMAND!"); | ||
242 | } | ||
243 | }, | ||
244 | Err(e) => { | ||
245 | self.message.set_message(e.to_string()); | ||
246 | self.message.set_kind(MessageKind::Error); | ||
244 | } | 247 | } |
245 | } | 248 | } |
246 | } | 249 | } |