From 2a3be003015bac9c6a13549029b9fb4595e88384 Mon Sep 17 00:00:00 2001 From: Akshay Date: Mon, 20 Jul 2020 21:13:45 +0530 Subject: allow "untracking" of habits, closes #18 --- Cargo.lock | 44 +------------------------------------------- src/habit/bit.rs | 17 ++++++++++++++--- src/habit/count.rs | 7 +++++-- src/habit/prelude.rs | 1 + 4 files changed, 21 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 63d34ea..ade0cc4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -91,12 +91,6 @@ dependencies = [ "constant_time_eq", ] -[[package]] -name = "cc" -version = "1.0.58" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f9a06fb2e53271d7c279ec1efea6ab691c35a2ae67ec0d91d7acec0caf13b518" - [[package]] name = "cfg-if" version = "0.1.10" @@ -201,10 +195,7 @@ dependencies = [ "lazy_static", "libc", "log", - "maplit", - "ncurses", "signal-hook", - "term_size", "termion", "unicode-segmentation", "unicode-width", @@ -269,7 +260,7 @@ dependencies = [ [[package]] name = "dijo" -version = "0.1.3" +version = "0.1.4" dependencies = [ "chrono", "clap", @@ -539,12 +530,6 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "maplit" -version = "1.0.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "3e2e65a1a2e43cfcb47a895c4c8b10d1f4a61097f9f254f183aee60cad9c651d" - [[package]] name = "maybe-uninit" version = "2.0.0" @@ -594,17 +579,6 @@ dependencies = [ "ws2_32-sys", ] -[[package]] -name = "ncurses" -version = "5.99.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "15699bee2f37e9f8828c7b35b2bc70d13846db453f2d507713b758fabe536b82" -dependencies = [ - "cc", - "libc", - "pkg-config", -] - [[package]] name = "net2" version = "0.2.34" @@ -712,12 +686,6 @@ dependencies = [ "stable_deref_trait", ] -[[package]] -name = "pkg-config" -version = "0.3.18" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d36492546b6af1463394d46f0c834346f31548646f6ba10849802c9c9a27ac33" - [[package]] name = "proc-macro-hack" version = "0.5.16" @@ -881,16 +849,6 @@ dependencies = [ "unicode-xid", ] -[[package]] -name = "term_size" -version = "0.3.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e4129646ca0ed8f45d09b929036bafad5377103edd06e50bf574b353d2b08d9" -dependencies = [ - "libc", - "winapi 0.3.9", -] - [[package]] name = "termion" version = "1.5.5" diff --git a/src/habit/bit.rs b/src/habit/bit.rs index 3386182..8fa14c2 100644 --- a/src/habit/bit.rs +++ b/src/habit/bit.rs @@ -100,11 +100,22 @@ impl Habit for Bit { fn goal(&self) -> u32 { return 1; } - fn modify(&mut self, date: NaiveDate, _: TrackEvent) { + fn modify(&mut self, date: NaiveDate, event: TrackEvent) { if let Some(val) = self.stats.get_mut(&date) { - *val = (val.0 ^ true).into(); + match event { + TrackEvent::Increment => *val = (val.0 ^ true).into(), + TrackEvent::Decrement => { + if val.0 { + *val = false.into(); + } else { + self.stats.remove(&date); + } + } + } } else { - self.insert_entry(date, CustomBool(true)); + if event == TrackEvent::Increment { + self.insert_entry(date, CustomBool(true)); + } } } fn set_view_month_offset(&mut self, offset: u32) { diff --git a/src/habit/count.rs b/src/habit/count.rs index 1bdf920..d351758 100644 --- a/src/habit/count.rs +++ b/src/habit/count.rs @@ -84,12 +84,15 @@ impl Habit for Count { if *val > 0 { *val -= 1 } else { - *val = 0 + self.stats.remove(&date); }; } } } else { - self.insert_entry(date, 1); + match event { + TrackEvent::Increment => self.insert_entry(date, 1), + _ => {} + }; } } fn set_view_month_offset(&mut self, offset: u32) { diff --git a/src/habit/prelude.rs b/src/habit/prelude.rs index 19b00a7..8335d6c 100644 --- a/src/habit/prelude.rs +++ b/src/habit/prelude.rs @@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize}; use std::default; use std::fmt; +#[derive(Debug, PartialEq)] pub enum TrackEvent { Increment, Decrement, -- cgit v1.2.3