diff options
| author | Denis Evsyukov <[email protected]> | 2020-03-08 17:33:00 +0300 |
|---|---|---|
| committer | Denis Evsyukov <[email protected]> | 2020-03-08 17:33:00 +0300 |
| commit | da72f547b2debf8e05451ce8744162799cbb2968 (patch) | |
| tree | 332be6bdad789ed4b454191fc000b30f02f76227 | |
| parent | [+] read_files (diff) | |
| download | t-da72f547b2debf8e05451ce8744162799cbb2968.tar.xz t-da72f547b2debf8e05451ce8744162799cbb2968.zip | |
Work with files
[+] write to files
[~] read from files
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | src/main.rs | 105 |
2 files changed, 84 insertions, 23 deletions
@@ -1,3 +1,5 @@ /target /Cargo.lock +tasks +.tasks.done diff --git a/src/main.rs b/src/main.rs index 6bdc1fb..b2a59be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use std::env; use std::fs; use std::path::Path; use std::path::PathBuf; -use std::process::exit; + fn main() { let args: Vec<String> = env::args().collect(); let mut opts = Options::new(); @@ -60,50 +60,109 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; println!("taskdir: {}", taskpath.to_str().unwrap().to_string()); - let donefile = format!(".{}.done", taskfile); - let mut donepath = PathBuf::from(&taskdir); - donepath.push(donefile); + let (mut tasks, done) = read_files(&taskpath); - let (tasks, done) = read_files(taskpath, donepath); + if matches.opt_present("done") { + for (hash, task) in done { + println!("{} - {}", hash, task); + } + return; + } - let input = if !matches.free.is_empty() { - matches.free[0].clone() - } else { - "Print tasks".to_string() - }; - println!("{}", input); + 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 { +fn hash(str: &String) -> String { let mut hasher = Sha256::new(); hasher.input_str(&str); hasher.result_str() } -fn read_files( - taskpath: PathBuf, - donefile: PathBuf, -) -> (HashMap<String, String>, HashMap<String, String>) { - if !Path::new(&taskpath).exists() { - println!("File {} does not exist...", taskpath.to_str().unwrap()); - exit(1); - } +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); + // } + let donefile = format!( + ".{}.done", + taskpath + .as_path() + .file_name() + .unwrap() + .to_os_string() + .into_string() + .unwrap() + ); + 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()); println!("{}", contents); - let contents_done = fs::read_to_string(donefile).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(); let mut done: HashMap<String, String> = HashMap::new(); for line in contents.lines() { - tasks.insert(hash(line.to_string()), line.to_string()); + tasks.insert(hash(&line.to_string()), line.to_string()); } for line in contents_done.lines() { - done.insert(hash(line.to_string()), line.to_string()); + done.insert(hash(&line.to_string()), line.to_string()); } (tasks, done) } + +fn write_files( + tasks: HashMap<String, String>, + done: HashMap<String, String>, + taskpath: PathBuf, + delete_empty: bool, +) { + let donefile = format!( + ".{}.done", + taskpath + .as_path() + .file_name() + .unwrap() + .to_os_string() + .into_string() + .unwrap() + ); + let mut donepath = PathBuf::from(taskpath.as_path().parent().unwrap().to_path_buf()); + donepath.push(donefile); + + if delete_empty { + if Path::new(&taskpath).exists() { + fs::remove_file(taskpath).unwrap(); + fs::remove_file(donepath).unwrap(); + } + return; + } + let mut data = String::new(); + for (hash, task) in &tasks { + data = format!("{}\n{} - {}\n", data, hash, task); + } + println!("{:?}", tasks); + println!("{:?}", data); + fs::write(taskpath, data).expect("Unable to write task file"); + let mut data = String::new(); + for (hash, task) in done { + data = format!("{}\n{} - {}\n", data, hash, task); + } + fs::write(donepath, data).expect("Unable to write donefile"); +} |