diff options
| -rw-r--r-- | src/main.rs | 73 |
1 files changed, 32 insertions, 41 deletions
diff --git a/src/main.rs b/src/main.rs index b2a59be..72f81de 100644 --- a/src/main.rs +++ b/src/main.rs @@ -60,40 +60,7 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; println!("taskdir: {}", taskpath.to_str().unwrap().to_string()); - let (mut tasks, done) = read_files(&taskpath); - - if matches.opt_present("done") { - for (hash, task) in done { - println!("{} - {}", hash, task); - } - return; - } - - if !matches.free.is_empty() { - let task = matches.free.join(" "); - tasks.insert(hash(&task), task); - let delete_empty = matches.opt_present("d"); - println!("{:?}", delete_empty); - write_files(tasks, done, taskpath, delete_empty); - return; - } - - for (hash, task) in tasks { - println!("{} - {}", hash, task); - } -} - -fn hash(str: &String) -> String { - let mut hasher = Sha256::new(); - hasher.input_str(&str); - hasher.result_str() -} - -fn read_files(taskpath: &PathBuf) -> (HashMap<String, String>, HashMap<String, String>) { - // if !Path::new(&taskpath).exists() { - // println!("File {} does not exist...", taskpath.to_str().unwrap()); - // exit(1); - // } + // read files let donefile = format!( ".{}.done", taskpath @@ -107,10 +74,10 @@ fn read_files(taskpath: &PathBuf) -> (HashMap<String, String>, HashMap<String, S let mut donepath = PathBuf::from(taskpath.as_path().parent().unwrap().to_path_buf()); donepath.push(donefile); - let contents = fs::read_to_string(taskpath).unwrap_or_else(|_| "".to_string()); + let contents = fs::read_to_string(&taskpath).unwrap_or_else(|_| "".to_string()); println!("{}", contents); - let contents_done = fs::read_to_string(donepath).unwrap_or_else(|_| "".to_string()); + let contents_done = fs::read_to_string(&donepath).unwrap_or_else(|_| "".to_string()); println!("{}", contents); let mut tasks: HashMap<String, String> = HashMap::new(); @@ -124,13 +91,37 @@ fn read_files(taskpath: &PathBuf) -> (HashMap<String, String>, HashMap<String, S done.insert(hash(&line.to_string()), line.to_string()); } - (tasks, done) + if matches.opt_present("done") { + for (hash, task) in done { + println!("{} - {}", hash, task); + } + return; + } + + if !matches.free.is_empty() { + let task = matches.free.join(" "); + tasks.insert(hash(&task), task); + let delete_empty = matches.opt_present("d"); + println!("{:?}", delete_empty); + write_files(&tasks, &done, &taskpath, delete_empty); + return; + } + + for (hash, task) in tasks { + println!("{} - {}", hash, task); + } +} + +fn hash(str: &String) -> 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, + tasks: &HashMap<String, String>, + done: &HashMap<String, String>, + taskpath: &PathBuf, delete_empty: bool, ) { let donefile = format!( @@ -154,7 +145,7 @@ fn write_files( return; } let mut data = String::new(); - for (hash, task) in &tasks { + for (hash, task) in tasks { data = format!("{}\n{} - {}\n", data, hash, task); } println!("{:?}", tasks); |