diff options
author | Rory Dudley | 2024-05-20 23:54:01 -0600 |
---|---|---|
committer | Rory Dudley | 2024-05-20 23:54:01 -0600 |
commit | dedadcfd30516c40692fe495a6ad10aea7c050de (patch) | |
tree | ce0f8dcb6546bbd9e0f61a35c88c32c9c6c608ab | |
parent | 85a3bd273e927b8dc31aadf263b93ef8fd3b7a39 (diff) | |
download | dwarvish-dedadcfd30516c40692fe495a6ad10aea7c050de.tar.gz |
Added the 'which' built-in command
Added a built-in which command (for MacOS and BSD), which can check
aliases, and other shell built-in commands, in addition to bins on the
$PATH.
-rw-r--r-- | src/poem/anthology.rs | 6 | ||||
-rw-r--r-- | src/poem/anthology/which.rs | 61 |
2 files changed, 65 insertions, 2 deletions
diff --git a/src/poem/anthology.rs b/src/poem/anthology.rs index 7766f62..8ff70f9 100644 --- a/src/poem/anthology.rs +++ b/src/poem/anthology.rs @@ -3,12 +3,13 @@ mod cd; mod exit; mod export; mod source; +mod which; use crate::compose::Environment; use crate::poem::Verse; /// A static list of all the built-in commands -static INDEX: [&str; 7] = [ - "alias", "cd", "exit", "export", "source", "unalias", "unset", +static INDEX: [&str; 8] = [ + "alias", "cd", "exit", "export", "source", "unalias", "unset", "which", ]; /// Lookup the index of a built-in command @@ -53,6 +54,7 @@ pub fn incant(verse: &Verse, out: &mut Vec<u8>, index: usize, env: &mut Environm "source" => source::incant(verse, out, env), "unalias" => alias::unincant(verse, &mut env.aliases), "unset" => export::unincant(verse), + "which" => which::incant(verse, out, env), _ => unreachable!(), } } diff --git a/src/poem/anthology/which.rs b/src/poem/anthology/which.rs new file mode 100644 index 0000000..515b52e --- /dev/null +++ b/src/poem/anthology/which.rs @@ -0,0 +1,61 @@ +use crate::compose::Environment; +use crate::poem::Verse; + +pub fn incant(verse: &Verse, out: &mut Vec<u8>, env: &Environment) -> i32 { + let mut status = 0; + match verse.clause() { + Some(clause) => { + let mut output: String; + for word in clause { + // Check if it's an alias + if env.aliases.contains_key(&word) { + output = format!("{}: aliased to {}\n", word, env.aliases.get(&word).unwrap()); + if verse.couplet > 0 { + out.append(&mut output.as_bytes().to_vec()); + } else { + print!("{}", output); + } + continue; + } + + // Check if it's a built-in + match super::lookup(&word) { + Some(_) => { + output = format!("{}: shell built-in command\n", word); + if verse.couplet > 0 { + out.append(&mut output.as_bytes().to_vec()); + } else { + print!("{}", output); + } + continue; + } + None => {} + } + + // Manually check the path + let mut verb = Verse::new(); + verb.push(word.clone()); + match verb.spellcheck(&env.bins) { + Some(i) => { + output = format!("{}\n", env.bins[i]); + if verse.couplet > 0 { + out.append(&mut output.as_bytes().to_vec()); + } else { + print!("{}", output); + } + } + None => { + output = format!("{} not found\n", word); + status = 1; + eprint!("{}", output); + } + } + } + } + None => { + eprintln!("which: not enough arguments"); + return 1; + } + } + status +} |