From ce6db76dc3d8f2f8b43a2e8769cf17416e57a065 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 11 Jul 2020 22:17:45 +0530 Subject: begin work on month mode --- src/app.rs | 29 ++++++----------------------- src/habit.rs | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++--- src/views.rs | 51 +++++++++++++++++++++++++++++++-------------------- 3 files changed, 87 insertions(+), 46 deletions(-) diff --git a/src/app.rs b/src/app.rs index 82096e1..f39ae3c 100644 --- a/src/app.rs +++ b/src/app.rs @@ -9,26 +9,13 @@ use cursive::{Printer, Vec2}; use chrono::{Local, NaiveDate}; -use crate::habit::{Bit, Count, Habit, HabitWrapper}; +use crate::habit::{Bit, Count, Habit, HabitWrapper, ViewMode}; use crate::utils; use crate::Command; use crate::CONFIGURATION; use serde::{Deserialize, Serialize}; -#[derive(PartialEq, Serialize, Deserialize)] -pub enum ViewMode { - Day, - Month, - Year, -} - -impl std::default::Default for ViewMode { - fn default() -> Self { - ViewMode::Month - } -} - struct StatusLine(String, String); #[derive(Serialize, Deserialize)] @@ -40,9 +27,6 @@ pub struct App { #[serde(skip)] focus: usize, - #[serde(skip)] - view_mode: ViewMode, - #[serde(skip)] view_month_offset: u32, } @@ -51,7 +35,6 @@ impl App { pub fn new() -> Self { return App { habits: vec![], - view_mode: ViewMode::Day, focus: 0, view_month_offset: 0, }; @@ -65,9 +48,9 @@ impl App { self.habits.retain(|h| h.get_name() != name); } - pub fn set_mode(&mut self, set_mode: ViewMode) { - if set_mode != self.view_mode { - self.view_mode = set_mode; + pub fn set_mode(&mut self, mode: ViewMode) { + if !self.habits.is_empty() { + self.habits[self.focus].set_view_mode(mode); } } @@ -233,8 +216,8 @@ impl View for App { } offset = offset.map_x(|_| 0).map_y(|_| self.max_size().y - 2); - printer.print(offset, &self.status().1); // right - printer.print(offset, &self.status().0); // left + printer.print(offset, &self.status().1); // right status + printer.print(offset, &self.status().0); // left status } fn required_size(&mut self, _: Vec2) -> Vec2 { diff --git a/src/habit.rs b/src/habit.rs index 48dd363..92e0b9f 100644 --- a/src/habit.rs +++ b/src/habit.rs @@ -15,6 +15,19 @@ pub enum TrackEvent { Decrement, } +#[derive(Debug, PartialEq, Serialize, Deserialize)] +pub enum ViewMode { + Day, + Month, + Year, +} + +impl std::default::Default for ViewMode { + fn default() -> Self { + ViewMode::Day + } +} + #[derive(Copy, Clone, Debug, Serialize, Deserialize)] pub struct CustomBool(bool); @@ -51,8 +64,12 @@ pub trait Habit { fn remaining(&self, date: NaiveDate) -> u32; fn total(&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 set_view_mode(&mut self, mode: ViewMode); + fn view_mode(&self) -> ViewMode; } #[typetag::serde(tag = "type")] @@ -64,9 +81,13 @@ pub trait HabitWrapper: erased_serde::Serialize { fn on_event(&mut self, event: Event) -> EventResult; fn required_size(&mut self, _: Vec2) -> Vec2; fn take_focus(&mut self, _: Direction) -> bool; + fn get_name(&self) -> String; + fn set_view_month_offset(&mut self, offset: u32); fn view_month_offset(&self) -> u32; - fn get_name(&self) -> String; + + fn set_view_mode(&mut self, mode: ViewMode); + fn view_mode(&self) -> ViewMode; } macro_rules! auto_habit_impl { @@ -97,11 +118,17 @@ macro_rules! auto_habit_impl { fn set_view_month_offset(&mut self, offset: u32) { Habit::set_view_month_offset(self, offset) } + fn get_name(&self) -> String { + Habit::name(self) + } fn view_month_offset(&self) -> u32 { Habit::view_month_offset(self) } - fn get_name(&self) -> String { - Habit::name(self) + fn set_view_mode(&mut self, mode: ViewMode) { + Habit::set_view_mode(&mut self, mode: ViewMode) + } + fn view_mode(&self) -> ViewMode { + Habit::view_mode(self) } } }; @@ -118,6 +145,9 @@ pub struct Count { #[serde(skip)] view_month_offset: u32, + + #[serde(skip)] + view_mode: ViewMode, } impl Count { @@ -127,6 +157,7 @@ impl Count { stats: HashMap::new(), goal, view_month_offset: 0, + view_mode: ViewMode::Day, }; } } @@ -193,6 +224,12 @@ impl Habit for Count { fn view_month_offset(&self) -> u32 { self.view_month_offset } + fn set_view_mode(&mut self, mode: ViewMode) { + self.view_mode = mode; + } + fn view_mode(&self) -> ViewMode { + self.view_mode + } } #[derive(Debug, Serialize, Deserialize)] @@ -203,6 +240,9 @@ pub struct Bit { #[serde(skip)] view_month_offset: u32, + + #[serde(skip)] + view_mode: ViewMode, } impl Bit { @@ -212,6 +252,7 @@ impl Bit { stats: HashMap::new(), goal: CustomBool(true), view_month_offset: 0, + view_mode: ViewMode::Day, }; } } @@ -268,4 +309,10 @@ impl Habit for Bit { fn view_month_offset(&self) -> u32 { self.view_month_offset } + fn set_view_mode(&mut self, mode: ViewMode) { + self.view_mode = mode; + } + fn view_mode(&self) -> ViewMode { + self.view_mode + } } diff --git a/src/views.rs b/src/views.rs index d25e59b..facbd55 100644 --- a/src/views.rs +++ b/src/views.rs @@ -7,7 +7,7 @@ use cursive::{Printer, Vec2}; use chrono::prelude::*; use chrono::{Duration, Local, NaiveDate}; -use crate::habit::{Bit, Count, Habit, TrackEvent}; +use crate::habit::{Bit, Count, Habit, TrackEvent, ViewMode}; use crate::CONFIGURATION; pub trait ShadowView { @@ -62,26 +62,37 @@ where }, ); - let mut i = 1; - while let Some(d) = NaiveDate::from_ymd_opt(year, month, i) { - let day_style; - if self.reached_goal(d) { - day_style = goal_reached_style; - } else { - day_style = todo_style; - } - let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); - if let Some(c) = self.get_by_date(d) { - printer.with_style(day_style, |p| { - p.print(coords, &format!("{:^3}", c)); - }); - } else { - printer.with_style(future_style, |p| { - p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr)); - }); + // fn draw_day(&self, p: &Printer) { }; + // fn draw_month(&self, p: &Printer) {}; + + // match self.view_mode() { + // ViewMode::Day => draw_day(self, p), + // ViewMode::Month => + // } + + let draw_day = |p: &Printer| { + let mut i = 1; + while let Some(d) = NaiveDate::from_ymd_opt(year, month, i) { + let day_style; + if self.reached_goal(d) { + day_style = goal_reached_style; + } else { + day_style = todo_style; + } + let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); + if let Some(c) = self.get_by_date(d) { + printer.with_style(day_style, |p| { + p.print(coords, &format!("{:^3}", c)); + }); + } else { + printer.with_style(future_style, |p| { + p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr)); + }); + } + i += 1; } - i += 1; - } + }; + draw_day(printer); } fn required_size(&mut self, _: Vec2) -> Vec2 { -- cgit v1.2.3 From fbb0c754f4029cc93033ff4aabb28a8ab9c1e7e7 Mon Sep 17 00:00:00 2001 From: Akshay Date: Sat, 11 Jul 2020 22:17:58 +0530 Subject: misc modules --- src/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index 4f91990..387dc64 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,7 +9,7 @@ mod theme; mod utils; mod views; -use crate::app::{App, ViewMode}; +use crate::app::App; use crate::command::{open_command_window, Command}; use crate::habit::{Bit, Count, Habit}; use crate::utils::{load_configuration_file, AppConfig}; -- cgit v1.2.3 From 0b18c65466a59b1c9f8d1bfbe596fc2750571dfb Mon Sep 17 00:00:00 2001 From: Akshay Date: Sun, 12 Jul 2020 09:30:29 +0530 Subject: fix trait bounds bug, prep for view modes --- src/habit.rs | 10 +++++----- src/views.rs | 47 ++++++++++++++++++++++++----------------------- 2 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/habit.rs b/src/habit.rs index 92e0b9f..5469aab 100644 --- a/src/habit.rs +++ b/src/habit.rs @@ -15,7 +15,7 @@ pub enum TrackEvent { Decrement, } -#[derive(Debug, PartialEq, Serialize, Deserialize)] +#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize)] pub enum ViewMode { Day, Month, @@ -115,17 +115,17 @@ macro_rules! auto_habit_impl { fn take_focus(&mut self, d: Direction) -> bool { ShadowView::take_focus(self, d) } - fn set_view_month_offset(&mut self, offset: u32) { - Habit::set_view_month_offset(self, offset) - } fn get_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 set_view_mode(&mut self, mode: ViewMode) { - Habit::set_view_mode(&mut self, mode: ViewMode) + Habit::set_view_mode(self, mode) } fn view_mode(&self) -> ViewMode { Habit::view_mode(self) diff --git a/src/views.rs b/src/views.rs index facbd55..3e623a9 100644 --- a/src/views.rs +++ b/src/views.rs @@ -7,7 +7,8 @@ use cursive::{Printer, Vec2}; use chrono::prelude::*; use chrono::{Duration, Local, NaiveDate}; -use crate::habit::{Bit, Count, Habit, TrackEvent, ViewMode}; +use crate::habit::{Bit, Count, Habit, TrackEvent}; + use crate::CONFIGURATION; pub trait ShadowView { @@ -70,29 +71,29 @@ where // ViewMode::Month => // } - let draw_day = |p: &Printer| { - let mut i = 1; - while let Some(d) = NaiveDate::from_ymd_opt(year, month, i) { - let day_style; - if self.reached_goal(d) { - day_style = goal_reached_style; - } else { - day_style = todo_style; - } - let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); - if let Some(c) = self.get_by_date(d) { - printer.with_style(day_style, |p| { - p.print(coords, &format!("{:^3}", c)); - }); - } else { - printer.with_style(future_style, |p| { - p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr)); - }); - } - i += 1; + //let draw_day = |printer: &Printer| { + let mut i = 1; + while let Some(d) = NaiveDate::from_ymd_opt(year, month, i) { + let day_style; + if self.reached_goal(d) { + day_style = goal_reached_style; + } else { + day_style = todo_style; } - }; - draw_day(printer); + let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); + if let Some(c) = self.get_by_date(d) { + printer.with_style(day_style, |p| { + p.print(coords, &format!("{:^3}", c)); + }); + } else { + printer.with_style(future_style, |p| { + p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr)); + }); + } + i += 1; + } + //}; + //draw_day(printer); } fn required_size(&mut self, _: Vec2) -> Vec2 { -- cgit v1.2.3