aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-07-11 17:47:45 +0100
committerAkshay <[email protected]>2020-07-11 17:47:45 +0100
commitce6db76dc3d8f2f8b43a2e8769cf17416e57a065 (patch)
tree6383763aa474040d4a0060de24871a60e7231781 /src
parent33d9ba70bee7e6271404d1db7b2724885ee5c9e8 (diff)
begin work on month mode
Diffstat (limited to 'src')
-rw-r--r--src/app.rs29
-rw-r--r--src/habit.rs53
-rw-r--r--src/views.rs51
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};
9 9
10use chrono::{Local, NaiveDate}; 10use chrono::{Local, NaiveDate};
11 11
12use crate::habit::{Bit, Count, Habit, HabitWrapper}; 12use crate::habit::{Bit, Count, Habit, HabitWrapper, ViewMode};
13use crate::utils; 13use crate::utils;
14use crate::Command; 14use crate::Command;
15use crate::CONFIGURATION; 15use crate::CONFIGURATION;
16 16
17use serde::{Deserialize, Serialize}; 17use serde::{Deserialize, Serialize};
18 18
19#[derive(PartialEq, Serialize, Deserialize)]
20pub enum ViewMode {
21 Day,
22 Month,
23 Year,
24}
25
26impl std::default::Default for ViewMode {
27 fn default() -> Self {
28 ViewMode::Month
29 }
30}
31
32struct StatusLine(String, String); 19struct StatusLine(String, String);
33 20
34#[derive(Serialize, Deserialize)] 21#[derive(Serialize, Deserialize)]
@@ -41,9 +28,6 @@ pub struct App {
41 focus: usize, 28 focus: usize,
42 29
43 #[serde(skip)] 30 #[serde(skip)]
44 view_mode: ViewMode,
45
46 #[serde(skip)]
47 view_month_offset: u32, 31 view_month_offset: u32,
48} 32}
49 33
@@ -51,7 +35,6 @@ impl App {
51 pub fn new() -> Self { 35 pub fn new() -> Self {
52 return App { 36 return App {
53 habits: vec![], 37 habits: vec![],
54 view_mode: ViewMode::Day,
55 focus: 0, 38 focus: 0,
56 view_month_offset: 0, 39 view_month_offset: 0,
57 }; 40 };
@@ -65,9 +48,9 @@ impl App {
65 self.habits.retain(|h| h.get_name() != name); 48 self.habits.retain(|h| h.get_name() != name);
66 } 49 }
67 50
68 pub fn set_mode(&mut self, set_mode: ViewMode) { 51 pub fn set_mode(&mut self, mode: ViewMode) {
69 if set_mode != self.view_mode { 52 if !self.habits.is_empty() {
70 self.view_mode = set_mode; 53 self.habits[self.focus].set_view_mode(mode);
71 } 54 }
72 } 55 }
73 56
@@ -233,8 +216,8 @@ impl View for App {
233 } 216 }
234 217
235 offset = offset.map_x(|_| 0).map_y(|_| self.max_size().y - 2); 218 offset = offset.map_x(|_| 0).map_y(|_| self.max_size().y - 2);
236 printer.print(offset, &self.status().1); // right 219 printer.print(offset, &self.status().1); // right status
237 printer.print(offset, &self.status().0); // left 220 printer.print(offset, &self.status().0); // left status
238 } 221 }
239 222
240 fn required_size(&mut self, _: Vec2) -> Vec2 { 223 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 {
15 Decrement, 15 Decrement,
16} 16}
17 17
18#[derive(Debug, PartialEq, Serialize, Deserialize)]
19pub enum ViewMode {
20 Day,
21 Month,
22 Year,
23}
24
25impl std::default::Default for ViewMode {
26 fn default() -> Self {
27 ViewMode::Day
28 }
29}
30
18#[derive(Copy, Clone, Debug, Serialize, Deserialize)] 31#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
19pub struct CustomBool(bool); 32pub struct CustomBool(bool);
20 33
@@ -51,8 +64,12 @@ pub trait Habit {
51 fn remaining(&self, date: NaiveDate) -> u32; 64 fn remaining(&self, date: NaiveDate) -> u32;
52 fn total(&self) -> u32; 65 fn total(&self) -> u32;
53 fn modify(&mut self, date: NaiveDate, event: TrackEvent); 66 fn modify(&mut self, date: NaiveDate, event: TrackEvent);
67
54 fn set_view_month_offset(&mut self, offset: u32); 68 fn set_view_month_offset(&mut self, offset: u32);
55 fn view_month_offset(&self) -> u32; 69 fn view_month_offset(&self) -> u32;
70
71 fn set_view_mode(&mut self, mode: ViewMode);
72 fn view_mode(&self) -> ViewMode;
56} 73}
57 74
58#[typetag::serde(tag = "type")] 75#[typetag::serde(tag = "type")]
@@ -64,9 +81,13 @@ pub trait HabitWrapper: erased_serde::Serialize {
64 fn on_event(&mut self, event: Event) -> EventResult; 81 fn on_event(&mut self, event: Event) -> EventResult;
65 fn required_size(&mut self, _: Vec2) -> Vec2; 82 fn required_size(&mut self, _: Vec2) -> Vec2;
66 fn take_focus(&mut self, _: Direction) -> bool; 83 fn take_focus(&mut self, _: Direction) -> bool;
84 fn get_name(&self) -> String;
85
67 fn set_view_month_offset(&mut self, offset: u32); 86 fn set_view_month_offset(&mut self, offset: u32);
68 fn view_month_offset(&self) -> u32; 87 fn view_month_offset(&self) -> u32;
69 fn get_name(&self) -> String; 88
89 fn set_view_mode(&mut self, mode: ViewMode);
90 fn view_mode(&self) -> ViewMode;
70} 91}
71 92
72macro_rules! auto_habit_impl { 93macro_rules! auto_habit_impl {
@@ -97,11 +118,17 @@ macro_rules! auto_habit_impl {
97 fn set_view_month_offset(&mut self, offset: u32) { 118 fn set_view_month_offset(&mut self, offset: u32) {
98 Habit::set_view_month_offset(self, offset) 119 Habit::set_view_month_offset(self, offset)
99 } 120 }
121 fn get_name(&self) -> String {
122 Habit::name(self)
123 }
100 fn view_month_offset(&self) -> u32 { 124 fn view_month_offset(&self) -> u32 {
101 Habit::view_month_offset(self) 125 Habit::view_month_offset(self)
102 } 126 }
103 fn get_name(&self) -> String { 127 fn set_view_mode(&mut self, mode: ViewMode) {
104 Habit::name(self) 128 Habit::set_view_mode(&mut self, mode: ViewMode)
129 }
130 fn view_mode(&self) -> ViewMode {
131 Habit::view_mode(self)
105 } 132 }
106 } 133 }
107 }; 134 };
@@ -118,6 +145,9 @@ pub struct Count {
118 145
119 #[serde(skip)] 146 #[serde(skip)]
120 view_month_offset: u32, 147 view_month_offset: u32,
148
149 #[serde(skip)]
150 view_mode: ViewMode,
121} 151}
122 152
123impl Count { 153impl Count {
@@ -127,6 +157,7 @@ impl Count {
127 stats: HashMap::new(), 157 stats: HashMap::new(),
128 goal, 158 goal,
129 view_month_offset: 0, 159 view_month_offset: 0,
160 view_mode: ViewMode::Day,
130 }; 161 };
131 } 162 }
132} 163}
@@ -193,6 +224,12 @@ impl Habit for Count {
193 fn view_month_offset(&self) -> u32 { 224 fn view_month_offset(&self) -> u32 {
194 self.view_month_offset 225 self.view_month_offset
195 } 226 }
227 fn set_view_mode(&mut self, mode: ViewMode) {
228 self.view_mode = mode;
229 }
230 fn view_mode(&self) -> ViewMode {
231 self.view_mode
232 }
196} 233}
197 234
198#[derive(Debug, Serialize, Deserialize)] 235#[derive(Debug, Serialize, Deserialize)]
@@ -203,6 +240,9 @@ pub struct Bit {
203 240
204 #[serde(skip)] 241 #[serde(skip)]
205 view_month_offset: u32, 242 view_month_offset: u32,
243
244 #[serde(skip)]
245 view_mode: ViewMode,
206} 246}
207 247
208impl Bit { 248impl Bit {
@@ -212,6 +252,7 @@ impl Bit {
212 stats: HashMap::new(), 252 stats: HashMap::new(),
213 goal: CustomBool(true), 253 goal: CustomBool(true),
214 view_month_offset: 0, 254 view_month_offset: 0,
255 view_mode: ViewMode::Day,
215 }; 256 };
216 } 257 }
217} 258}
@@ -268,4 +309,10 @@ impl Habit for Bit {
268 fn view_month_offset(&self) -> u32 { 309 fn view_month_offset(&self) -> u32 {
269 self.view_month_offset 310 self.view_month_offset
270 } 311 }
312 fn set_view_mode(&mut self, mode: ViewMode) {
313 self.view_mode = mode;
314 }
315 fn view_mode(&self) -> ViewMode {
316 self.view_mode
317 }
271} 318}
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};
7use chrono::prelude::*; 7use chrono::prelude::*;
8use chrono::{Duration, Local, NaiveDate}; 8use chrono::{Duration, Local, NaiveDate};
9 9
10use crate::habit::{Bit, Count, Habit, TrackEvent}; 10use crate::habit::{Bit, Count, Habit, TrackEvent, ViewMode};
11use crate::CONFIGURATION; 11use crate::CONFIGURATION;
12 12
13pub trait ShadowView { 13pub trait ShadowView {
@@ -62,26 +62,37 @@ where
62 }, 62 },
63 ); 63 );
64 64
65 let mut i = 1; 65 // fn draw_day(&self, p: &Printer) { };
66 while let Some(d) = NaiveDate::from_ymd_opt(year, month, i) { 66 // fn draw_month(&self, p: &Printer) {};
67 let day_style; 67
68 if self.reached_goal(d) { 68 // match self.view_mode() {
69 day_style = goal_reached_style; 69 // ViewMode::Day => draw_day(self, p),
70 } else { 70 // ViewMode::Month =>
71 day_style = todo_style; 71 // }
72 } 72
73 let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into(); 73 let draw_day = |p: &Printer| {
74 if let Some(c) = self.get_by_date(d) { 74 let mut i = 1;
75 printer.with_style(day_style, |p| { 75 while let Some(d) = NaiveDate::from_ymd_opt(year, month, i) {
76 p.print(coords, &format!("{:^3}", c)); 76 let day_style;
77 }); 77 if self.reached_goal(d) {
78 } else { 78 day_style = goal_reached_style;
79 printer.with_style(future_style, |p| { 79 } else {
80 p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr)); 80 day_style = todo_style;
81 }); 81 }
82 let coords: Vec2 = ((i % 7) * 3, i / 7 + 2).into();
83 if let Some(c) = self.get_by_date(d) {
84 printer.with_style(day_style, |p| {
85 p.print(coords, &format!("{:^3}", c));
86 });
87 } else {
88 printer.with_style(future_style, |p| {
89 p.print(coords, &format!("{:^3}", CONFIGURATION.future_chr));
90 });
91 }
92 i += 1;
82 } 93 }
83 i += 1; 94 };
84 } 95 draw_day(printer);
85 } 96 }
86 97
87 fn required_size(&mut self, _: Vec2) -> Vec2 { 98 fn required_size(&mut self, _: Vec2) -> Vec2 {