diff options
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | src/app.rs | 53 |
2 files changed, 47 insertions, 7 deletions
@@ -10,6 +10,7 @@ edition = "2018" | |||
10 | cursive = "0.14" | 10 | cursive = "0.14" |
11 | serde_json = "1.0" | 11 | serde_json = "1.0" |
12 | lazy_static = "1.4.0" | 12 | lazy_static = "1.4.0" |
13 | erased-serde = "0.3" | ||
13 | 14 | ||
14 | [dependencies.chrono] | 15 | [dependencies.chrono] |
15 | version = "0.4" | 16 | version = "0.4" |
@@ -1,24 +1,33 @@ | |||
1 | use std::f64; | 1 | use std::f64; |
2 | use std::fs::File; | ||
3 | use std::io::prelude::*; | ||
2 | 4 | ||
3 | use cursive::direction::{Absolute, Direction}; | 5 | use cursive::direction::{Absolute, Direction}; |
4 | use cursive::event::{Event, EventResult, Key}; | 6 | use cursive::event::{Event, EventResult, Key}; |
5 | use cursive::view::View; | 7 | use cursive::view::View; |
6 | use cursive::{Printer, Vec2}; | 8 | use cursive::{Printer, Vec2}; |
7 | 9 | ||
10 | use chrono::NaiveDate; | ||
11 | |||
8 | use crate::habit::{Bit, Count, Habit, HabitWrapper}; | 12 | use crate::habit::{Bit, Count, Habit, HabitWrapper}; |
9 | use crate::CONFIGURATION; | 13 | use crate::CONFIGURATION; |
10 | 14 | ||
11 | #[derive(PartialEq)] | 15 | use serde::Serialize; |
16 | |||
17 | #[derive(PartialEq, Serialize)] | ||
12 | pub enum ViewMode { | 18 | pub enum ViewMode { |
13 | Day, | 19 | Day, |
14 | Month, | 20 | Month, |
15 | Year, | 21 | Year, |
16 | } | 22 | } |
17 | 23 | ||
24 | #[derive(Serialize)] | ||
18 | pub struct App { | 25 | pub struct App { |
19 | habits: Vec<Box<dyn HabitWrapper>>, | 26 | habits: Vec<Box<dyn HabitWrapper>>, |
20 | view_mode: ViewMode, | ||
21 | focus: usize, | 27 | focus: usize, |
28 | |||
29 | #[serde(skip)] | ||
30 | view_mode: ViewMode, | ||
22 | } | 31 | } |
23 | 32 | ||
24 | impl App { | 33 | impl App { |
@@ -30,16 +39,14 @@ impl App { | |||
30 | }; | 39 | }; |
31 | } | 40 | } |
32 | 41 | ||
33 | pub fn add_habit(mut self, h: Box<dyn HabitWrapper>) -> Self { | 42 | pub fn add_habit(&mut self, h: Box<dyn HabitWrapper>) { |
34 | self.habits.push(h); | 43 | self.habits.push(h); |
35 | return self; | ||
36 | } | 44 | } |
37 | 45 | ||
38 | pub fn set_mode(mut self, set_mode: ViewMode) -> Self { | 46 | pub fn set_mode(&mut self, set_mode: ViewMode) { |
39 | if set_mode != self.view_mode { | 47 | if set_mode != self.view_mode { |
40 | self.view_mode = set_mode | 48 | self.view_mode = set_mode; |
41 | } | 49 | } |
42 | return self; | ||
43 | } | 50 | } |
44 | 51 | ||
45 | fn set_focus(&mut self, d: Absolute) { | 52 | fn set_focus(&mut self, d: Absolute) { |
@@ -81,6 +88,7 @@ impl App { | |||
81 | 88 | ||
82 | return format!("{} completed, {} remaining", completed, remaining); | 89 | return format!("{} completed, {} remaining", completed, remaining); |
83 | } | 90 | } |
91 | |||
84 | fn max_size(&self) -> Vec2 { | 92 | fn max_size(&self) -> Vec2 { |
85 | let grid_width = CONFIGURATION.grid_width; | 93 | let grid_width = CONFIGURATION.grid_width; |
86 | let width = { | 94 | let width = { |
@@ -101,6 +109,10 @@ impl App { | |||
101 | }; | 109 | }; |
102 | Vec2::new(width, height) | 110 | Vec2::new(width, height) |
103 | } | 111 | } |
112 | |||
113 | // this function does IO | ||
114 | // TODO: convert this into non-blocking async function | ||
115 | fn save_state(&self) {} | ||
104 | } | 116 | } |
105 | 117 | ||
106 | impl View for App { | 118 | impl View for App { |
@@ -161,6 +173,33 @@ impl View for App { | |||
161 | self.set_focus(Absolute::Down); | 173 | self.set_focus(Absolute::Down); |
162 | return EventResult::Consumed(None); | 174 | return EventResult::Consumed(None); |
163 | } | 175 | } |
176 | Event::Char('a') => { | ||
177 | let mut gymming = Count::new("gym", 5); | ||
178 | gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 11), 7); | ||
179 | gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 12), 8); | ||
180 | gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 13), 9); | ||
181 | gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 14), 10); | ||
182 | gymming.insert_entry(NaiveDate::from_ymd(2020, 3, 15), 11); | ||
183 | self.add_habit(Box::new(gymming)); | ||
184 | return EventResult::Consumed(None); | ||
185 | } | ||
186 | Event::Char('d') => { | ||
187 | self.habits.remove(self.focus); | ||
188 | return EventResult::Consumed(None); | ||
189 | } | ||
190 | Event::Char('w') => { | ||
191 | let j = serde_json::to_string_pretty(&self).unwrap(); | ||
192 | let mut file = File::create("foo.txt").unwrap(); | ||
193 | file.write_all(j.as_bytes()).unwrap(); | ||
194 | return EventResult::Consumed(None); | ||
195 | } | ||
196 | Event::Char('q') => { | ||
197 | let j = serde_json::to_string_pretty(&self).unwrap(); | ||
198 | let mut file = File::create("foo.txt").unwrap(); | ||
199 | file.write_all(j.as_bytes()).unwrap(); | ||
200 | |||
201 | return EventResult::with_cb(|s| s.quit()); | ||
202 | } | ||
164 | _ => self.habits[self.focus].on_event(e), | 203 | _ => self.habits[self.focus].on_event(e), |
165 | } | 204 | } |
166 | } | 205 | } |