From 859ac5d3e49bc9a123df3f5a74b43d2281a3bed1 Mon Sep 17 00:00:00 2001 From: Akshay Date: Wed, 15 Jul 2020 20:39:46 +0530 Subject: add utils for auto habits auto trackable habits can be added, but not tracked as of this commit --- src/app.rs | 58 ++++++++++++++++++++++++++++++++++++---------------------- src/utils.rs | 28 +++++++++++++++++++--------- 2 files changed, 55 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/app.rs b/src/app.rs index 2d83714..81a574a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -2,6 +2,7 @@ use std::default::Default; use std::f64; use std::fs::{File, OpenOptions}; use std::io::prelude::*; +use std::path::PathBuf; use cursive::direction::{Absolute, Direction}; use cursive::event::{Event, EventResult, Key}; @@ -161,34 +162,47 @@ impl App { } pub fn load_state() -> Self { - 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 App { - habits: serde_json::from_str(&j).unwrap(), - ..Default::default() - }; - } else { - Self::new() - } + let (regular_f, auto_f) = (utils::habit_file(), utils::auto_habit_file()); + let read_from_file = |file: PathBuf| -> Vec> { + if let Ok(ref mut f) = File::open(file) { + let mut j = String::new(); + f.read_to_string(&mut j); + return serde_json::from_str(&j).unwrap(); + } else { + return Vec::new(); + } + }; + + let mut regular = read_from_file(regular_f); + let auto = read_from_file(auto_f); + regular.extend(auto); + return App { + habits: regular, + ..Default::default() + }; } // this function does IO // TODO: convert this into non-blocking async function fn save_state(&self) { - let j = serde_json::to_string_pretty(&self.habits).unwrap(); - let data_file = utils::data_file(); - - match OpenOptions::new() - .write(true) - .create(true) - .truncate(true) - .open(data_file) - { - Ok(ref mut file) => file.write_all(j.as_bytes()).unwrap(), - Err(_) => panic!("Unable to write!"), + let (regular, auto): (Vec<_>, Vec<_>) = self.habits.iter().partition(|&x| !x.is_auto()); + let (regular_f, auto_f) = (utils::habit_file(), utils::auto_habit_file()); + + let write_to_file = |data: Vec<&Box>, file: PathBuf| { + let j = serde_json::to_string_pretty(&data).unwrap(); + match OpenOptions::new() + .write(true) + .create(true) + .truncate(true) + .open(file) + { + Ok(ref mut f) => f.write_all(j.as_bytes()).unwrap(), + Err(_) => panic!("Unable to write!"), + }; }; + + write_to_file(regular, regular_f); + write_to_file(auto, auto_f); } 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 { }; } -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()); - fs::create_dir_all(&data_file); - data_file.push("habit_record.json"); - return data_file; - } else { - panic!("Invalid home directory!") - }; +fn project_dirs() -> ProjectDirs { + ProjectDirs::from("rs", "nerdypepper", "dijo") + .unwrap_or_else(|| panic!("Invalid home directory!")) +} + +pub fn habit_file() -> PathBuf { + let proj_dirs = project_dirs(); + let mut data_file = PathBuf::from(proj_dirs.data_dir()); + fs::create_dir_all(&data_file); + data_file.push("habit_record.json"); + return data_file; +} + +pub fn auto_habit_file() -> PathBuf { + let proj_dirs = project_dirs(); + let mut data_file = PathBuf::from(proj_dirs.data_dir()); + fs::create_dir_all(&data_file); + data_file.push("habit_record[auto].json"); + return data_file; } -- cgit v1.2.3