diff options
author | Canop <cano.petrole@gmail.com> | 2019-01-27 20:28:13 +0100 |
---|---|---|
committer | Canop <cano.petrole@gmail.com> | 2019-01-27 20:28:13 +0100 |
commit | e5ebd35b96040efe27c4683b7731d9489089d2c7 (patch) | |
tree | 9611bd0f5b7bb62e5c383cba4621f2c06d91d3b1 /src | |
parent | 3cb8224a3e2d4c28c673611f09f8352105a004e2 (diff) |
fix exact match not detected in verb search
Diffstat (limited to 'src')
-rw-r--r-- | src/conf.rs | 6 | ||||
-rw-r--r-- | src/verbs.rs | 29 |
2 files changed, 28 insertions, 7 deletions
diff --git a/src/conf.rs b/src/conf.rs index 7fe82d4..ce2653c 100644 --- a/src/conf.rs +++ b/src/conf.rs @@ -102,6 +102,12 @@ const DEFAULT_CONF_FILE: &str = r#" # The configuration documentation and complete list of built-in verbs # can be found in https://github.com/Canop/broot/documentation.md + +[[verbs]] +name = "parent" +invocation = "p" +execution = ":parent" + [[verbs]] name = "edit" invocation = "ed" diff --git a/src/verbs.rs b/src/verbs.rs index 49e4f51..d997395 100644 --- a/src/verbs.rs +++ b/src/verbs.rs @@ -309,16 +309,31 @@ impl VerbStore { pub fn matching_verbs(&self, prefix: &str) -> Vec<&Verb> { self.verbs.iter().filter(|v| v.matches(prefix)).collect() } - // TODO remove ? The intermediate PrefixSearchResult is about useless pub fn search(&self, prefix: &str) -> PrefixSearchResult<&Verb> { - if prefix.len() == 0 { - return PrefixSearchResult::TooManyMatches; + let mut found_index = 0; + let mut nb_found = 0; + for (index, verb) in self.verbs.iter().enumerate() { + if let Some(short_key) = &verb.short_key { + if short_key.starts_with(prefix) { + if short_key == prefix { + return PrefixSearchResult::Match(&verb); + } + found_index = index; + nb_found += 1; + continue; + } + } + if verb.long_key.starts_with(prefix) { + if verb.long_key == prefix { + return PrefixSearchResult::Match(&verb); + } + found_index = index; + nb_found += 1; + } } - let matching_verbs = self.matching_verbs(prefix); - debug!("matching verbs: {:?}", &matching_verbs); - match matching_verbs.len() { + match nb_found { 0 => PrefixSearchResult::NoMatch, - 1 => PrefixSearchResult::Match(matching_verbs[0]), + 1 => PrefixSearchResult::Match(&self.verbs[found_index]), _ => PrefixSearchResult::TooManyMatches, } } |