From f012eb7bdd30d93b68418cdc2016d14af49dd47f Mon Sep 17 00:00:00 2001 From: Jason Gedge Date: Sun, 3 Nov 2019 20:04:01 -0500 Subject: [PATCH] Eliminate is_first_command by defaulting to Value::nothing() --- src/cli.rs | 10 ++---- src/commands/autoview.rs | 8 ++--- src/commands/classified.rs | 4 +-- src/commands/command.rs | 66 +++++++++++--------------------------- src/commands/enter.rs | 1 - src/commands/fetch.rs | 2 +- src/commands/open.rs | 2 +- src/commands/post.rs | 3 +- src/commands/save.rs | 2 +- src/context.rs | 3 +- 10 files changed, 31 insertions(+), 70 deletions(-) diff --git a/src/cli.rs b/src/cli.rs index fa68346ef..e5231afd1 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -588,9 +588,7 @@ async fn process_line(readline: Result, ctx: &mut Context } let mut input = ClassifiedInputStream::new(); - let mut iter = pipeline.commands.item.into_iter().peekable(); - let mut is_first_command = true; // Check the config to see if we need to update the path // TODO: make sure config is cached so we don't path this load every call @@ -628,20 +626,20 @@ async fn process_line(readline: Result, ctx: &mut Context ( Some(ClassifiedCommand::Internal(left)), Some(ClassifiedCommand::External(_)), - ) => match left.run(ctx, input, Text::from(line), is_first_command) { + ) => match left.run(ctx, input, Text::from(line)) { Ok(val) => ClassifiedInputStream::from_input_stream(val), Err(err) => return LineResult::Error(line.to_string(), err), }, (Some(ClassifiedCommand::Internal(left)), Some(_)) => { - match left.run(ctx, input, Text::from(line), is_first_command) { + match left.run(ctx, input, Text::from(line)) { Ok(val) => ClassifiedInputStream::from_input_stream(val), Err(err) => return LineResult::Error(line.to_string(), err), } } (Some(ClassifiedCommand::Internal(left)), None) => { - match left.run(ctx, input, Text::from(line), is_first_command) { + match left.run(ctx, input, Text::from(line)) { Ok(val) => { use futures::stream::TryStreamExt; @@ -693,8 +691,6 @@ async fn process_line(readline: Result, ctx: &mut Context } } }; - - is_first_command = false; } LineResult::Success(line.to_string()) diff --git a/src/commands/autoview.rs b/src/commands/autoview.rs index 4f7d7172a..774dfcf88 100644 --- a/src/commands/autoview.rs +++ b/src/commands/autoview.rs @@ -96,7 +96,7 @@ pub fn autoview( named_args.insert_optional("start_number", Some(Expression::number(current_idx, Tag::unknown()))); command_args.call_info.args.named = Some(named_args); - let result = table.run(command_args, &context.commands, false); + let result = table.run(command_args, &context.commands); result.collect::>().await; if finished { @@ -117,7 +117,7 @@ pub fn autoview( if let Some(text) = text { let mut stream = VecDeque::new(); stream.push_back(Value::string(s).tagged(Tag { anchor, span })); - let result = text.run(raw.with_input(stream.into()), &context.commands, false); + let result = text.run(raw.with_input(stream.into()), &context.commands); result.collect::>().await; } else { println!("{}", s); @@ -134,7 +134,7 @@ pub fn autoview( if let Some(binary) = binary { let mut stream = VecDeque::new(); stream.push_back(x.clone()); - let result = binary.run(raw.with_input(stream.into()), &context.commands, false); + let result = binary.run(raw.with_input(stream.into()), &context.commands); result.collect::>().await; } else { use pretty_hex::*; @@ -149,7 +149,7 @@ pub fn autoview( if let Some(table) = table { let mut stream = VecDeque::new(); stream.push_back(x.clone()); - let result = table.run(raw.with_input(stream.into()), &context.commands, false); + let result = table.run(raw.with_input(stream.into()), &context.commands); result.collect::>().await; } else { println!("{:?}", item); diff --git a/src/commands/classified.rs b/src/commands/classified.rs index e69426462..0691a68a3 100644 --- a/src/commands/classified.rs +++ b/src/commands/classified.rs @@ -54,7 +54,7 @@ pub(crate) struct ClassifiedInputStream { impl ClassifiedInputStream { pub(crate) fn new() -> ClassifiedInputStream { ClassifiedInputStream { - objects: VecDeque::new().into(), + objects: vec![Value::nothing().tagged(Tag::unknown())].into(), stdin: None, } } @@ -158,7 +158,6 @@ impl InternalCommand { context: &mut Context, input: ClassifiedInputStream, source: Text, - is_first_command: bool, ) -> Result { if log_enabled!(log::Level::Trace) { trace!(target: "nu::run::internal", "->"); @@ -178,7 +177,6 @@ impl InternalCommand { self.args.item, &source, objects, - is_first_command, ) }; diff --git a/src/commands/command.rs b/src/commands/command.rs index 73b14ca25..2dc69df9c 100644 --- a/src/commands/command.rs +++ b/src/commands/command.rs @@ -544,20 +544,13 @@ impl Command { } } - pub fn run( - &self, - args: CommandArgs, - registry: ®istry::CommandRegistry, - is_first_command: bool, - ) -> OutputStream { + pub fn run(&self, args: CommandArgs, registry: ®istry::CommandRegistry) -> OutputStream { match self { Command::WholeStream(command) => match command.run(args, registry) { Ok(stream) => stream, Err(err) => OutputStream::one(Err(err)), }, - Command::PerItem(command) => { - self.run_helper(command.clone(), args, registry.clone(), is_first_command) - } + Command::PerItem(command) => self.run_helper(command.clone(), args, registry.clone()), } } @@ -566,7 +559,6 @@ impl Command { command: Arc, args: CommandArgs, registry: CommandRegistry, - is_first_command: bool, ) -> OutputStream { let raw_args = RawCommandArgs { host: args.host, @@ -575,45 +567,23 @@ impl Command { call_info: args.call_info, }; - if !is_first_command { - 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, x) { - Ok(o) => o, - Err(e) => VecDeque::from(vec![ReturnValue::Err(e)]).to_output_stream(), - } - }) - .flatten(); - - 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())); - - match call_info { - Ok(call_info) => { - match command - .run(&call_info, ®istry, &raw_args, nothing) - .into() - { - Ok(o) => o, - Err(e) => OutputStream::one(Err(e)), - } + 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, x) { + Ok(o) => o, + Err(e) => VecDeque::from(vec![ReturnValue::Err(e)]).to_output_stream(), } - Err(e) => OutputStream::one(Err(e)), - } - } + }) + .flatten(); + + out.to_output_stream() } pub fn is_binary(&self) -> bool { diff --git a/src/commands/enter.rs b/src/commands/enter.rs index 59f7ca0f2..2153a0a08 100644 --- a/src/commands/enter.rs +++ b/src/commands/enter.rs @@ -105,7 +105,6 @@ impl PerItemCommand for Enter { let mut result = converter.run( new_args.with_input(vec![tagged_contents]), ®istry, - false ); let result_vec: Vec> = result.drain_vec().await; diff --git a/src/commands/fetch.rs b/src/commands/fetch.rs index 703c3279c..8fe3ca247 100644 --- a/src/commands/fetch.rs +++ b/src/commands/fetch.rs @@ -100,7 +100,7 @@ fn run( name_tag: raw_args.call_info.name_tag, } }; - let mut result = converter.run(new_args.with_input(vec![tagged_contents]), ®istry, false); + let mut result = converter.run(new_args.with_input(vec![tagged_contents]), ®istry); let result_vec: Vec> = result.drain_vec().await; for res in result_vec { match res { diff --git a/src/commands/open.rs b/src/commands/open.rs index 19c7d539e..3d7e066a2 100644 --- a/src/commands/open.rs +++ b/src/commands/open.rs @@ -101,7 +101,7 @@ fn run( name_tag: raw_args.call_info.name_tag, } }; - let mut result = converter.run(new_args.with_input(vec![tagged_contents]), ®istry, false); + let mut result = converter.run(new_args.with_input(vec![tagged_contents]), ®istry); let result_vec: Vec> = result.drain_vec().await; for res in result_vec { match res { diff --git a/src/commands/post.rs b/src/commands/post.rs index eb06cdbae..92e4e8f13 100644 --- a/src/commands/post.rs +++ b/src/commands/post.rs @@ -124,7 +124,7 @@ fn run( name_tag: raw_args.call_info.name_tag, } }; - let mut result = converter.run(new_args.with_input(vec![tagged_contents]), ®istry, false); + let mut result = converter.run(new_args.with_input(vec![tagged_contents]), ®istry); let result_vec: Vec> = result.drain_vec().await; for res in result_vec { match res { @@ -270,7 +270,6 @@ pub async fn post( let mut result = converter.run( new_args.with_input(vec![item.clone().tagged(tag.clone())]), ®istry, - false, ); let result_vec: Vec> = result.drain_vec().await; diff --git a/src/commands/save.rs b/src/commands/save.rs index 45063dca4..48cfa1acc 100644 --- a/src/commands/save.rs +++ b/src/commands/save.rs @@ -193,7 +193,7 @@ fn save( name_tag: raw_args.call_info.name_tag, } }; - let mut result = converter.run(new_args.with_input(input), ®istry, false); + let mut result = converter.run(new_args.with_input(input), ®istry); let result_vec: Vec> = result.drain_vec().await; if converter.is_binary() { process_binary_return_success!('scope, result_vec, name_tag) diff --git a/src/context.rs b/src/context.rs index 6983f467a..fe7d99319 100644 --- a/src/context.rs +++ b/src/context.rs @@ -112,10 +112,9 @@ impl Context { args: hir::Call, source: &Text, input: InputStream, - is_first_command: bool, ) -> OutputStream { let command_args = self.command_args(args, input, source, name_tag); - command.run(command_args, self.registry(), is_first_command) + command.run(command_args, self.registry()) } fn call_info(&self, args: hir::Call, source: &Text, name_tag: Tag) -> UnevaluatedCallInfo {