From efd9631a9081a5c1669c4ec1d133c59e0eefecc0 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Thu, 15 Aug 2019 17:46:19 +1200 Subject: [PATCH] All tests pass --- src/commands/command.rs | 74 ++++++++++++++++++++++++++++++++--------- src/commands/open.rs | 2 -- src/parser/registry.rs | 2 -- 3 files changed, 58 insertions(+), 20 deletions(-) diff --git a/src/commands/command.rs b/src/commands/command.rs index 2d06e70af..571bc525d 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -41,6 +41,33 @@ impl UnevaluatedCallInfo { name_span: self.name_span, }) } + + pub fn has_it_or_block(&self) -> bool { + use hir::RawExpression; + use hir::Variable; + + if let Some(positional) = &self.args.positional() { + for pos in positional { + match pos { + Tagged { + item: RawExpression::Variable(Variable::It(_)), + .. + } => { + return true; + } + Tagged { + item: RawExpression::Block(_), + .. + } => { + return true; + } + _ => {} + } + } + } + + false + } } #[derive(Deserialize, Serialize, Debug, Clone)] @@ -465,29 +492,44 @@ impl Command { args: CommandArgs, registry: CommandRegistry, ) -> Result { - println!("raw_args: {:?}", args.call_info); let raw_args = RawCommandArgs { host: args.host, shell_manager: args.shell_manager, call_info: args.call_info, }; - let out = args - .input - .values - .map(move |x| { - let call_info = raw_args - .clone() - .call_info - .evaluate(®istry, &Scope::it_value(x.clone())) - .unwrap(); - command - .run(&call_info, ®istry, &raw_args.shell_manager, x) - .unwrap() - }) - .flatten(); + if raw_args.call_info.has_it_or_block() { + let out = args + .input + .values + .map(move |x| { + let call_info = raw_args + .clone() + .call_info + .evaluate(®istry, &Scope::it_value(x.clone())) + .unwrap(); + match command.run(&call_info, ®istry, &raw_args.shell_manager, x) { + Ok(o) => o, + Err(e) => VecDeque::from(vec![ReturnValue::Err(e)]), + } + }) + .flatten(); - Ok(out.to_output_stream()) + Ok(out.to_output_stream()) + } else { + let nothing = Value::nothing().tagged(Tag::unknown()); + let call_info = raw_args + .clone() + .call_info + .evaluate(®istry, &Scope::it_value(nothing.clone())) + .unwrap(); + // We don't have an $it or block, so just execute what we have + let out = match command.run(&call_info, ®istry, &raw_args.shell_manager, nothing) { + Ok(o) => o, + Err(e) => VecDeque::from(vec![ReturnValue::Err(e)]), + }; + Ok(out.to_output_stream()) + } } } diff --git a/src/commands/open.rs b/src/commands/open.rs index 25edca9d3..a93cdc437 100644 --- a/src/commands/open.rs +++ b/src/commands/open.rs @@ -39,8 +39,6 @@ fn run( let cwd = PathBuf::from(shell_manager.path()); let full_path = PathBuf::from(cwd); - println!("{:?}", call_info.args.nth(0)); - let path = match call_info .args .nth(0) diff --git a/src/parser/registry.rs b/src/parser/registry.rs index b73007d26..90a709da3 100644 --- a/src/parser/registry.rs +++ b/src/parser/registry.rs @@ -290,7 +290,6 @@ crate fn evaluate_args( scope: &Scope, source: &Text, ) -> Result { - println!("positional (before): {:?}", call); let positional: Result>, _> = call .positional() .as_ref() @@ -301,7 +300,6 @@ crate fn evaluate_args( }) .transpose(); - println!("positional: {:?}", positional); let positional = positional?; let named: Result>>, ShellError> = call