From 101d877bf713df10c58968b80b0712477fad7bf3 Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Sat, 7 Mar 2020 18:46:45 +0300 Subject: [+] getopts --- src/main.rs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main.rs (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..88027c7 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,43 @@ +extern crate getopts; + +use getopts::*; +use std::env; + +fn main() { + let args: Vec = env::args().collect(); + let mut opts = Options::new(); + opts.optflag("h", "help", "Show this help screen"); + opts.optflag("", "done", "list done tasks instead of unfinished ones"); + opts.optopt("e", "edit", "edit TASK to contain TEXT", "TASK"); + opts.optopt("f", "finish", "mark TASK as finished", "TASK"); + opts.optopt("r", "remove", "Remove TASK from list", "TASK"); + opts.optopt("l", "list", "work on LIST", "LIST"); + opts.optopt("t", "taskdir", "work on the lists in DIR", "DIR"); + opts.optflag( + "d", + "delete-if-empty", + "delete the task file if it becomes empty", + ); + opts.optopt("g", "grep", "print only tasks that contain WORD", "WORD"); + opts.optflag( + "v", + "verbose", + "print more detailed output (full task ids, etc)", + ); + opts.optflag( + "q", + "quiet", + "print less detailed output (no task ids, etc)", + ); + + let matches = match opts.parse(&args[1..]) { + Ok(m) => m, + Err(f) => panic!(f.to_string()), + }; + + if matches.opt_present("h") { + let brief = "t is for people that want do things, not organize their tasks"; + opts.usage(&brief); + return; + } +} -- cgit v1.2.3 From 6a4acffd73eb51fcdaca701304482f9fe95b3612 Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Sat, 7 Mar 2020 18:57:10 +0300 Subject: [+] help --- src/main.rs | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 88027c7..283fa69 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,8 +36,17 @@ fn main() { }; if matches.opt_present("h") { - let brief = "t is for people that want do things, not organize their tasks"; - opts.usage(&brief); + let brief = "t is for people that want do things, not organize their tasks + +Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; + print!("{}", opts.usage(&brief)); return; } + + let input = if !matches.free.is_empty() { + matches.free[0].clone() + } else { + "Print tasks".to_string() + }; + println!("{}", input); } -- cgit v1.2.3 From 1f5898bafaa58f31d6914145c895bc93bd8336c6 Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Sat, 7 Mar 2020 20:10:38 +0300 Subject: [+] read file --- src/main.rs | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 283fa69..f1ec884 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,7 +2,8 @@ extern crate getopts; use getopts::*; use std::env; - +use std::fs; +use std::path::PathBuf; fn main() { let args: Vec = env::args().collect(); let mut opts = Options::new(); @@ -43,6 +44,22 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; return; } + let path = env::current_dir().unwrap(); + let taskfile = matches.opt_str("l").unwrap_or_else(|| "tasks".to_string()); + let taskdir = matches + .opt_str("t") + .unwrap_or_else(|| path.to_str().unwrap().to_string()); + + let mut taskpath = PathBuf::from(&taskdir); + taskpath.push(&taskfile); + + let donefile = format!(".{}.done", taskfile); + let mut donepath = PathBuf::from(&taskdir); + donepath.push(donefile); + + let contents = fs::read_to_string(taskpath).expect("Something went wrong reading the file"); + println!("{}", contents); + let input = if !matches.free.is_empty() { matches.free[0].clone() } else { -- cgit v1.2.3 From 31d2dd764d7d08a110c0d4b8699edd0da6e0448d Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Sun, 8 Mar 2020 16:09:09 +0300 Subject: [+] read_files --- src/main.rs | 44 ++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 42 insertions(+), 2 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index f1ec884..6bdc1fb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,9 +1,14 @@ extern crate getopts; +use crypto::digest::Digest; +use crypto::sha2::Sha256; use getopts::*; +use std::collections::HashMap; use std::env; use std::fs; +use std::path::Path; use std::path::PathBuf; +use std::process::exit; fn main() { let args: Vec = env::args().collect(); let mut opts = Options::new(); @@ -53,12 +58,13 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; let mut taskpath = PathBuf::from(&taskdir); taskpath.push(&taskfile); + println!("taskdir: {}", taskpath.to_str().unwrap().to_string()); + let donefile = format!(".{}.done", taskfile); let mut donepath = PathBuf::from(&taskdir); donepath.push(donefile); - let contents = fs::read_to_string(taskpath).expect("Something went wrong reading the file"); - println!("{}", contents); + let (tasks, done) = read_files(taskpath, donepath); let input = if !matches.free.is_empty() { matches.free[0].clone() @@ -67,3 +73,37 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; }; println!("{}", input); } + +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, HashMap) { + if !Path::new(&taskpath).exists() { + println!("File {} does not exist...", taskpath.to_str().unwrap()); + exit(1); + } + 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()); + println!("{}", contents); + + let mut tasks: HashMap = HashMap::new(); + let mut done: HashMap = HashMap::new(); + + for line in contents.lines() { + tasks.insert(hash(line.to_string()), line.to_string()); + } + + for line in contents_done.lines() { + done.insert(hash(line.to_string()), line.to_string()); + } + + (tasks, done) +} -- cgit v1.2.3 From da72f547b2debf8e05451ce8744162799cbb2968 Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Sun, 8 Mar 2020 17:33:00 +0300 Subject: Work with files [+] write to files [~] read from files --- src/main.rs | 105 +++++++++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 82 insertions(+), 23 deletions(-) (limited to 'src/main.rs') 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 = 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, HashMap) { - if !Path::new(&taskpath).exists() { - println!("File {} does not exist...", taskpath.to_str().unwrap()); - exit(1); - } +fn read_files(taskpath: &PathBuf) -> (HashMap, HashMap) { + // 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 = HashMap::new(); let mut done: HashMap = 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, + done: HashMap, + 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"); +} -- cgit v1.2.3 From 3337cb38b2030c5521ab86d34a65b0b636b46053 Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Mon, 9 Mar 2020 10:03:19 +0300 Subject: [~] read files --- src/main.rs | 73 +++++++++++++++++++++++++++---------------------------------- 1 file changed, 32 insertions(+), 41 deletions(-) (limited to 'src/main.rs') 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, HashMap) { - // 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, HashMap = HashMap::new(); @@ -124,13 +91,37 @@ fn read_files(taskpath: &PathBuf) -> (HashMap, HashMap String { + let mut hasher = Sha256::new(); + hasher.input_str(&str); + hasher.result_str() } fn write_files( - tasks: HashMap, - done: HashMap, - taskpath: PathBuf, + tasks: &HashMap, + done: &HashMap, + 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); -- cgit v1.2.3 From a7778e34ab4eef5ca774efbc58e00fa7a00a2e77 Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Mon, 9 Mar 2020 10:05:48 +0300 Subject: [~] simpify donefile --- src/main.rs | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 72f81de..806d303 100644 --- a/src/main.rs +++ b/src/main.rs @@ -51,6 +51,7 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; let path = env::current_dir().unwrap(); let taskfile = matches.opt_str("l").unwrap_or_else(|| "tasks".to_string()); + let donefile = format!(".{}.done", taskfile); let taskdir = matches .opt_str("t") .unwrap_or_else(|| path.to_str().unwrap().to_string()); @@ -61,16 +62,6 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; println!("taskdir: {}", taskpath.to_str().unwrap().to_string()); // read files - 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); -- cgit v1.2.3 From 1378ced08948eed9f800caf2f7d0981817c8ede9 Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Mon, 9 Mar 2020 11:02:05 +0300 Subject: [~] write files --- src/main.rs | 93 ++++++++++++++++++++++++++++++------------------------------- 1 file changed, 46 insertions(+), 47 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 806d303..a302650 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,9 +6,11 @@ use getopts::*; use std::collections::HashMap; use std::env; use std::fs; +use std::fs::File; +use std::io::LineWriter; +use std::io::Write; use std::path::Path; use std::path::PathBuf; - fn main() { let args: Vec = env::args().collect(); let mut opts = Options::new(); @@ -59,8 +61,6 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; let mut taskpath = PathBuf::from(&taskdir); taskpath.push(&taskfile); - println!("taskdir: {}", taskpath.to_str().unwrap().to_string()); - // read files let mut donepath = PathBuf::from(taskpath.as_path().parent().unwrap().to_path_buf()); donepath.push(donefile); @@ -83,21 +83,60 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; } if matches.opt_present("done") { - for (hash, task) in done { - println!("{} - {}", hash, task); + for (_, task) in done { + println!("{}", task); } return; } + let mut write = false; + + // 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(&key); + write = true; + } + + // add new task 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; + 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 file = File::create(taskpath.to_str().unwrap()).unwrap(); + let mut file = LineWriter::new(file); + + for (_, task) in &tasks { + file.write_all(task.as_bytes()).unwrap(); + } + file.flush().unwrap(); + //done + let file = File::create(donepath.to_str().unwrap()).unwrap(); + let mut file = LineWriter::new(file); + + for (_, task) in &done { + file.write_all(task.as_bytes()).unwrap(); + } + file.flush().unwrap(); } + // print tasks for (hash, task) in tasks { println!("{} - {}", hash, task); } @@ -108,43 +147,3 @@ fn hash(str: &String) -> String { hasher.input_str(&str); hasher.result_str() } - -fn write_files( - tasks: &HashMap, - done: &HashMap, - 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"); -} -- cgit v1.2.3 From ad30146ff6c1a16b24599d45ced41fa70607f35b Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Mon, 9 Mar 2020 11:18:46 +0300 Subject: [~] write to files --- src/main.rs | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index a302650..b2816fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,8 +6,7 @@ use getopts::*; use std::collections::HashMap; use std::env; use std::fs; -use std::fs::File; -use std::io::LineWriter; +use std::fs::OpenOptions; use std::io::Write; use std::path::Path; use std::path::PathBuf; @@ -66,10 +65,7 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; donepath.push(donefile); 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()); - println!("{}", contents); let mut tasks: HashMap = HashMap::new(); let mut done: HashMap = HashMap::new(); @@ -104,8 +100,6 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; 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 = true; } @@ -118,22 +112,31 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; } return; } + // TODO fix write to file //tasks - let file = File::create(taskpath.to_str().unwrap()).unwrap(); - let mut file = LineWriter::new(file); + let mut file = OpenOptions::new() + .create(true) + .write(true) + .append(true) + .open(taskpath.to_str().unwrap()) + .unwrap(); for (_, task) in &tasks { - file.write_all(task.as_bytes()).unwrap(); + writeln!(file, "{}", task).unwrap(); } - file.flush().unwrap(); + //done - let file = File::create(donepath.to_str().unwrap()).unwrap(); - let mut file = LineWriter::new(file); + let mut file = OpenOptions::new() + .create(true) + .write(true) + .append(true) + .open(donepath.to_str().unwrap()) + .unwrap(); for (_, task) in &done { - file.write_all(task.as_bytes()).unwrap(); + writeln!(file, "{}", task).unwrap(); } - file.flush().unwrap(); + return; } // print tasks -- cgit v1.2.3 From b328d9900d0f17c12083911536c3513b177afbc7 Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Mon, 9 Mar 2020 12:49:49 +0300 Subject: [~] write files --- src/main.rs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index b2816fe..67ae35f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,7 +92,7 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; 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(&key); + tasks.remove_entry(&key); write = true; } @@ -112,12 +112,10 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; } return; } - // TODO fix write to file //tasks let mut file = OpenOptions::new() .create(true) .write(true) - .append(true) .open(taskpath.to_str().unwrap()) .unwrap(); @@ -129,7 +127,6 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; let mut file = OpenOptions::new() .create(true) .write(true) - .append(true) .open(donepath.to_str().unwrap()) .unwrap(); -- cgit v1.2.3 From d61637d581a341bae0c3095dbbef88620d73ce00 Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Mon, 9 Mar 2020 16:12:53 +0300 Subject: [~] write_files --- src/main.rs | 87 +++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 47 insertions(+), 40 deletions(-) (limited to 'src/main.rs') 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, + done: HashMap, + 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(); +} -- cgit v1.2.3 From c0a2b5b4762f8e2693dfd97cb680d9f41ad81d5e Mon Sep 17 00:00:00 2001 From: Evsyukov Denis Anatolyevich Date: Tue, 10 Mar 2020 09:11:02 +0300 Subject: [+] check directory exist --- src/main.rs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 7de513a..d2dc7e9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,10 +57,14 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; .opt_str("t") .unwrap_or_else(|| path.to_str().unwrap().to_string()); + if !Path::new(&taskdir).exists() { + eprintln!("Directory does not exist: {}", taskdir); + return; + } + // read files let mut taskpath = PathBuf::from(&taskdir); taskpath.push(&taskfile); - // read files let mut donepath = taskpath.as_path().parent().unwrap().to_path_buf(); donepath.push(donefile); @@ -94,6 +98,13 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; write_files(tasks, done, taskpath, donepath, delete_empty); return; } + // remove task + if matches.opt_present("r") { + let task = matches.opt_str("f").unwrap(); + tasks.remove(&task); + write_files(tasks, done, taskpath, donepath, delete_empty); + return; + } // add new task if !matches.free.is_empty() { -- cgit v1.2.3 From 4954473947928e2528c41530a359076f07e48781 Mon Sep 17 00:00:00 2001 From: Evsyukov Denis Anatolyevich Date: Tue, 10 Mar 2020 09:20:00 +0300 Subject: [+] remove and edit tasks --- src/main.rs | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index d2dc7e9..8ee376a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -93,16 +93,43 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; 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(&key); - write_files(tasks, done, taskpath, donepath, delete_empty); + if tasks.contains_key(&task) { + done.insert(task, tasks.get(&key).unwrap().to_string()); + tasks.remove(&key); + write_files(tasks, done, taskpath, donepath, delete_empty); + } else { + println!("Task does not exist: {}", &task); + } return; } // remove task if matches.opt_present("r") { - let task = matches.opt_str("f").unwrap(); - tasks.remove(&task); - write_files(tasks, done, taskpath, donepath, delete_empty); + let task = matches.opt_str("r").unwrap(); + if tasks.contains_key(&task) { + tasks.remove(&task); + write_files(tasks, done, taskpath, donepath, delete_empty); + } else { + println!("Task does not exist: {}", &task); + } + return; + } + + // edit task + if matches.opt_present("e") { + let task = matches.opt_str("e").unwrap(); + if tasks.contains_key(&task) { + if !matches.free.is_empty() { + tasks.remove(&task); + let task = matches.free.join(" "); + tasks.insert(hash(&task), task); + write_files(tasks, done, taskpath, donepath, delete_empty); + } else { + println!("Please provide text for new task."); + return; + } + } else { + println!("Task does not exist: {}", &task); + } return; } -- cgit v1.2.3 From 071d08e41b5b896af95a58a48bb4fc5077701502 Mon Sep 17 00:00:00 2001 From: Evsyukov Denis Anatolyevich Date: Tue, 10 Mar 2020 09:29:27 +0300 Subject: [+] support for multiple ids in remove and finish --- src/main.rs | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 8ee376a..201d77d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -92,25 +92,31 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; // finish task if matches.opt_present("f") { let task = matches.opt_str("f").unwrap(); - let key = matches.opt_str("f").unwrap(); - if tasks.contains_key(&task) { - done.insert(task, tasks.get(&key).unwrap().to_string()); - tasks.remove(&key); - write_files(tasks, done, taskpath, donepath, delete_empty); - } else { - println!("Task does not exist: {}", &task); + for t in task.split(',') { + let t = String::from(t); + let key = String::from(&t); + if tasks.contains_key(&t) { + done.insert(t, tasks.get(&key).unwrap().to_string()); + tasks.remove(&key); + } else { + println!("Task does not exist: {}", &t); + } } + write_files(tasks, done, taskpath, donepath, delete_empty); return; } // remove task if matches.opt_present("r") { let task = matches.opt_str("r").unwrap(); - if tasks.contains_key(&task) { - tasks.remove(&task); - write_files(tasks, done, taskpath, donepath, delete_empty); - } else { - println!("Task does not exist: {}", &task); + for t in task.split(',') { + let t = String::from(t); + if tasks.contains_key(&t) { + tasks.remove(&t); + } else { + println!("Task does not exist: {}", &task); + } } + write_files(tasks, done, taskpath, donepath, delete_empty); return; } -- cgit v1.2.3 From fb046388692921051856406e76bc633a6e98e67b Mon Sep 17 00:00:00 2001 From: Evsyukov Denis Anatolyevich Date: Tue, 10 Mar 2020 10:25:47 +0300 Subject: [+] prefixes --- src/main.rs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 201d77d..0811cfb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -73,6 +73,7 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; let mut tasks: HashMap = HashMap::new(); let mut done: HashMap = HashMap::new(); + let mut prefixes: HashMap = HashMap::new(); for line in contents.lines() { tasks.insert(hash(&line.to_string()), line.to_string()); @@ -82,6 +83,12 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; done.insert(hash(&line.to_string()), line.to_string()); } + // fill prefixes + for (hash, _) in tasks.iter() { + prefixes.insert(get_prefix(&prefixes, hash.as_str()), String::from(hash)); + } + + // commands if matches.opt_present("done") { for (_, task) in done { println!("{}", task); @@ -148,8 +155,14 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; } // print tasks - for (hash, task) in tasks { - println!("{} - {}", hash, task); + for (hash, task) in tasks.iter() { + if matches.opt_present("q") { + println!("{}", task); + } else if matches.opt_present("v") { + println!("{} - {}", hash, task); + } else { + println!("{} - {}", get_prefix_by_hash(&prefixes, hash), task); + } } } @@ -159,6 +172,25 @@ fn hash(str: &str) -> String { hasher.result_str() } +fn get_prefix(prefixes: &HashMap, hash: &str) -> String { + for i in 1..hash.len() { + let prefix = &hash[..i]; + if !prefixes.contains_key(prefix) { + return String::from(prefix); + } + } + String::from(hash) +} + +fn get_prefix_by_hash(prefixes: &HashMap, hash: &str) -> String { + for (id, prefix) in prefixes.iter() { + if hash == prefix { + return String::from(id); + } + } + String::from("") +} + fn write_files( tasks: HashMap, done: HashMap, -- cgit v1.2.3 From c5b8e603cd2a90ffe6ac2cd6f7a60b100859c1dd Mon Sep 17 00:00:00 2001 From: Denis Evsyukov Date: Tue, 10 Mar 2020 19:42:45 +0300 Subject: [+] use prefix and hash for tasks --- src/main.rs | 37 ++++++++++++++++++++++++++++--------- 1 file changed, 28 insertions(+), 9 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index 0811cfb..ac2fcbc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -99,6 +99,7 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; // finish task if matches.opt_present("f") { let task = matches.opt_str("f").unwrap(); + let task = get_hash(task.as_str(), &tasks, &prefixes).unwrap(); for t in task.split(',') { let t = String::from(t); let key = String::from(&t); @@ -115,6 +116,7 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; // remove task if matches.opt_present("r") { let task = matches.opt_str("r").unwrap(); + let task = get_hash(task.as_str(), &tasks, &prefixes).unwrap(); for t in task.split(',') { let t = String::from(t); if tasks.contains_key(&t) { @@ -130,6 +132,7 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; // edit task if matches.opt_present("e") { let task = matches.opt_str("e").unwrap(); + let task = get_hash(task.as_str(), &tasks, &prefixes).unwrap(); if tasks.contains_key(&task) { if !matches.free.is_empty() { tasks.remove(&task); @@ -155,13 +158,16 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; } // print tasks + let word = matches.opt_str("g").unwrap_or_else(|| "".to_string()); for (hash, task) in tasks.iter() { - if matches.opt_present("q") { - println!("{}", task); - } else if matches.opt_present("v") { - println!("{} - {}", hash, task); - } else { - println!("{} - {}", get_prefix_by_hash(&prefixes, hash), task); + if task.contains(&word) { + if matches.opt_present("q") { + println!("{}", task); + } else if matches.opt_present("v") { + println!("{} - {}", hash, task); + } else if let Some(prefix) = get_prefix_by_hash(&prefixes, hash) { + println!("{} - {}", prefix, task); + } } } } @@ -182,13 +188,26 @@ fn get_prefix(prefixes: &HashMap, hash: &str) -> String { String::from(hash) } -fn get_prefix_by_hash(prefixes: &HashMap, hash: &str) -> String { +fn get_prefix_by_hash(prefixes: &HashMap, hash: &str) -> Option { for (id, prefix) in prefixes.iter() { if hash == prefix { - return String::from(id); + return Some(String::from(id)); } } - String::from("") + None +} + +fn get_hash( + id: &str, + tasks: &HashMap, + prefixes: &HashMap, +) -> Option { + if prefixes.contains_key(id) { + return Some(prefixes.get(id).unwrap().to_string()); + } else if tasks.contains_key(id) { + return Some(String::from(id)); + } + None } fn write_files( -- cgit v1.2.3 From 0772669b1dfc45c030890097aecccd4329b3628c Mon Sep 17 00:00:00 2001 From: Evsyukov Denis Anatolyevich Date: Wed, 11 Mar 2020 08:35:56 +0300 Subject: [~] rename hash function to get_sha256 --- src/main.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'src/main.rs') diff --git a/src/main.rs b/src/main.rs index ac2fcbc..da2726b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ use std::fs::OpenOptions; use std::io::Write; use std::path::Path; use std::path::PathBuf; + fn main() { let args: Vec = env::args().collect(); let mut opts = Options::new(); @@ -76,11 +77,11 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; let mut prefixes: HashMap = HashMap::new(); for line in contents.lines() { - tasks.insert(hash(&line.to_string()), line.to_string()); + tasks.insert(get_sha256(&line.to_string()), line.to_string()); } for line in contents_done.lines() { - done.insert(hash(&line.to_string()), line.to_string()); + done.insert(get_sha256(&line.to_string()), line.to_string()); } // fill prefixes @@ -137,7 +138,7 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; if !matches.free.is_empty() { tasks.remove(&task); let task = matches.free.join(" "); - tasks.insert(hash(&task), task); + tasks.insert(get_sha256(&task), task); write_files(tasks, done, taskpath, donepath, delete_empty); } else { println!("Please provide text for new task."); @@ -152,7 +153,7 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; // add new task if !matches.free.is_empty() { let task = matches.free.join(" "); - tasks.insert(hash(&task), task); + tasks.insert(get_sha256(&task), task); write_files(tasks, done, taskpath, donepath, delete_empty); return; } @@ -172,7 +173,7 @@ Usage: t [-t DIR] [-l LIST] [options] [TEXT]"; } } -fn hash(str: &str) -> String { +fn get_sha256(str: &str) -> String { let mut hasher = Sha256::new(); hasher.input_str(&str); hasher.result_str() -- cgit v1.2.3