diff options
| author | Denis Evsyukov <[email protected]> | 2020-03-10 19:42:45 +0300 |
|---|---|---|
| committer | Denis Evsyukov <[email protected]> | 2020-03-10 19:42:45 +0300 |
| commit | c5b8e603cd2a90ffe6ac2cd6f7a60b100859c1dd (patch) | |
| tree | df42db31451493bbf01c7f947789d3be9cbff0fb | |
| parent | [+] optimize by size (diff) | |
| download | t-c5b8e603cd2a90ffe6ac2cd6f7a60b100859c1dd.tar.xz t-c5b8e603cd2a90ffe6ac2cd6f7a60b100859c1dd.zip | |
[+] use prefix and hash for tasks
| -rw-r--r-- | Cargo.toml | 4 | ||||
| -rw-r--r-- | src/main.rs | 37 |
2 files changed, 30 insertions, 11 deletions
@@ -11,7 +11,7 @@ getopts = "0.2" rust-crypto = "0.2" [profile.release] -opt-level = 's' # Optimize for size. +opt-level = 'z' # Optimize for size. lto = true # codegen-units = 1 -# panic = 'abort'
\ No newline at end of file +panic = 'abort'
\ No newline at end of file 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<String, String>, hash: &str) -> String { String::from(hash) } -fn get_prefix_by_hash(prefixes: &HashMap<String, String>, hash: &str) -> String { +fn get_prefix_by_hash(prefixes: &HashMap<String, String>, hash: &str) -> Option<String> { 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<String, String>, + prefixes: &HashMap<String, String>, +) -> Option<String> { + 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( |