diff options
author | Akshay <[email protected]> | 2020-07-12 16:39:35 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-07-12 16:39:35 +0100 |
commit | 68ebbd1f753e0937bfaf07006daf1d6144c21bed (patch) | |
tree | f3c195cdbd6901cef1a5d848d9b6e84c7c51c835 /src/app.rs | |
parent | 2b3ba619402a9d6a5408d35bb38ef31f7e488ec5 (diff) |
prep for autohabits, impl Default for App, bugfix file writing
Diffstat (limited to 'src/app.rs')
-rw-r--r-- | src/app.rs | 31 |
1 files changed, 20 insertions, 11 deletions
@@ -1,3 +1,4 @@ | |||
1 | use std::default::Default; | ||
1 | use std::f64; | 2 | use std::f64; |
2 | use std::fs::{File, OpenOptions}; | 3 | use std::fs::{File, OpenOptions}; |
3 | use std::io::prelude::*; | 4 | use std::io::prelude::*; |
@@ -14,23 +15,22 @@ use crate::utils; | |||
14 | use crate::Command; | 15 | use crate::Command; |
15 | use crate::CONFIGURATION; | 16 | use crate::CONFIGURATION; |
16 | 17 | ||
17 | use serde::{Deserialize, Serialize}; | ||
18 | |||
19 | struct StatusLine(String, String); | 18 | struct StatusLine(String, String); |
20 | 19 | ||
21 | #[derive(Serialize, Deserialize)] | ||
22 | pub struct App { | 20 | pub struct App { |
23 | // holds app data | 21 | // holds app data |
24 | habits: Vec<Box<dyn HabitWrapper>>, | 22 | habits: Vec<Box<dyn HabitWrapper>>, |
25 | 23 | ||
26 | // serialization of the rest can be skipped | ||
27 | #[serde(skip)] | ||
28 | focus: usize, | 24 | focus: usize, |
29 | |||
30 | #[serde(skip)] | ||
31 | view_month_offset: u32, | 25 | view_month_offset: u32, |
32 | } | 26 | } |
33 | 27 | ||
28 | impl Default for App { | ||
29 | fn default() -> Self { | ||
30 | App::new() | ||
31 | } | ||
32 | } | ||
33 | |||
34 | impl App { | 34 | impl App { |
35 | pub fn new() -> Self { | 35 | pub fn new() -> Self { |
36 | return App { | 36 | return App { |
@@ -161,7 +161,10 @@ impl App { | |||
161 | if let Ok(ref mut file) = File::open(data_file) { | 161 | if let Ok(ref mut file) = File::open(data_file) { |
162 | let mut j = String::new(); | 162 | let mut j = String::new(); |
163 | file.read_to_string(&mut j); | 163 | file.read_to_string(&mut j); |
164 | return serde_json::from_str(&j).unwrap(); | 164 | return App { |
165 | habits: serde_json::from_str(&j).unwrap(), | ||
166 | ..Default::default() | ||
167 | }; | ||
165 | } else { | 168 | } else { |
166 | Self::new() | 169 | Self::new() |
167 | } | 170 | } |
@@ -170,10 +173,15 @@ impl App { | |||
170 | // this function does IO | 173 | // this function does IO |
171 | // TODO: convert this into non-blocking async function | 174 | // TODO: convert this into non-blocking async function |
172 | fn save_state(&self) { | 175 | fn save_state(&self) { |
173 | let j = serde_json::to_string_pretty(&self).unwrap(); | 176 | let j = serde_json::to_string_pretty(&self.habits).unwrap(); |
174 | let data_file = utils::data_file(); | 177 | let data_file = utils::data_file(); |
175 | 178 | ||
176 | match OpenOptions::new().write(true).create(true).open(data_file) { | 179 | match OpenOptions::new() |
180 | .write(true) | ||
181 | .create(true) | ||
182 | .truncate(true) | ||
183 | .open(data_file) | ||
184 | { | ||
177 | Ok(ref mut file) => file.write_all(j.as_bytes()).unwrap(), | 185 | Ok(ref mut file) => file.write_all(j.as_bytes()).unwrap(), |
178 | Err(_) => panic!("Unable to write!"), | 186 | Err(_) => panic!("Unable to write!"), |
179 | }; | 187 | }; |
@@ -274,7 +282,8 @@ impl View for App { | |||
274 | return EventResult::Consumed(None); | 282 | return EventResult::Consumed(None); |
275 | } | 283 | } |
276 | Event::Char('w') => { | 284 | Event::Char('w') => { |
277 | let j = serde_json::to_string_pretty(&self).unwrap(); | 285 | // helper bind to test write to file |
286 | let j = serde_json::to_string_pretty(&self.habits).unwrap(); | ||
278 | let mut file = File::create("foo.txt").unwrap(); | 287 | let mut file = File::create("foo.txt").unwrap(); |
279 | file.write_all(j.as_bytes()).unwrap(); | 288 | file.write_all(j.as_bytes()).unwrap(); |
280 | return EventResult::Consumed(None); | 289 | return EventResult::Consumed(None); |