summaryrefslogtreecommitdiffstats
path: root/src/poem/anthology
diff options
context:
space:
mode:
authorRory Dudley2024-05-20 23:54:01 -0600
committerRory Dudley2024-05-20 23:54:01 -0600
commitdedadcfd30516c40692fe495a6ad10aea7c050de (patch)
treece0f8dcb6546bbd9e0f61a35c88c32c9c6c608ab /src/poem/anthology
parent85a3bd273e927b8dc31aadf263b93ef8fd3b7a39 (diff)
downloaddwarvish-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.
Diffstat (limited to 'src/poem/anthology')
-rw-r--r--src/poem/anthology/which.rs61
1 files changed, 61 insertions, 0 deletions
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
+}