From 188827f3548ea8ca1ab84735b8fe19f99c790207 Mon Sep 17 00:00:00 2001 From: Akshay Date: Fri, 10 Jul 2020 21:51:37 +0530 Subject: redo quit command --- src/app.rs | 27 ++++++++++++++++----------- src/command.rs | 10 ++++++++++ 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/src/app.rs b/src/app.rs index 44de5bb..e322091 100644 --- a/src/app.rs +++ b/src/app.rs @@ -32,8 +32,10 @@ struct StatusLine(String, String); #[derive(Serialize, Deserialize)] pub struct App { + // holds app data habits: Vec>, + // serialization of the rest can be skipped #[serde(skip)] focus: usize, @@ -135,6 +137,7 @@ impl App { width = CONFIGURATION.view_width * CONFIGURATION.grid_width ) } else { + let months = self.view_month_offset; format!( "{:>width$}", format!("{} months ago", self.view_month_offset), @@ -190,14 +193,9 @@ impl App { self.delete_by_name(&name); self.focus = 0; } + Command::Quit => self.save_state(), Command::MonthNext => self.sift_forward(), Command::MonthPrev => self.sift_backward(), - - // we can get away with calling an event here, - // saves us some writing - Command::Quit => { - self.on_event(Event::Char('q')); - } _ => { eprintln!("UNKNOWN COMMAND!"); } @@ -297,20 +295,27 @@ impl View for App { /* We want sifting to be an app level function, * that later trickles down into each habit * */ - Event::CtrlChar('f') => { + Event::Char(']') => { self.sift_forward(); return EventResult::Consumed(None); } - Event::CtrlChar('b') => { + Event::Char('[') => { self.sift_backward(); return EventResult::Consumed(None); } + Event::Char('}') => { + self.set_view_month_offset(0); + return EventResult::Consumed(None); + } - /* Every keybind that is not caught by App trickle - * s down to the focused Habit We sift back to today + /* Every keybind that is not caught by App trickles + * down to the focused Habit We sift back to today * before performing any action, "refocusing" the cursor * */ - _ => self.habits[self.focus].on_event(e), + _ => { + self.set_view_month_offset(0); + self.habits[self.focus].on_event(e) + } } } } diff --git a/src/command.rs b/src/command.rs index d25608f..afc00ba 100644 --- a/src/command.rs +++ b/src/command.rs @@ -13,9 +13,19 @@ fn call_on_app(s: &mut Cursive, input: &str) { s.call_on_name("Main", |view: &mut App| { view.parse_command(input); }); + + // special command that requires access to + // our main cursive object, has to be parsed again + // here + // TODO: fix this somehow + if Command::from_string(input) == Command::Quit { + s.quit(); + } + s.pop_layer(); } +#[derive(PartialEq)] pub enum Command { Add(String, String, Option), // habit name, habit type, optional goal MonthPrev, -- cgit v1.2.3