aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/app.rs38
-rw-r--r--src/utils.rs12
2 files changed, 37 insertions, 13 deletions
diff --git a/src/app.rs b/src/app.rs
index e322091..82096e1 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -1,5 +1,5 @@
1use std::f64; 1use std::f64;
2use std::fs::File; 2use std::fs::{File, OpenOptions};
3use std::io::prelude::*; 3use std::io::prelude::*;
4 4
5use cursive::direction::{Absolute, Direction}; 5use cursive::direction::{Absolute, Direction};
@@ -10,6 +10,7 @@ use cursive::{Printer, Vec2};
10use chrono::{Local, NaiveDate}; 10use chrono::{Local, NaiveDate};
11 11
12use crate::habit::{Bit, Count, Habit, HabitWrapper}; 12use crate::habit::{Bit, Count, Habit, HabitWrapper};
13use crate::utils;
13use crate::Command; 14use crate::Command;
14use crate::CONFIGURATION; 15use 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
214impl View for App { 223impl 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 @@
1use cursive::theme::{BaseColor, Color}; 1use cursive::theme::{BaseColor, Color};
2use directories::ProjectDirs;
3use std::path::{Path, PathBuf};
2 4
3pub struct AppConfig { 5pub 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
36pub 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}