diff options
| author | Evsyukov Denis Anatolyevich <[email protected]> | 2020-03-10 10:25:47 +0300 |
|---|---|---|
| committer | Evsyukov Denis Anatolyevich <[email protected]> | 2020-03-10 10:25:47 +0300 |
| commit | fb046388692921051856406e76bc633a6e98e67b (patch) | |
| tree | 126593f92d8dbfd1480d19b0cd8aac9f47171902 | |
| parent | [+] support for multiple ids in remove and finish (diff) | |
| download | t-fb046388692921051856406e76bc633a6e98e67b.tar.xz t-fb046388692921051856406e76bc633a6e98e67b.zip | |
[+] prefixes
| -rw-r--r-- | src/main.rs | 36 |
1 files changed, 34 insertions, 2 deletions
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<String, String> = HashMap::new(); let mut done: HashMap<String, String> = HashMap::new(); + let mut prefixes: HashMap<String, String> = 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<String, String>, 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<String, String>, hash: &str) -> String { + for (id, prefix) in prefixes.iter() { + if hash == prefix { + return String::from(id); + } + } + String::from("") +} + fn write_files( tasks: HashMap<String, String>, done: HashMap<String, String>, |