aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/main.rs87
1 files changed, 47 insertions, 40 deletions
diff --git a/src/main.rs b/src/main.rs
index 67ae35f..7de513a 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -61,7 +61,7 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]";
taskpath.push(&taskfile);
// read files
- let mut donepath = PathBuf::from(taskpath.as_path().parent().unwrap().to_path_buf());
+ let mut donepath = taskpath.as_path().parent().unwrap().to_path_buf();
donepath.push(donefile);
let contents = fs::read_to_string(&taskpath).unwrap_or_else(|_| "".to_string());
@@ -84,55 +84,22 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]";
}
return;
}
-
- let mut write = false;
-
+ let delete_empty = matches.opt_present("d");
// finish task
if matches.opt_present("f") {
let task = matches.opt_str("f").unwrap();
let key = matches.opt_str("f").unwrap();
done.insert(task, tasks.get(&key).unwrap().to_string());
- tasks.remove_entry(&key);
- write = true;
+ tasks.remove(&key);
+ write_files(tasks, done, taskpath, donepath, delete_empty);
+ return;
}
// add new task
if !matches.free.is_empty() {
let task = matches.free.join(" ");
tasks.insert(hash(&task), task);
- write = true;
- }
-
- // write files
- if write {
- if matches.opt_present("d") && tasks.is_empty() {
- if Path::new(&taskpath).exists() {
- fs::remove_file(taskpath).unwrap();
- fs::remove_file(donepath).unwrap();
- }
- return;
- }
- //tasks
- let mut file = OpenOptions::new()
- .create(true)
- .write(true)
- .open(taskpath.to_str().unwrap())
- .unwrap();
-
- for (_, task) in &tasks {
- writeln!(file, "{}", task).unwrap();
- }
-
- //done
- let mut file = OpenOptions::new()
- .create(true)
- .write(true)
- .open(donepath.to_str().unwrap())
- .unwrap();
-
- for (_, task) in &done {
- writeln!(file, "{}", task).unwrap();
- }
+ write_files(tasks, done, taskpath, donepath, delete_empty);
return;
}
@@ -142,8 +109,48 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]";
}
}
-fn hash(str: &String) -> String {
+fn hash(str: &str) -> String {
let mut hasher = Sha256::new();
hasher.input_str(&str);
hasher.result_str()
}
+
+fn write_files(
+ tasks: HashMap<String, String>,
+ done: HashMap<String, String>,
+ taskpath: PathBuf,
+ donepath: PathBuf,
+ delete_empty: bool,
+) {
+ if delete_empty && tasks.is_empty() {
+ if Path::new(&taskpath).exists() {
+ fs::remove_file(taskpath).unwrap();
+ fs::remove_file(donepath).unwrap();
+ }
+ return;
+ }
+ //tasks
+ let mut taskfile = OpenOptions::new()
+ .create(true)
+ .write(true)
+ .truncate(true)
+ .open(taskpath.to_str().unwrap())
+ .unwrap();
+
+ for (_, task) in tasks {
+ writeln!(taskfile, "{}", task).unwrap();
+ }
+ taskfile.sync_all().unwrap();
+ //done
+ let mut donefile = OpenOptions::new()
+ .create(true)
+ .write(true)
+ .truncate(true)
+ .open(donepath.to_str().unwrap())
+ .unwrap();
+
+ for (_, task) in done {
+ writeln!(donefile, "{}", task).unwrap();
+ }
+ donefile.sync_all().unwrap();
+}