From 77368eed146ec8278609e04e395c7ef04c386313 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 6 Feb 2021 19:00:40 +0530 Subject: deprecate view_month_offset in favor of cursor --- src/app/cursor.rs | 22 ++++++++++++++++++- src/app/impl_self.rs | 41 ++++++++++++++++++----------------- src/app/impl_view.rs | 10 ++++----- src/habit/bit.rs | 37 +++++++------------------------ src/habit/count.rs | 37 +++++++------------------------ src/habit/mod.rs | 27 +++++++++++++++++++++++ src/habit/traits.rs | 61 ++++++++++++++++------------------------------------ src/views.rs | 8 +++---- 8 files changed, 112 insertions(+), 131 deletions(-) (limited to 'src') diff --git a/src/app/cursor.rs b/src/app/cursor.rs index ed6bd65..f76d591 100644 --- a/src/app/cursor.rs +++ b/src/app/cursor.rs @@ -16,7 +16,7 @@ impl Cursor { 0: Local::now().naive_local().date(), } } - pub fn do_move(&mut self, d: Absolute) { + pub fn small_seek(&mut self, d: Absolute) { let today = Local::now().naive_local().date(); let cursor = self.0; match d { @@ -48,4 +48,24 @@ impl Cursor { Absolute::None => {} } } + fn long_seek(&mut self, offset: Duration) { + let cursor = self.0; + let today = Local::now().naive_local().date(); + let next = cursor.checked_add_signed(offset).unwrap_or(cursor); + + if next <= today { + self.0 = next; + } else { + self.0 = today; + } + } + pub fn month_forward(&mut self) { + self.long_seek(Duration::weeks(4)); + } + pub fn month_backward(&mut self) { + self.long_seek(Duration::weeks(-4)); + } + pub fn reset(&mut self) { + self.0 = Local::now().naive_local().date(); + } } diff --git a/src/app/impl_self.rs b/src/app/impl_self.rs index abf5209..fec7219 100644 --- a/src/app/impl_self.rs +++ b/src/app/impl_self.rs @@ -6,7 +6,7 @@ use std::path::PathBuf; use std::sync::mpsc::channel; use std::time::Duration; -use chrono::Local; +use chrono::{Local, NaiveDate}; use cursive::direction::Absolute; use cursive::Vec2; use notify::{watcher, RecursiveMode, Watcher}; @@ -54,42 +54,42 @@ impl App { if self.habits.is_empty() { return ViewMode::Day; } - return self.habits[self.focus].view_mode(); + return self.habits[self.focus].inner_data_ref().view_mode(); } pub fn set_mode(&mut self, mode: ViewMode) { if !self.habits.is_empty() { - self.habits[self.focus].set_view_mode(mode); + self.habits[self.focus] + .inner_data_mut_ref() + .set_view_mode(mode); } } - pub fn set_view_month_offset(&mut self, offset: u32) { - self.view_month_offset = offset; + pub fn sift_backward(&mut self) { + self.cursor.month_backward(); for v in self.habits.iter_mut() { - v.set_view_month_offset(offset); + v.inner_data_mut_ref().cursor.month_backward(); } } - pub fn sift_backward(&mut self) { - self.view_month_offset += 1; + pub fn sift_forward(&mut self) { + self.cursor.month_forward(); for v in self.habits.iter_mut() { - v.set_view_month_offset(self.view_month_offset); + v.inner_data_mut_ref().cursor.month_forward(); } } - pub fn sift_forward(&mut self) { - if self.view_month_offset > 0 { - self.view_month_offset -= 1; - for v in self.habits.iter_mut() { - v.set_view_month_offset(self.view_month_offset); - } + pub fn reset_cursor(&mut self) { + self.cursor.reset(); + for v in self.habits.iter_mut() { + v.inner_data_mut_ref().cursor.reset(); } } pub fn move_cursor(&mut self, d: Absolute) { - self.cursor.do_move(d); + self.cursor.small_seek(d); for v in self.habits.iter_mut() { - v.move_cursor(d); + v.inner_data_mut_ref().move_cursor(d); } } @@ -133,11 +133,12 @@ impl App { let total = self.habits.iter().map(|h| h.goal()).sum::(); let completed = total - remaining; - let timestamp = if self.view_month_offset == 0 { + let timestamp = if self.cursor.0 == today { format!("{}", Local::now().naive_local().date().format("%d/%b/%y"),) } else { - let months = self.view_month_offset; - format!("{}", format!("{} month(s) ago", months),) + let since = NaiveDate::signed_duration_since(today, self.cursor.0).num_days(); + let plural = if since == 1 { "" } else { "s" }; + format!("{} ({} day{} ago)", self.cursor.0, since, plural) }; StatusLine { diff --git a/src/app/impl_view.rs b/src/app/impl_view.rs index 0ec47f1..db05432 100644 --- a/src/app/impl_view.rs +++ b/src/app/impl_view.rs @@ -117,7 +117,7 @@ impl View for App { if self.habits.is_empty() { return EventResult::Consumed(None); } - if self.habits[self.focus].view_mode() == ViewMode::Week { + if self.habits[self.focus].inner_data_ref().view_mode() == ViewMode::Week { self.set_mode(ViewMode::Day) } else { self.set_mode(ViewMode::Week) @@ -126,13 +126,13 @@ impl View for App { } Event::Char('V') => { for habit in self.habits.iter_mut() { - habit.set_view_mode(ViewMode::Week); + habit.inner_data_mut_ref().set_view_mode(ViewMode::Week); } return EventResult::Consumed(None); } Event::Key(Key::Esc) => { for habit in self.habits.iter_mut() { - habit.set_view_mode(ViewMode::Day); + habit.inner_data_mut_ref().set_view_mode(ViewMode::Day); } return EventResult::Consumed(None); } @@ -149,7 +149,7 @@ impl View for App { return EventResult::Consumed(None); } Event::Char('}') => { - self.set_view_month_offset(0); + self.reset_cursor(); return EventResult::Consumed(None); } Event::CtrlChar('l') => { @@ -166,7 +166,7 @@ impl View for App { if self.habits.is_empty() { return EventResult::Ignored; } - self.set_view_month_offset(0); + self.reset_cursor(); self.habits[self.focus].on_event(e) } } diff --git a/src/habit/bit.rs b/src/habit/bit.rs index 7fe6fd9..da64ece 100644 --- a/src/habit/bit.rs +++ b/src/habit/bit.rs @@ -1,13 +1,12 @@ use std::collections::HashMap; +use std::default::Default; use chrono::NaiveDate; -use cursive::direction::Absolute; use serde::{Deserialize, Serialize}; -use crate::app::Cursor; use crate::habit::prelude::default_auto; use crate::habit::traits::Habit; -use crate::habit::{TrackEvent, ViewMode}; +use crate::habit::{InnerData, TrackEvent}; use crate::CONFIGURATION; #[derive(Copy, Clone, Debug, Serialize, Deserialize)] @@ -44,13 +43,7 @@ pub struct Bit { auto: bool, #[serde(skip)] - view_month_offset: u32, - - #[serde(skip)] - cursor: Cursor, - - #[serde(skip)] - view_mode: ViewMode, + inner_data: InnerData, } impl Bit { @@ -60,9 +53,7 @@ impl Bit { stats: HashMap::new(), goal: CustomBool(true), auto, - view_month_offset: 0, - cursor: Cursor::new(), - view_mode: ViewMode::Day, + inner_data: Default::default(), }; } } @@ -124,23 +115,11 @@ impl Habit for Bit { } } } - fn set_view_month_offset(&mut self, offset: u32) { - self.view_month_offset = offset; - } - fn view_month_offset(&self) -> u32 { - self.view_month_offset - } - fn move_cursor(&mut self, d: Absolute) { - self.cursor.do_move(d); - } - fn cursor(&self) -> Cursor { - self.cursor - } - fn set_view_mode(&mut self, mode: ViewMode) { - self.view_mode = mode; + fn inner_data_ref(&self) -> &InnerData { + &self.inner_data } - fn view_mode(&self) -> ViewMode { - self.view_mode + fn inner_data_mut_ref(&mut self) -> &mut InnerData { + &mut self.inner_data } fn is_auto(&self) -> bool { self.auto diff --git a/src/habit/count.rs b/src/habit/count.rs index b14354c..09fd399 100644 --- a/src/habit/count.rs +++ b/src/habit/count.rs @@ -1,13 +1,12 @@ use std::collections::HashMap; +use std::default::Default; use chrono::NaiveDate; -use cursive::direction::Absolute; use serde::{Deserialize, Serialize}; -use crate::app::Cursor; use crate::habit::prelude::default_auto; use crate::habit::traits::Habit; -use crate::habit::{TrackEvent, ViewMode}; +use crate::habit::{InnerData, TrackEvent}; #[derive(Debug, Serialize, Deserialize)] pub struct Count { @@ -19,13 +18,7 @@ pub struct Count { auto: bool, #[serde(skip)] - view_month_offset: u32, - - #[serde(skip)] - cursor: Cursor, - - #[serde(skip)] - view_mode: ViewMode, + inner_data: InnerData, } impl Count { @@ -35,9 +28,7 @@ impl Count { stats: HashMap::new(), goal, auto, - view_month_offset: 0, - cursor: Cursor::new(), - view_mode: ViewMode::Day, + inner_data: Default::default(), }; } } @@ -101,23 +92,11 @@ impl Habit for Count { }; } } - fn set_view_month_offset(&mut self, offset: u32) { - self.view_month_offset = offset; - } - fn view_month_offset(&self) -> u32 { - self.view_month_offset - } - fn move_cursor(&mut self, d: Absolute) { - self.cursor.do_move(d); - } - fn cursor(&self) -> Cursor { - self.cursor - } - fn set_view_mode(&mut self, mode: ViewMode) { - self.view_mode = mode; + fn inner_data_ref(&self) -> &InnerData { + &self.inner_data } - fn view_mode(&self) -> ViewMode { - self.view_mode + fn inner_data_mut_ref(&mut self) -> &mut InnerData { + &mut self.inner_data } fn is_auto(&self) -> bool { self.auto diff --git a/src/habit/mod.rs b/src/habit/mod.rs index 75e734a..d51abe5 100644 --- a/src/habit/mod.rs +++ b/src/habit/mod.rs @@ -1,3 +1,5 @@ +use std::default::Default; + mod traits; pub use traits::{Habit, HabitWrapper}; @@ -9,3 +11,28 @@ pub use bit::Bit; mod prelude; pub use prelude::{TrackEvent, ViewMode}; + +use crate::app::Cursor; + +use cursive::direction::Absolute; + +#[derive(Debug, Default)] +pub struct InnerData { + pub cursor: Cursor, + pub view_mode: ViewMode, +} + +impl InnerData { + pub fn move_cursor(&mut self, d: Absolute) { + self.cursor.small_seek(d); + } + pub fn cursor(&self) -> Cursor { + self.cursor + } + pub fn set_view_mode(&mut self, mode: ViewMode) { + self.view_mode = mode; + } + pub fn view_mode(&self) -> ViewMode { + self.view_mode + } +} diff --git a/src/habit/traits.rs b/src/habit/traits.rs index 289fd95..24d941d 100644 --- a/src/habit/traits.rs +++ b/src/habit/traits.rs @@ -1,58 +1,45 @@ use chrono::NaiveDate; -use cursive::direction::{Absolute, Direction}; +use cursive::direction::Direction; use cursive::event::{Event, EventResult}; use cursive::{Printer, Vec2}; use typetag; -use crate::app::Cursor; -use crate::habit::{Bit, Count, TrackEvent, ViewMode}; +use crate::habit::{Bit, Count, InnerData, TrackEvent}; use crate::views::ShadowView; pub trait Habit { type HabitType; - fn set_name(&mut self, name: impl AsRef); - fn set_goal(&mut self, goal: Self::HabitType); - fn name(&self) -> String; fn get_by_date(&self, date: NaiveDate) -> Option<&Self::HabitType>; + fn goal(&self) -> u32; fn insert_entry(&mut self, date: NaiveDate, val: Self::HabitType); + fn modify(&mut self, date: NaiveDate, event: TrackEvent); + fn name(&self) -> String; fn reached_goal(&self, date: NaiveDate) -> bool; fn remaining(&self, date: NaiveDate) -> u32; - fn goal(&self) -> u32; - fn modify(&mut self, date: NaiveDate, event: TrackEvent); - - fn set_view_month_offset(&mut self, offset: u32); - fn view_month_offset(&self) -> u32; - - fn move_cursor(&mut self, d: Absolute); - fn cursor(&self) -> Cursor; + fn set_goal(&mut self, goal: Self::HabitType); + fn set_name(&mut self, name: impl AsRef); - fn set_view_mode(&mut self, mode: ViewMode); - fn view_mode(&self) -> ViewMode; + fn inner_data_ref(&self) -> &InnerData; + fn inner_data_mut_ref(&mut self) -> &mut InnerData; fn is_auto(&self) -> bool; } #[typetag::serde(tag = "type")] pub trait HabitWrapper: erased_serde::Serialize { - fn remaining(&self, date: NaiveDate) -> u32; + fn draw(&self, printer: &Printer); fn goal(&self) -> u32; fn modify(&mut self, date: NaiveDate, event: TrackEvent); - fn draw(&self, printer: &Printer); + fn name(&self) -> String; fn on_event(&mut self, event: Event) -> EventResult; + fn remaining(&self, date: NaiveDate) -> u32; fn required_size(&mut self, _: Vec2) -> Vec2; fn take_focus(&mut self, _: Direction) -> bool; - fn name(&self) -> String; - fn set_view_month_offset(&mut self, offset: u32); - fn view_month_offset(&self) -> u32; - - fn move_cursor(&mut self, d: Absolute); - fn cursor(&self) -> Cursor; - - fn set_view_mode(&mut self, mode: ViewMode); - fn view_mode(&self) -> ViewMode; + fn inner_data_ref(&self) -> &InnerData; + fn inner_data_mut_ref(&mut self) -> &mut InnerData; fn is_auto(&self) -> bool; } @@ -88,23 +75,11 @@ macro_rules! auto_habit_impl { fn name(&self) -> String { Habit::name(self) } - fn set_view_month_offset(&mut self, offset: u32) { - Habit::set_view_month_offset(self, offset) - } - fn view_month_offset(&self) -> u32 { - Habit::view_month_offset(self) - } - fn move_cursor(&mut self, d: Absolute) { - Habit::move_cursor(self, d) - } - fn cursor(&self) -> Cursor { - Habit::cursor(self) - } - fn set_view_mode(&mut self, mode: ViewMode) { - Habit::set_view_mode(self, mode) + fn inner_data_ref(&self) -> &InnerData { + Habit::inner_data_ref(self) } - fn view_mode(&self) -> ViewMode { - Habit::view_mode(self) + fn inner_data_mut_ref(&mut self) -> &mut InnerData { + Habit::inner_data_mut_ref(self) } fn is_auto(&self) -> bool { Habit::is_auto(self) diff --git a/src/views.rs b/src/views.rs index a306602..f02eb83 100644 --- a/src/views.rs +++ b/src/views.rs @@ -1,6 +1,6 @@ use cursive::direction::Direction; use cursive::event::{Event, EventResult, Key}; -use cursive::theme::{ColorStyle, ColorType, Effect, Style}; +use cursive::theme::{ColorStyle, Effect, Style}; use cursive::view::View; use cursive::{Printer, Vec2}; @@ -35,7 +35,7 @@ where // .checked_sub_signed(Duration::weeks(4 * self.view_month_offset() as i64)) // .unwrap() // }; - let now = self.cursor().0; + let now = self.inner_data_ref().cursor().0; let is_today = now == Local::now().naive_local().date(); let year = now.year(); let month = now.month(); @@ -141,7 +141,7 @@ where } }; - match self.view_mode() { + match self.inner_data_ref().view_mode() { ViewMode::Day => draw_day(printer), ViewMode::Week => draw_week(printer), _ => draw_day(printer), @@ -157,7 +157,7 @@ where } fn on_event(&mut self, e: Event) -> EventResult { - let now = self.cursor().0; + let now = self.inner_data_mut_ref().cursor().0; if self.is_auto() { return EventResult::Ignored; } -- cgit v1.2.3