aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-07-12 05:01:53 +0100
committerAkshay <[email protected]>2020-07-12 05:01:53 +0100
commit968c1a48a0febdb78ab85a34f0dafd16a3761d5a (patch)
treef105eb93bbbfa86d8af913e2922163946a2d2d3e /src
parent28f72d4949da6093ef2232d3ba708ac0fcafe900 (diff)
parent0b18c65466a59b1c9f8d1bfbe596fc2750571dfb (diff)
Merge branch 'master' into dependabot/add-v2-config-file
Diffstat (limited to 'src')
-rw-r--r--src/app.rs29
-rw-r--r--src/habit.rs53
-rw-r--r--src/main.rs2
-rw-r--r--src/views.rs12
4 files changed, 69 insertions, 27 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..5469aab 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, Clone, Copy, 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 {
@@ -94,14 +115,20 @@ macro_rules! auto_habit_impl {
94 fn take_focus(&mut self, d: Direction) -> bool { 115 fn take_focus(&mut self, d: Direction) -> bool {
95 ShadowView::take_focus(self, d) 116 ShadowView::take_focus(self, d)
96 } 117 }
118 fn get_name(&self) -> String {
119 Habit::name(self)
120 }
97 fn set_view_month_offset(&mut self, offset: u32) { 121 fn set_view_month_offset(&mut self, offset: u32) {
98 Habit::set_view_month_offset(self, offset) 122 Habit::set_view_month_offset(self, offset)
99 } 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(self, mode)
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/main.rs b/src/main.rs
index 4f91990..387dc64 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -9,7 +9,7 @@ mod theme;
9mod utils; 9mod utils;
10mod views; 10mod views;
11 11
12use crate::app::{App, ViewMode}; 12use crate::app::App;
13use crate::command::{open_command_window, Command}; 13use crate::command::{open_command_window, Command};
14use crate::habit::{Bit, Count, Habit}; 14use crate::habit::{Bit, Count, Habit};
15use crate::utils::{load_configuration_file, AppConfig}; 15use crate::utils::{load_configuration_file, AppConfig};
diff --git a/src/views.rs b/src/views.rs
index d25e59b..3e623a9 100644
--- a/src/views.rs
+++ b/src/views.rs
@@ -8,6 +8,7 @@ use 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};
11
11use crate::CONFIGURATION; 12use crate::CONFIGURATION;
12 13
13pub trait ShadowView { 14pub trait ShadowView {
@@ -62,6 +63,15 @@ where
62 }, 63 },
63 ); 64 );
64 65
66 // fn draw_day(&self, p: &Printer) { };
67 // fn draw_month(&self, p: &Printer) {};
68
69 // match self.view_mode() {
70 // ViewMode::Day => draw_day(self, p),
71 // ViewMode::Month =>
72 // }
73
74 //let draw_day = |printer: &Printer| {
65 let mut i = 1; 75 let mut i = 1;
66 while let Some(d) = NaiveDate::from_ymd_opt(year, month, i) { 76 while let Some(d) = NaiveDate::from_ymd_opt(year, month, i) {
67 let day_style; 77 let day_style;
@@ -82,6 +92,8 @@ where
82 } 92 }
83 i += 1; 93 i += 1;
84 } 94 }
95 //};
96 //draw_day(printer);
85 } 97 }
86 98
87 fn required_size(&mut self, _: Vec2) -> Vec2 { 99 fn required_size(&mut self, _: Vec2) -> Vec2 {