aboutsummaryrefslogtreecommitdiff
path: root/src/app.rs
diff options
context:
space:
mode:
authorAkshay <[email protected]>2020-07-10 18:05:30 +0100
committerAkshay <[email protected]>2020-07-10 18:05:30 +0100
commitb6ea375485083860094a904b7a6c7d97fa42b8f8 (patch)
tree52dba4e164e23b2fba4a8764c145d6d734ed7fab /src/app.rs
parent872297132d9d1fa39545fee54a1a25a95bdbe22d (diff)
follow XDG_DATA_DIR spec for app data
Diffstat (limited to 'src/app.rs')
-rw-r--r--src/app.rs38
1 files changed, 25 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 }