summaryrefslogtreecommitdiffstats
path: root/src/poem/elements/verse.rs
diff options
context:
space:
mode:
authorRory Dudley2024-05-20 23:52:37 -0600
committerRory Dudley2024-05-20 23:52:37 -0600
commit85a3bd273e927b8dc31aadf263b93ef8fd3b7a39 (patch)
treea3d228ce5c2d94ee4651c21cc5759b362fe0f769 /src/poem/elements/verse.rs
parent6d39d319d54c5385e9db345e0ff5a9640cc4190d (diff)
downloaddwarvish-85a3bd273e927b8dc31aadf263b93ef8fd3b7a39.tar.gz
Update the return value of Verse::spellcheck()
Instead of returning true or false, if a given bin is found on the $PATH, return the index of where the bin was found. This is useful, in case we want to actually get the full path of the bin. If a full or relative path is specified, the function will return the length of the bins vector.
Notes
Notes: This is primarily useful for the built-in 'which' command, so that we can print out the full bin path without invoking /usr/bin/which.
Diffstat (limited to 'src/poem/elements/verse.rs')
-rw-r--r--src/poem/elements/verse.rs25
1 files changed, 13 insertions, 12 deletions
diff --git a/src/poem/elements/verse.rs b/src/poem/elements/verse.rs
index 5694b55..a1ae65f 100644
--- a/src/poem/elements/verse.rs
+++ b/src/poem/elements/verse.rs
@@ -192,16 +192,16 @@ impl Verse {
/// let stanza_success = Stanza::new(command_success);
/// let stanza_fail = Stanza::new(command_fail);
///
- /// stanza_success.spellcheck(bins) // -> true
- /// stanza_fail.spellcheck(bins) // -> false
+ /// stanza_success.spellcheck(bins) // -> Some(i)
+ /// stanza_fail.spellcheck(bins) // -> None
/// ```
- pub fn spellcheck(&self, bins: &Vec<String>) -> bool {
+ pub fn spellcheck(&self, bins: &Vec<String>) -> Option<usize> {
// An empty verb (i.e. the empty string) cannot be a program, so
// return false
// Thanks to the parsing in Poem::read, however, it's
// unlikely for this to happen
if self.verb().is_empty() {
- return false;
+ return None;
}
// Only search the $PATH if a full or relative path was not given, or
@@ -209,17 +209,18 @@ impl Verse {
if !Path::new(self.verb().as_str()).exists() {
// Try to find a binary in our path with the same name as the verb
// Searches in $PATH order
- match bins
- .iter()
- .find(|bin| bin.split('/').last().unwrap() == self.verb())
- {
- Some(_) => return true,
- None => return false,
+ for (i, path) in bins.iter().enumerate() {
+ if path.split('/').last().unwrap() == self.verb() {
+ return Some(i);
+ }
}
+
+ // If it was not found, return None
+ return None;
}
- // Return true if the full path or relative path exists
- true
+ // Return the length of bins if the full path or relative path exists
+ Some(bins.len())
}
/// Run a command