diff options
author | Akshay <[email protected]> | 2020-07-15 16:09:46 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-07-15 16:09:46 +0100 |
commit | 859ac5d3e49bc9a123df3f5a74b43d2281a3bed1 (patch) | |
tree | 7f5f49ec0b75bff5917b093fefc6e34746909eaf /src | |
parent | 99d2a835b4f032de78d9cdbe27521363a25204bd (diff) |
add utils for auto habits
auto trackable habits can be added, but not tracked as of this commit
Diffstat (limited to 'src')
-rw-r--r-- | src/app.rs | 58 | ||||
-rw-r--r-- | src/utils.rs | 28 |
2 files changed, 55 insertions, 31 deletions
@@ -2,6 +2,7 @@ use std::default::Default; | |||
2 | use std::f64; | 2 | use std::f64; |
3 | use std::fs::{File, OpenOptions}; | 3 | use std::fs::{File, OpenOptions}; |
4 | use std::io::prelude::*; | 4 | use std::io::prelude::*; |
5 | use std::path::PathBuf; | ||
5 | 6 | ||
6 | use cursive::direction::{Absolute, Direction}; | 7 | use cursive::direction::{Absolute, Direction}; |
7 | use cursive::event::{Event, EventResult, Key}; | 8 | use cursive::event::{Event, EventResult, Key}; |
@@ -161,34 +162,47 @@ impl App { | |||
161 | } | 162 | } |
162 | 163 | ||
163 | pub fn load_state() -> Self { | 164 | pub fn load_state() -> Self { |
164 | let data_file = utils::data_file(); | 165 | let (regular_f, auto_f) = (utils::habit_file(), utils::auto_habit_file()); |
165 | if let Ok(ref mut file) = File::open(data_file) { | 166 | let read_from_file = |file: PathBuf| -> Vec<Box<dyn HabitWrapper>> { |
166 | let mut j = String::new(); | 167 | if let Ok(ref mut f) = File::open(file) { |
167 | file.read_to_string(&mut j); | 168 | let mut j = String::new(); |
168 | return App { | 169 | f.read_to_string(&mut j); |
169 | habits: serde_json::from_str(&j).unwrap(), | 170 | return serde_json::from_str(&j).unwrap(); |
170 | ..Default::default() | 171 | } else { |
171 | }; | 172 | return Vec::new(); |
172 | } else { | 173 | } |
173 | Self::new() | 174 | }; |
174 | } | 175 | |
176 | let mut regular = read_from_file(regular_f); | ||
177 | let auto = read_from_file(auto_f); | ||
178 | regular.extend(auto); | ||
179 | return App { | ||
180 | habits: regular, | ||
181 | ..Default::default() | ||
182 | }; | ||
175 | } | 183 | } |
176 | 184 | ||
177 | // this function does IO | 185 | // this function does IO |
178 | // TODO: convert this into non-blocking async function | 186 | // TODO: convert this into non-blocking async function |
179 | fn save_state(&self) { | 187 | fn save_state(&self) { |
180 | let j = serde_json::to_string_pretty(&self.habits).unwrap(); | 188 | let (regular, auto): (Vec<_>, Vec<_>) = self.habits.iter().partition(|&x| !x.is_auto()); |
181 | let data_file = utils::data_file(); | 189 | let (regular_f, auto_f) = (utils::habit_file(), utils::auto_habit_file()); |
182 | 190 | ||
183 | match OpenOptions::new() | 191 | let write_to_file = |data: Vec<&Box<dyn HabitWrapper>>, file: PathBuf| { |
184 | .write(true) | 192 | let j = serde_json::to_string_pretty(&data).unwrap(); |
185 | .create(true) | 193 | match OpenOptions::new() |
186 | .truncate(true) | 194 | .write(true) |
187 | .open(data_file) | 195 | .create(true) |
188 | { | 196 | .truncate(true) |
189 | Ok(ref mut file) => file.write_all(j.as_bytes()).unwrap(), | 197 | .open(file) |
190 | Err(_) => panic!("Unable to write!"), | 198 | { |
199 | Ok(ref mut f) => f.write_all(j.as_bytes()).unwrap(), | ||
200 | Err(_) => panic!("Unable to write!"), | ||
201 | }; | ||
191 | }; | 202 | }; |
203 | |||
204 | write_to_file(regular, regular_f); | ||
205 | write_to_file(auto, auto_f); | ||
192 | } | 206 | } |
193 | 207 | ||
194 | pub fn parse_command(&mut self, input: &str) { | 208 | pub fn parse_command(&mut self, input: &str) { |
diff --git a/src/utils.rs b/src/utils.rs index 1d56377..e6ec6ac 100644 --- a/src/utils.rs +++ b/src/utils.rs | |||
@@ -34,13 +34,23 @@ pub fn load_configuration_file() -> AppConfig { | |||
34 | }; | 34 | }; |
35 | } | 35 | } |
36 | 36 | ||
37 | pub fn data_file() -> PathBuf { | 37 | fn project_dirs() -> ProjectDirs { |
38 | if let Some(proj_dirs) = ProjectDirs::from("rs", "nerdypepper", "dijo") { | 38 | ProjectDirs::from("rs", "nerdypepper", "dijo") |
39 | let mut data_file = PathBuf::from(proj_dirs.data_dir()); | 39 | .unwrap_or_else(|| panic!("Invalid home directory!")) |
40 | fs::create_dir_all(&data_file); | 40 | } |
41 | data_file.push("habit_record.json"); | 41 | |
42 | return data_file; | 42 | pub fn habit_file() -> PathBuf { |
43 | } else { | 43 | let proj_dirs = project_dirs(); |
44 | panic!("Invalid home directory!") | 44 | let mut data_file = PathBuf::from(proj_dirs.data_dir()); |
45 | }; | 45 | fs::create_dir_all(&data_file); |
46 | data_file.push("habit_record.json"); | ||
47 | return data_file; | ||
48 | } | ||
49 | |||
50 | pub fn auto_habit_file() -> PathBuf { | ||
51 | let proj_dirs = project_dirs(); | ||
52 | let mut data_file = PathBuf::from(proj_dirs.data_dir()); | ||
53 | fs::create_dir_all(&data_file); | ||
54 | data_file.push("habit_record[auto].json"); | ||
55 | return data_file; | ||
46 | } | 56 | } |