From 0b4af96a515d51c409c6dafef406542dee9da3d4 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 18 Jul 2020 21:06:40 +0530 Subject: add smooth error handling --- src/app/impl_self.rs | 83 +++++++++++++++++++++++++++------------------------- src/app/impl_view.rs | 13 +++++++- 2 files changed, 55 insertions(+), 41 deletions(-) (limited to 'src/app') 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; use cursive::Vec2; use notify::{watcher, RecursiveMode, Watcher}; +use crate::command::{Command, CommandLineError}; use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode}; use crate::utils; -use crate::Command; use crate::CONFIGURATION; -use crate::app::{App, StatusLine}; +use crate::app::{App, Message, MessageKind, StatusLine}; impl App { pub fn new() -> Self { @@ -33,6 +33,7 @@ impl App { _file_watcher: watcher, file_event_recv: rx, view_month_offset: 0, + message: Message::default(), }; } @@ -200,47 +201,49 @@ impl App { write_to_file(auto, auto_f); } - pub fn parse_command(&mut self, c: Command) { - match c { - Command::Add(name, goal, auto) => { - let kind = if goal == Some(1) { "bit" } else { "count" }; - if kind == "count" { - self.add_habit(Box::new(Count::new( - name, - goal.unwrap_or(0), - auto.unwrap_or(false), - ))); - } else if kind == "bit" { - self.add_habit(Box::new(Bit::new(name, auto.unwrap_or(false)))); + pub fn parse_command(&mut self, result: Result) { + match result { + Ok(c) => match c { + Command::Add(name, goal, auto) => { + let kind = if goal == Some(1) { "bit" } else { "count" }; + if kind == "count" { + self.add_habit(Box::new(Count::new(name, goal.unwrap_or(0), auto))); + } else if kind == "bit" { + self.add_habit(Box::new(Bit::new(name, auto))); + } } - } - Command::Delete(name) => { - self.delete_by_name(&name); - self.focus = 0; - } - Command::TrackUp(name) => { - let target_habit = self - .habits - .iter_mut() - .find(|x| x.name() == name && x.is_auto()); - if let Some(h) = target_habit { - h.modify(Local::now().naive_utc().date(), TrackEvent::Increment); + Command::Delete(name) => { + self.delete_by_name(&name); + self.focus = 0; } - } - Command::TrackDown(name) => { - let target_habit = self - .habits - .iter_mut() - .find(|x| x.name() == name && x.is_auto()); - if let Some(h) = target_habit { - h.modify(Local::now().naive_utc().date(), TrackEvent::Decrement); + Command::TrackUp(name) => { + let target_habit = self + .habits + .iter_mut() + .find(|x| x.name() == name && x.is_auto()); + if let Some(h) = target_habit { + h.modify(Local::now().naive_utc().date(), TrackEvent::Increment); + } } - } - Command::Quit => self.save_state(), - Command::MonthNext => self.sift_forward(), - Command::MonthPrev => self.sift_backward(), - _ => { - eprintln!("UNKNOWN COMMAND!"); + Command::TrackDown(name) => { + let target_habit = self + .habits + .iter_mut() + .find(|x| x.name() == name && x.is_auto()); + if let Some(h) = target_habit { + h.modify(Local::now().naive_utc().date(), TrackEvent::Decrement); + } + } + Command::Quit => self.save_state(), + Command::MonthNext => self.sift_forward(), + Command::MonthPrev => self.sift_backward(), + _ => { + eprintln!("UNKNOWN COMMAND!"); + } + }, + Err(e) => { + self.message.set_message(e.to_string()); + self.message.set_kind(MessageKind::Error); } } } 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; use cursive::direction::{Absolute, Direction}; use cursive::event::{Event, EventResult, Key}; +use cursive::theme::{Color, Style}; use cursive::view::View; use cursive::{Printer, Vec2}; use notify::DebouncedEvent; -use crate::app::App; +use crate::app::{App, MessageKind}; use crate::habit::{HabitWrapper, ViewMode}; use crate::utils; use crate::CONFIGURATION; @@ -36,6 +37,11 @@ impl View for App { let full = self.max_size().x; offset = offset.map_x(|_| full - status.1.len()); printer.print(offset, &status.1); // right status + + offset = offset.map_x(|_| 0).map_y(|_| self.max_size().y - 1); + printer.with_style(Color::from(self.message.kind()), |p| { + p.print(offset, self.message.contents()) + }); } fn required_size(&mut self, _: Vec2) -> Vec2 { @@ -158,6 +164,11 @@ impl View for App { self.set_view_month_offset(0); return EventResult::Consumed(None); } + Event::CtrlChar('l') => { + self.message.clear(); + self.message.set_kind(MessageKind::Info); + return EventResult::Consumed(None); + } /* Every keybind that is not caught by App trickles * down to the focused habit. We sift back to today -- cgit v1.2.3