Eliminate is_first_command by defaulting to Value::nothing()

This commit is contained in:
Jason Gedge 2019-11-03 20:04:01 -05:00
parent f966394b63
commit f012eb7bdd
10 changed files with 31 additions and 70 deletions

View file

@ -588,9 +588,7 @@ async fn process_line(readline: Result<String, ReadlineError>, ctx: &mut Context
} }
let mut input = ClassifiedInputStream::new(); let mut input = ClassifiedInputStream::new();
let mut iter = pipeline.commands.item.into_iter().peekable(); 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 // 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 // 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<String, ReadlineError>, ctx: &mut Context
( (
Some(ClassifiedCommand::Internal(left)), Some(ClassifiedCommand::Internal(left)),
Some(ClassifiedCommand::External(_)), 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), Ok(val) => ClassifiedInputStream::from_input_stream(val),
Err(err) => return LineResult::Error(line.to_string(), err), Err(err) => return LineResult::Error(line.to_string(), err),
}, },
(Some(ClassifiedCommand::Internal(left)), Some(_)) => { (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), Ok(val) => ClassifiedInputStream::from_input_stream(val),
Err(err) => return LineResult::Error(line.to_string(), err), Err(err) => return LineResult::Error(line.to_string(), err),
} }
} }
(Some(ClassifiedCommand::Internal(left)), None) => { (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) => { Ok(val) => {
use futures::stream::TryStreamExt; use futures::stream::TryStreamExt;
@ -693,8 +691,6 @@ async fn process_line(readline: Result<String, ReadlineError>, ctx: &mut Context
} }
} }
}; };
is_first_command = false;
} }
LineResult::Success(line.to_string()) LineResult::Success(line.to_string())

View file

@ -96,7 +96,7 @@ pub fn autoview(
named_args.insert_optional("start_number", Some(Expression::number(current_idx, Tag::unknown()))); named_args.insert_optional("start_number", Some(Expression::number(current_idx, Tag::unknown())));
command_args.call_info.args.named = Some(named_args); 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::<Vec<_>>().await; result.collect::<Vec<_>>().await;
if finished { if finished {
@ -117,7 +117,7 @@ pub fn autoview(
if let Some(text) = text { if let Some(text) = text {
let mut stream = VecDeque::new(); let mut stream = VecDeque::new();
stream.push_back(Value::string(s).tagged(Tag { anchor, span })); 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::<Vec<_>>().await; result.collect::<Vec<_>>().await;
} else { } else {
println!("{}", s); println!("{}", s);
@ -134,7 +134,7 @@ pub fn autoview(
if let Some(binary) = binary { if let Some(binary) = binary {
let mut stream = VecDeque::new(); let mut stream = VecDeque::new();
stream.push_back(x.clone()); 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::<Vec<_>>().await; result.collect::<Vec<_>>().await;
} else { } else {
use pretty_hex::*; use pretty_hex::*;
@ -149,7 +149,7 @@ pub fn autoview(
if let Some(table) = table { if let Some(table) = table {
let mut stream = VecDeque::new(); let mut stream = VecDeque::new();
stream.push_back(x.clone()); 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::<Vec<_>>().await; result.collect::<Vec<_>>().await;
} else { } else {
println!("{:?}", item); println!("{:?}", item);

View file

@ -54,7 +54,7 @@ pub(crate) struct ClassifiedInputStream {
impl ClassifiedInputStream { impl ClassifiedInputStream {
pub(crate) fn new() -> ClassifiedInputStream { pub(crate) fn new() -> ClassifiedInputStream {
ClassifiedInputStream { ClassifiedInputStream {
objects: VecDeque::new().into(), objects: vec![Value::nothing().tagged(Tag::unknown())].into(),
stdin: None, stdin: None,
} }
} }
@ -158,7 +158,6 @@ impl InternalCommand {
context: &mut Context, context: &mut Context,
input: ClassifiedInputStream, input: ClassifiedInputStream,
source: Text, source: Text,
is_first_command: bool,
) -> Result<InputStream, ShellError> { ) -> Result<InputStream, ShellError> {
if log_enabled!(log::Level::Trace) { if log_enabled!(log::Level::Trace) {
trace!(target: "nu::run::internal", "->"); trace!(target: "nu::run::internal", "->");
@ -178,7 +177,6 @@ impl InternalCommand {
self.args.item, self.args.item,
&source, &source,
objects, objects,
is_first_command,
) )
}; };

View file

@ -544,20 +544,13 @@ impl Command {
} }
} }
pub fn run( pub fn run(&self, args: CommandArgs, registry: &registry::CommandRegistry) -> OutputStream {
&self,
args: CommandArgs,
registry: &registry::CommandRegistry,
is_first_command: bool,
) -> OutputStream {
match self { match self {
Command::WholeStream(command) => match command.run(args, registry) { Command::WholeStream(command) => match command.run(args, registry) {
Ok(stream) => stream, Ok(stream) => stream,
Err(err) => OutputStream::one(Err(err)), Err(err) => OutputStream::one(Err(err)),
}, },
Command::PerItem(command) => { Command::PerItem(command) => self.run_helper(command.clone(), args, registry.clone()),
self.run_helper(command.clone(), args, registry.clone(), is_first_command)
}
} }
} }
@ -566,7 +559,6 @@ impl Command {
command: Arc<dyn PerItemCommand>, command: Arc<dyn PerItemCommand>,
args: CommandArgs, args: CommandArgs,
registry: CommandRegistry, registry: CommandRegistry,
is_first_command: bool,
) -> OutputStream { ) -> OutputStream {
let raw_args = RawCommandArgs { let raw_args = RawCommandArgs {
host: args.host, host: args.host,
@ -575,7 +567,6 @@ impl Command {
call_info: args.call_info, call_info: args.call_info,
}; };
if !is_first_command {
let out = args let out = args
.input .input
.values .values
@ -593,27 +584,6 @@ impl Command {
.flatten(); .flatten();
out.to_output_stream() out.to_output_stream()
} else {
let nothing = Value::nothing().tagged(Tag::unknown());
let call_info = raw_args
.clone()
.call_info
.evaluate(&registry, &Scope::it_value(nothing.clone()));
match call_info {
Ok(call_info) => {
match command
.run(&call_info, &registry, &raw_args, nothing)
.into()
{
Ok(o) => o,
Err(e) => OutputStream::one(Err(e)),
}
}
Err(e) => OutputStream::one(Err(e)),
}
}
} }
pub fn is_binary(&self) -> bool { pub fn is_binary(&self) -> bool {

View file

@ -105,7 +105,6 @@ impl PerItemCommand for Enter {
let mut result = converter.run( let mut result = converter.run(
new_args.with_input(vec![tagged_contents]), new_args.with_input(vec![tagged_contents]),
&registry, &registry,
false
); );
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = let result_vec: Vec<Result<ReturnSuccess, ShellError>> =
result.drain_vec().await; result.drain_vec().await;

View file

@ -100,7 +100,7 @@ fn run(
name_tag: raw_args.call_info.name_tag, name_tag: raw_args.call_info.name_tag,
} }
}; };
let mut result = converter.run(new_args.with_input(vec![tagged_contents]), &registry, false); let mut result = converter.run(new_args.with_input(vec![tagged_contents]), &registry);
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await; let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await;
for res in result_vec { for res in result_vec {
match res { match res {

View file

@ -101,7 +101,7 @@ fn run(
name_tag: raw_args.call_info.name_tag, name_tag: raw_args.call_info.name_tag,
} }
}; };
let mut result = converter.run(new_args.with_input(vec![tagged_contents]), &registry, false); let mut result = converter.run(new_args.with_input(vec![tagged_contents]), &registry);
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await; let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await;
for res in result_vec { for res in result_vec {
match res { match res {

View file

@ -124,7 +124,7 @@ fn run(
name_tag: raw_args.call_info.name_tag, name_tag: raw_args.call_info.name_tag,
} }
}; };
let mut result = converter.run(new_args.with_input(vec![tagged_contents]), &registry, false); let mut result = converter.run(new_args.with_input(vec![tagged_contents]), &registry);
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await; let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await;
for res in result_vec { for res in result_vec {
match res { match res {
@ -270,7 +270,6 @@ pub async fn post(
let mut result = converter.run( let mut result = converter.run(
new_args.with_input(vec![item.clone().tagged(tag.clone())]), new_args.with_input(vec![item.clone().tagged(tag.clone())]),
&registry, &registry,
false,
); );
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = let result_vec: Vec<Result<ReturnSuccess, ShellError>> =
result.drain_vec().await; result.drain_vec().await;

View file

@ -193,7 +193,7 @@ fn save(
name_tag: raw_args.call_info.name_tag, name_tag: raw_args.call_info.name_tag,
} }
}; };
let mut result = converter.run(new_args.with_input(input), &registry, false); let mut result = converter.run(new_args.with_input(input), &registry);
let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await; let result_vec: Vec<Result<ReturnSuccess, ShellError>> = result.drain_vec().await;
if converter.is_binary() { if converter.is_binary() {
process_binary_return_success!('scope, result_vec, name_tag) process_binary_return_success!('scope, result_vec, name_tag)

View file

@ -112,10 +112,9 @@ impl Context {
args: hir::Call, args: hir::Call,
source: &Text, source: &Text,
input: InputStream, input: InputStream,
is_first_command: bool,
) -> OutputStream { ) -> OutputStream {
let command_args = self.command_args(args, input, source, name_tag); 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 { fn call_info(&self, args: hir::Call, source: &Text, name_tag: Tag) -> UnevaluatedCallInfo {