summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/poem/anthology.rs6
-rw-r--r--src/poem/anthology/which.rs61
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
+}