From 3ded40d04f49983e7907366536dbc94917cee666 Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 14 Jul 2020 17:20:36 +0530 Subject: more boilerplate for auto-trackable habits add funding.yml also :^) --- .github/FUNDING.yml | 1 + src/app.rs | 10 +++++++--- src/command.rs | 4 +++- src/habit/bit.rs | 10 +++++++++- src/habit/count.rs | 10 +++++++++- src/habit/prelude.rs | 4 ++++ src/habit/traits.rs | 28 +++++++++++++++++++--------- src/views.rs | 8 ++++---- 8 files changed, 56 insertions(+), 19 deletions(-) create mode 100644 .github/FUNDING.yml diff --git a/.github/FUNDING.yml b/.github/FUNDING.yml new file mode 100644 index 0000000..ddf2552 --- /dev/null +++ b/.github/FUNDING.yml @@ -0,0 +1 @@ +liberapay: nerdypepper diff --git a/src/app.rs b/src/app.rs index 412cfe5..93e5def 100644 --- a/src/app.rs +++ b/src/app.rs @@ -190,11 +190,15 @@ impl App { pub fn parse_command(&mut self, input: &str) { let c = Command::from_string(input); match c { - Command::Add(name, kind, goal) => { + Command::Add(name, kind, goal, auto) => { if kind == "count" { - self.add_habit(Box::new(Count::new(name, goal.unwrap_or(0)))); + 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))); + self.add_habit(Box::new(Bit::new(name, auto.unwrap_or(false)))); } } Command::Delete(name) => { diff --git a/src/command.rs b/src/command.rs index afc00ba..c1a855e 100644 --- a/src/command.rs +++ b/src/command.rs @@ -27,7 +27,7 @@ fn call_on_app(s: &mut Cursive, input: &str) { #[derive(PartialEq)] pub enum Command { - Add(String, String, Option), // habit name, habit type, optional goal + Add(String, String, Option, Option), // habit name, habit type, optional goal, auto tracked MonthPrev, MonthNext, Delete(String), @@ -50,10 +50,12 @@ impl Command { return Command::Blank; } let goal = args.get(2).map(|g| g.parse::().ok()).flatten(); + let auto = args.get(3).map(|g| g.parse::().ok()).flatten(); return Command::Add( args.get_mut(0).unwrap().to_string(), args.get_mut(1).unwrap().to_string(), goal, + auto, ); } "delete" | "d" => { diff --git a/src/habit/bit.rs b/src/habit/bit.rs index 292b96a..3386182 100644 --- a/src/habit/bit.rs +++ b/src/habit/bit.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use chrono::NaiveDate; use serde::{Deserialize, Serialize}; +use crate::habit::prelude::default_auto; use crate::habit::traits::Habit; use crate::habit::{TrackEvent, ViewMode}; use crate::CONFIGURATION; @@ -37,6 +38,9 @@ pub struct Bit { stats: HashMap, goal: CustomBool, + #[serde(default = "default_auto")] + auto: bool, + #[serde(skip)] view_month_offset: u32, @@ -45,11 +49,12 @@ pub struct Bit { } impl Bit { - pub fn new(name: impl AsRef) -> Self { + pub fn new(name: impl AsRef, auto: bool) -> Self { return Bit { name: name.as_ref().to_owned(), stats: HashMap::new(), goal: CustomBool(true), + auto, view_month_offset: 0, view_mode: ViewMode::Day, }; @@ -114,4 +119,7 @@ impl Habit for Bit { fn view_mode(&self) -> ViewMode { self.view_mode } + fn is_auto(&self) -> bool { + self.auto + } } diff --git a/src/habit/count.rs b/src/habit/count.rs index a0e0aee..1bdf920 100644 --- a/src/habit/count.rs +++ b/src/habit/count.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use chrono::NaiveDate; use serde::{Deserialize, Serialize}; +use crate::habit::prelude::default_auto; use crate::habit::traits::Habit; use crate::habit::{TrackEvent, ViewMode}; @@ -12,6 +13,9 @@ pub struct Count { stats: HashMap, goal: u32, + #[serde(default = "default_auto")] + auto: bool, + #[serde(skip)] view_month_offset: u32, @@ -20,11 +24,12 @@ pub struct Count { } impl Count { - pub fn new(name: impl AsRef, goal: u32) -> Self { + pub fn new(name: impl AsRef, goal: u32, auto: bool) -> Self { return Count { name: name.as_ref().to_owned(), stats: HashMap::new(), goal, + auto, view_month_offset: 0, view_mode: ViewMode::Day, }; @@ -99,4 +104,7 @@ impl Habit for Count { fn view_mode(&self) -> ViewMode { self.view_mode } + fn is_auto(&self) -> bool { + self.auto + } } diff --git a/src/habit/prelude.rs b/src/habit/prelude.rs index 9196f00..b8b2bb2 100644 --- a/src/habit/prelude.rs +++ b/src/habit/prelude.rs @@ -18,3 +18,7 @@ impl std::default::Default for ViewMode { ViewMode::Day } } + +pub fn default_auto() -> bool { + false +} diff --git a/src/habit/traits.rs b/src/habit/traits.rs index e28e55d..5092bc5 100644 --- a/src/habit/traits.rs +++ b/src/habit/traits.rs @@ -26,6 +26,8 @@ pub trait Habit { fn set_view_mode(&mut self, mode: ViewMode); fn view_mode(&self) -> ViewMode; + + fn is_auto(&self) -> bool; } #[typetag::serde(tag = "type")] @@ -44,21 +46,15 @@ pub trait HabitWrapper: erased_serde::Serialize { fn set_view_mode(&mut self, mode: ViewMode); fn view_mode(&self) -> ViewMode; + + fn is_auto(&self) -> bool; } macro_rules! auto_habit_impl { ($struct_name:ident) => { #[typetag::serde] impl HabitWrapper for $struct_name { - fn remaining(&self, date: NaiveDate) -> u32 { - Habit::remaining(self, date) - } - fn goal(&self) -> u32 { - Habit::goal(self) - } - fn modify(&mut self, date: NaiveDate, event: TrackEvent) { - Habit::modify(self, date, event); - } + // ShadowView fn draw(&self, printer: &Printer) { ShadowView::draw(self, printer) } @@ -71,6 +67,17 @@ macro_rules! auto_habit_impl { fn take_focus(&mut self, d: Direction) -> bool { ShadowView::take_focus(self, d) } + + // Habit + fn remaining(&self, date: NaiveDate) -> u32 { + Habit::remaining(self, date) + } + fn goal(&self) -> u32 { + Habit::goal(self) + } + fn modify(&mut self, date: NaiveDate, event: TrackEvent) { + Habit::modify(self, date, event); + } fn get_name(&self) -> String { Habit::name(self) } @@ -86,6 +93,9 @@ macro_rules! auto_habit_impl { fn view_mode(&self) -> ViewMode { Habit::view_mode(self) } + fn is_auto(&self) -> bool { + Habit::is_auto(self) + } } }; } diff --git a/src/views.rs b/src/views.rs index 9e4a844..54d5431 100644 --- a/src/views.rs +++ b/src/views.rs @@ -63,7 +63,7 @@ where }, ); - let draw_month = |printer: &Printer| { + let draw_week = |printer: &Printer| { let days = (1..31) .map(|i| NaiveDate::from_ymd_opt(year, month, i)) .flatten() // dates 28-31 may not exist, ignore them if they don't @@ -77,10 +77,10 @@ where let bars_to_fill = (completions * full as u32) / weekly_goal; let percentage = (completions as f64 * 100.) / weekly_goal as f64; printer.with_style(future_style, |p| { - p.print((4, line_nr), &"―".repeat(full)); + p.print((4, line_nr), &"─".repeat(full)); }); printer.with_style(goal_reached_style, |p| { - p.print((4, line_nr), &"―".repeat(bars_to_fill as usize)); + p.print((4, line_nr), &"─".repeat(bars_to_fill as usize)); }); printer.with_style( if is_this_week { @@ -120,7 +120,7 @@ where match self.view_mode() { ViewMode::Day => draw_day(printer), - ViewMode::Week => draw_month(printer), + ViewMode::Week => draw_week(printer), _ => draw_day(printer), }; } -- cgit v1.2.3