From fedd4c31b0d1c6d036b1105a74b6e6a1f135f2b4 Mon Sep 17 00:00:00 2001 From: Rory Dudley Date: Fri, 14 Jun 2024 20:29:32 -0400 Subject: Add back spellcheck This commit reworks spellcheck() so it is more verbose about what it returns. It also re-introduces the use of spellcheck() in Poem::recite(). Spellcheck now returns a value in the enum, Spelling: FullPath -> Indicates a full path was specified as the verb OnPath -> Indicates the verb is on the $PATH BuiltIn -> Indicates the verb is a built-in command None (Option) -> The verb does not exist This commit also removes some defunct (commented-out) code. --- src/poem/anthology.rs | 41 +++-------------------------------------- 1 file changed, 3 insertions(+), 38 deletions(-) (limited to 'src/poem/anthology.rs') diff --git a/src/poem/anthology.rs b/src/poem/anthology.rs index a35eafb..086864c 100644 --- a/src/poem/anthology.rs +++ b/src/poem/anthology.rs @@ -29,38 +29,6 @@ pub fn lookup(verb: &str) -> Option { INDEX.iter().position(|v| v.to_string() == verb) } -/// Run a builtin command, based on its verb -/// -/// Use [lookup] to check if a verb is in the anthology's index (i.e. is a -/// builtin), then call this with the current verse, the index found by -/// [lookup], and the shell's global environment state. -/// -/// # Example -/// ``` -/// let index = anthology::lookup(verse.verb()); -/// if index.is_some() { -/// anthology::incant(&verse, index.unwrap(), env); -/// } else { -/// // Run an external command -/// ... -/// } -/// ``` -// pub fn incant(verse: &Verse, out: &mut Vec, index: usize, env: &mut Environment) -> i32 { -// // let verb = INDEX[index]; -// // match verb { -// // "alias" => alias::incant(verse, out, &mut env.aliases), -// // "cd" => cd::incant(verse), -// // "exit" => exit::incant(), -// // "export" => export::incant(verse), -// // "source" => source::incant(verse, out, env), -// // "unalias" => alias::unincant(verse, &mut env.aliases), -// // "unset" => export::unincant(verse), -// // "which" => which::incant(verse, out, env), -// // _ => unreachable!(), -// // } -// 0 -// } - #[derive(Debug, PartialEq, Eq, Clone)] pub struct AnthologyStdin { data: Vec, @@ -101,9 +69,9 @@ impl Anthology { /// ``` /// let mut command = Anthology::new("alias"); /// ``` - pub fn new(verb: String) -> Self { + pub fn new(i: usize) -> Self { Anthology { - verb, + verb: INDEX[i].to_string(), clause: None, uin: false, uout: false, @@ -148,11 +116,8 @@ impl Anthology { } pub fn spawn(&mut self, env: &mut Environment) -> Result { - let index = lookup(self.verb.as_str()).unwrap(); - let verb = INDEX[index]; - // Incant the built-in and set the output - self.output = Some(match verb { + self.output = Some(match self.verb.as_str() { "alias" => alias::incant(&self.clause, self.uout, &mut env.aliases), "cd" => cd::incant(&self.clause, self.uerr), "exit" => exit::incant(), -- cgit v1.2.3