diff options
author | Akshay <[email protected]> | 2020-07-10 18:05:30 +0100 |
---|---|---|
committer | Akshay <[email protected]> | 2020-07-10 18:05:30 +0100 |
commit | b6ea375485083860094a904b7a6c7d97fa42b8f8 (patch) | |
tree | 52dba4e164e23b2fba4a8764c145d6d734ed7fab | |
parent | 872297132d9d1fa39545fee54a1a25a95bdbe22d (diff) |
follow XDG_DATA_DIR spec for app data
-rw-r--r-- | src/app.rs | 38 | ||||
-rw-r--r-- | src/utils.rs | 12 |
2 files changed, 37 insertions, 13 deletions
@@ -1,5 +1,5 @@ | |||
1 | use std::f64; | 1 | use std::f64; |
2 | use std::fs::File; | 2 | use std::fs::{File, OpenOptions}; |
3 | use std::io::prelude::*; | 3 | use std::io::prelude::*; |
4 | 4 | ||
5 | use cursive::direction::{Absolute, Direction}; | 5 | use cursive::direction::{Absolute, Direction}; |
@@ -10,6 +10,7 @@ use cursive::{Printer, Vec2}; | |||
10 | use chrono::{Local, NaiveDate}; | 10 | use chrono::{Local, NaiveDate}; |
11 | 11 | ||
12 | use crate::habit::{Bit, Count, Habit, HabitWrapper}; | 12 | use crate::habit::{Bit, Count, Habit, HabitWrapper}; |
13 | use crate::utils; | ||
13 | use crate::Command; | 14 | use crate::Command; |
14 | use crate::CONFIGURATION; | 15 | use crate::CONFIGURATION; |
15 | 16 | ||
@@ -173,10 +174,26 @@ impl App { | |||
173 | } | 174 | } |
174 | 175 | ||
175 | pub fn load_state() -> Self { | 176 | pub fn load_state() -> Self { |
176 | let mut file = File::open("foo.txt").unwrap(); | 177 | let data_file = utils::data_file(); |
177 | let mut j = String::new(); | 178 | if let Ok(ref mut file) = File::open(data_file) { |
178 | file.read_to_string(&mut j); | 179 | let mut j = String::new(); |
179 | return serde_json::from_str(&j).unwrap(); | 180 | file.read_to_string(&mut j); |
181 | return serde_json::from_str(&j).unwrap(); | ||
182 | } else { | ||
183 | Self::new() | ||
184 | } | ||
185 | } | ||
186 | |||
187 | // this function does IO | ||
188 | // TODO: convert this into non-blocking async function | ||
189 | fn save_state(&self) { | ||
190 | let j = serde_json::to_string_pretty(&self).unwrap(); | ||
191 | let data_file = utils::data_file(); | ||
192 | |||
193 | match OpenOptions::new().write(true).create(true).open(data_file) { | ||
194 | Ok(ref mut file) => file.write_all(j.as_bytes()).unwrap(), | ||
195 | Err(_) => panic!("Unable to write!"), | ||
196 | }; | ||
180 | } | 197 | } |
181 | 198 | ||
182 | pub fn parse_command(&mut self, input: &str) { | 199 | pub fn parse_command(&mut self, input: &str) { |
@@ -201,14 +218,6 @@ impl App { | |||
201 | } | 218 | } |
202 | } | 219 | } |
203 | } | 220 | } |
204 | |||
205 | // this function does IO | ||
206 | // TODO: convert this into non-blocking async function | ||
207 | fn save_state(&self) { | ||
208 | let j = serde_json::to_string_pretty(&self).unwrap(); | ||
209 | let mut file = File::create("foo.txt").unwrap(); | ||
210 | file.write_all(j.as_bytes()).unwrap(); | ||
211 | } | ||
212 | } | 221 | } |
213 | 222 | ||
214 | impl View for App { | 223 | impl View for App { |
@@ -313,6 +322,9 @@ impl View for App { | |||
313 | * before performing any action, "refocusing" the cursor | 322 | * before performing any action, "refocusing" the cursor |
314 | * */ | 323 | * */ |
315 | _ => { | 324 | _ => { |
325 | if self.habits.is_empty() { | ||
326 | return EventResult::Ignored; | ||
327 | } | ||
316 | self.set_view_month_offset(0); | 328 | self.set_view_month_offset(0); |
317 | self.habits[self.focus].on_event(e) | 329 | self.habits[self.focus].on_event(e) |
318 | } | 330 | } |
diff --git a/src/utils.rs b/src/utils.rs index 55900b0..ab7e7ef 100644 --- a/src/utils.rs +++ b/src/utils.rs | |||
@@ -1,4 +1,6 @@ | |||
1 | use cursive::theme::{BaseColor, Color}; | 1 | use cursive::theme::{BaseColor, Color}; |
2 | use directories::ProjectDirs; | ||
3 | use std::path::{Path, PathBuf}; | ||
2 | 4 | ||
3 | pub struct AppConfig { | 5 | pub struct AppConfig { |
4 | pub true_chr: char, | 6 | pub true_chr: char, |
@@ -30,3 +32,13 @@ pub fn load_configuration_file() -> AppConfig { | |||
30 | future_color: Color::Light(BaseColor::Black), | 32 | future_color: Color::Light(BaseColor::Black), |
31 | }; | 33 | }; |
32 | } | 34 | } |
35 | |||
36 | pub fn data_file() -> PathBuf { | ||
37 | if let Some(proj_dirs) = ProjectDirs::from("rs", "nerdypepper", "dijo") { | ||
38 | let mut data_file = PathBuf::from(proj_dirs.data_dir()); | ||
39 | data_file.push("habit_record.json"); | ||
40 | return data_file; | ||
41 | } else { | ||
42 | panic!("Invalid home directory!") | ||
43 | }; | ||
44 | } | ||