aboutsummaryrefslogtreecommitdiff
path: root/src/app
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-07-18 16:36:40 +0100
committerAkshay <[email protected]>2020-07-18 16:36:40 +0100
commit0b4af96a515d51c409c6dafef406542dee9da3d4 (patch)
tree21acafc98b48988d43627b8b8374ad02358f25d1 /src/app
parent29d49dddaae57d59a2c99c376a632a0d9560dcfc (diff)
add smooth error handling
Diffstat (limited to 'src/app')
-rw-r--r--src/app/impl_self.rs83
-rw-r--r--src/app/impl_view.rs13
2 files changed, 55 insertions, 41 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;
11use cursive::Vec2; 11use cursive::Vec2;
12use notify::{watcher, RecursiveMode, Watcher}; 12use notify::{watcher, RecursiveMode, Watcher};
13 13
14use crate::command::{Command, CommandLineError};
14use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode}; 15use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode};
15use crate::utils; 16use crate::utils;
16use crate::Command;
17use crate::CONFIGURATION; 17use crate::CONFIGURATION;
18 18
19use crate::app::{App, StatusLine}; 19use crate::app::{App, Message, MessageKind, StatusLine};
20 20
21impl App { 21impl 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 }
diff --git a/src/app/impl_view.rs b/src/app/impl_view.rs
index 904403b..0a6bce6 100644
--- a/src/app/impl_view.rs
+++ b/src/app/impl_view.rs
@@ -5,11 +5,12 @@ use std::path::PathBuf;
5 5
6use cursive::direction::{Absolute, Direction}; 6use cursive::direction::{Absolute, Direction};
7use cursive::event::{Event, EventResult, Key}; 7use cursive::event::{Event, EventResult, Key};
8use cursive::theme::{Color, Style};
8use cursive::view::View; 9use cursive::view::View;
9use cursive::{Printer, Vec2}; 10use cursive::{Printer, Vec2};
10use notify::DebouncedEvent; 11use notify::DebouncedEvent;
11 12
12use crate::app::App; 13use crate::app::{App, MessageKind};
13use crate::habit::{HabitWrapper, ViewMode}; 14use crate::habit::{HabitWrapper, ViewMode};
14use crate::utils; 15use crate::utils;
15use crate::CONFIGURATION; 16use crate::CONFIGURATION;
@@ -36,6 +37,11 @@ impl View for App {
36 let full = self.max_size().x; 37 let full = self.max_size().x;
37 offset = offset.map_x(|_| full - status.1.len()); 38 offset = offset.map_x(|_| full - status.1.len());
38 printer.print(offset, &status.1); // right status 39 printer.print(offset, &status.1); // right status
40
41 offset = offset.map_x(|_| 0).map_y(|_| self.max_size().y - 1);
42 printer.with_style(Color::from(self.message.kind()), |p| {
43 p.print(offset, self.message.contents())
44 });
39 } 45 }
40 46
41 fn required_size(&mut self, _: Vec2) -> Vec2 { 47 fn required_size(&mut self, _: Vec2) -> Vec2 {
@@ -158,6 +164,11 @@ impl View for App {
158 self.set_view_month_offset(0); 164 self.set_view_month_offset(0);
159 return EventResult::Consumed(None); 165 return EventResult::Consumed(None);
160 } 166 }
167 Event::CtrlChar('l') => {
168 self.message.clear();
169 self.message.set_kind(MessageKind::Info);
170 return EventResult::Consumed(None);
171 }
161 172
162 /* Every keybind that is not caught by App trickles 173 /* Every keybind that is not caught by App trickles
163 * down to the focused habit. We sift back to today 174 * down to the focused habit. We sift back to today