summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAkshay <nerdy@peppe.rs>2020-07-10 22:35:30 +0530
committerAkshay <nerdy@peppe.rs>2020-07-10 22:35:30 +0530
commitb6ea375485083860094a904b7a6c7d97fa42b8f8 (patch)
tree52dba4e164e23b2fba4a8764c145d6d734ed7fab
parent872297132d9d1fa39545fee54a1a25a95bdbe22d (diff)
follow XDG_DATA_DIR spec for app data
-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 @@
use std::f64;
-use std::fs::File;
+use std::fs::{File, OpenOptions};
use std::io::prelude::*;
use cursive::direction::{Absolute, Direction};
@@ -10,6 +10,7 @@ use cursive::{Printer, Vec2};
use chrono::{Local, NaiveDate};
use crate::habit::{Bit, Count, Habit, HabitWrapper};
+use crate::utils;
use crate::Command;
use crate::CONFIGURATION;
@@ -173,10 +174,26 @@ impl App {
}
pub fn load_state() -> Self {
- let mut file = File::open("foo.txt").unwrap();
- let mut j = String::new();
- file.read_to_string(&mut j);
- return serde_json::from_str(&j).unwrap();
+ let data_file = utils::data_file();
+ if let Ok(ref mut file) = File::open(data_file) {
+ let mut j = String::new();
+ file.read_to_string(&mut j);
+ return serde_json::from_str(&j).unwrap();
+ } else {
+ Self::new()
+ }
+ }
+
+ // this function does IO
+ // TODO: convert this into non-blocking async function
+ fn save_state(&self) {
+ let j = serde_json::to_string_pretty(&self).unwrap();
+ let data_file = utils::data_file();
+
+ match OpenOptions::new().write(true).create(true).open(data_file) {
+ Ok(ref mut file) => file.write_all(j.as_bytes()).unwrap(),
+ Err(_) => panic!("Unable to write!"),
+ };
}
pub fn parse_command(&mut self, input: &str) {
@@ -201,14 +218,6 @@ impl App {
}
}
}
-
- // this function does IO
- // TODO: convert this into non-blocking async function
- fn save_state(&self) {
- let j = serde_json::to_string_pretty(&self).unwrap();
- let mut file = File::create("foo.txt").unwrap();
- file.write_all(j.as_bytes()).unwrap();
- }
}
impl View for App {
@@ -313,6 +322,9 @@ impl View for App {
* before performing any action, "refocusing" the cursor
* */
_ => {
+ if self.habits.is_empty() {
+ return EventResult::Ignored;
+ }
self.set_view_month_offset(0);
self.habits[self.focus].on_event(e)
}
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 @@
use cursive::theme::{BaseColor, Color};
+use directories::ProjectDirs;
+use std::path::{Path, PathBuf};
pub struct AppConfig {
pub true_chr: char,
@@ -30,3 +32,13 @@ pub fn load_configuration_file() -> AppConfig {
future_color: Color::Light(BaseColor::Black),
};
}
+
+pub fn data_file() -> PathBuf {
+ if let Some(proj_dirs) = ProjectDirs::from("rs", "nerdypepper", "dijo") {
+ let mut data_file = PathBuf::from(proj_dirs.data_dir());
+ data_file.push("habit_record.json");
+ return data_file;
+ } else {
+ panic!("Invalid home directory!")
+ };
+}