From 7d5e46626110a351104ededeaebdcb9723c7e786 Mon Sep 17 00:00:00 2001 From: Akshay Date: Thu, 16 Jul 2020 21:46:26 +0530 Subject: add track commands --- src/app.rs | 33 ++++++++++++++++++++++++--------- src/command.rs | 17 ++++++++++++++++- 2 files changed, 40 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index dafeed5..ab0bcd3 100644 --- a/src/app.rs +++ b/src/app.rs @@ -11,7 +11,7 @@ use cursive::{Printer, Vec2}; use chrono::Local; -use crate::habit::{Bit, Count, HabitWrapper, ViewMode}; +use crate::habit::{Bit, Count, HabitWrapper, TrackEvent, ViewMode}; use crate::utils; use crate::Command; use crate::CONFIGURATION; @@ -46,7 +46,7 @@ impl App { } pub fn delete_by_name(&mut self, name: &str) { - self.habits.retain(|h| h.get_name() != name); + self.habits.retain(|h| h.name() != name); } pub fn get_mode(&self) -> ViewMode { @@ -184,7 +184,7 @@ impl App { // this function does IO // TODO: convert this into non-blocking async function - fn save_state(&self) { + pub fn save_state(&self) { let (regular, auto): (Vec<_>, Vec<_>) = self.habits.iter().partition(|&x| !x.is_auto()); let (regular_f, auto_f) = (utils::habit_file(), utils::auto_habit_file()); @@ -205,8 +205,7 @@ impl App { write_to_file(auto, auto_f); } - pub fn parse_command(&mut self, input: &str) { - let c = Command::from_string(input); + pub fn parse_command(&mut self, c: Command) { match c { Command::Add(name, goal, auto) => { let kind = if goal == Some(1) { "bit" } else { "count" }; @@ -224,6 +223,24 @@ impl App { 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::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(), @@ -253,10 +270,8 @@ impl View for App { let status = self.status(); printer.print(offset, &status.0); // left status - let full = grid_width * (view_width + 2); - offset = offset - .map_x(|_| full - status.1.len()) - .map_y(|_| self.max_size().y - 2); + let full = self.max_size().x; + offset = offset.map_x(|_| full - status.1.len()); printer.print(offset, &status.1); // right status } diff --git a/src/command.rs b/src/command.rs index ae1b307..79d0fe5 100644 --- a/src/command.rs +++ b/src/command.rs @@ -11,7 +11,8 @@ pub fn open_command_window(s: &mut Cursive) { fn call_on_app(s: &mut Cursive, input: &str) { s.call_on_name("Main", |view: &mut App| { - view.parse_command(input); + let cmd = Command::from_string(input); + view.parse_command(cmd); }); // special command that requires access to @@ -31,6 +32,8 @@ pub enum Command { MonthPrev, MonthNext, Delete(String), + TrackUp(String), + TrackDown(String), Quit, Blank, } @@ -59,6 +62,18 @@ impl Command { } return Command::Delete(args[0].to_string()); } + "track-up" | "tup" => { + if args.len() < 1 { + return Command::Blank; + } + return Command::TrackUp(args[0].to_string()); + } + "track-down" | "tdown" => { + if args.len() < 1 { + return Command::Blank; + } + return Command::TrackDown(args[0].to_string()); + } "mprev" | "month-prev" => return Command::MonthPrev, "mnext" | "month-next" => return Command::MonthNext, "q" | "quit" => return Command::Quit, -- cgit v1.2.3