Batch of moving commands off async_stream #3 (#1919)

* Batch of moving commands off async_stream #3

* remove commented-out section

* merge master
This commit is contained in:
Jonathan Turner 2020-05-31 06:31:50 +12:00 committed by GitHub
parent 741d7b9f10
commit 4bdf27b173
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 76 additions and 52 deletions

View File

@ -909,6 +909,7 @@ async fn process_line(
shell_manager: ctx.shell_manager.clone(), shell_manager: ctx.shell_manager.clone(),
host: ctx.host.clone(), host: ctx.host.clone(),
ctrl_c: ctx.ctrl_c.clone(), ctrl_c: ctx.ctrl_c.clone(),
current_errors: ctx.current_errors.clone(),
registry: ctx.registry.clone(), registry: ctx.registry.clone(),
name: Tag::unknown(), name: Tag::unknown(),
raw_input: line.to_string(), raw_input: line.to_string(),

View File

@ -5,6 +5,7 @@ use crate::prelude::*;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{hir, hir::Expression, hir::Literal, hir::SpannedExpression}; use nu_protocol::{hir, hir::Expression, hir::Literal, hir::SpannedExpression};
use nu_protocol::{Primitive, Scope, Signature, UntaggedValue, Value}; use nu_protocol::{Primitive, Scope, Signature, UntaggedValue, Value};
use parking_lot::Mutex;
use prettytable::format::{FormatBuilder, LinePosition, LineSeparator}; use prettytable::format::{FormatBuilder, LinePosition, LineSeparator};
use prettytable::{color, Attr, Cell, Row, Table}; use prettytable::{color, Attr, Cell, Row, Table};
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;
@ -38,6 +39,7 @@ impl WholeStreamCommand for Autoview {
shell_manager: args.shell_manager, shell_manager: args.shell_manager,
host: args.host, host: args.host,
ctrl_c: args.ctrl_c, ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag, name: args.call_info.name_tag,
raw_input: args.raw_input, raw_input: args.raw_input,
}) })
@ -63,6 +65,7 @@ impl WholeStreamCommand for Autoview {
pub struct RunnableContextWithoutInput { pub struct RunnableContextWithoutInput {
pub shell_manager: ShellManager, pub shell_manager: ShellManager,
pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>, pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>,
pub current_errors: Arc<Mutex<Vec<ShellError>>>,
pub ctrl_c: Arc<AtomicBool>, pub ctrl_c: Arc<AtomicBool>,
pub registry: CommandRegistry, pub registry: CommandRegistry,
pub name: Tag, pub name: Tag,
@ -74,6 +77,7 @@ impl RunnableContextWithoutInput {
shell_manager: context.shell_manager, shell_manager: context.shell_manager,
host: context.host, host: context.host,
ctrl_c: context.ctrl_c, ctrl_c: context.ctrl_c,
current_errors: context.current_errors,
registry: context.registry, registry: context.registry,
name: context.name, name: context.name,
}; };
@ -389,6 +393,7 @@ fn create_default_command_args(context: &RunnableContextWithoutInput) -> RawComm
RawCommandArgs { RawCommandArgs {
host: context.host.clone(), host: context.host.clone(),
ctrl_c: context.ctrl_c.clone(), ctrl_c: context.ctrl_c.clone(),
current_errors: context.current_errors.clone(),
shell_manager: context.shell_manager.clone(), shell_manager: context.shell_manager.clone(),
call_info: UnevaluatedCallInfo { call_info: UnevaluatedCallInfo {
args: hir::Call { args: hir::Call {

View File

@ -38,6 +38,7 @@ impl WholeStreamCommand for Average {
shell_manager: args.shell_manager, shell_manager: args.shell_manager,
host: args.host, host: args.host,
ctrl_c: args.ctrl_c, ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag, name: args.call_info.name_tag,
raw_input: args.raw_input, raw_input: args.raw_input,
}) })

View File

@ -66,6 +66,7 @@ pub(crate) async fn run_internal_command(
let new_args = RawCommandArgs { let new_args = RawCommandArgs {
host: context.host.clone(), host: context.host.clone(),
ctrl_c: context.ctrl_c.clone(), ctrl_c: context.ctrl_c.clone(),
current_errors: context.current_errors.clone(),
shell_manager: context.shell_manager.clone(), shell_manager: context.shell_manager.clone(),
call_info: UnevaluatedCallInfo { call_info: UnevaluatedCallInfo {
args: nu_protocol::hir::Call { args: nu_protocol::hir::Call {

View File

@ -8,6 +8,7 @@ use getset::Getters;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::hir; use nu_protocol::hir;
use nu_protocol::{CallInfo, EvaluatedArgs, ReturnSuccess, Scope, Signature, UntaggedValue, Value}; use nu_protocol::{CallInfo, EvaluatedArgs, ReturnSuccess, Scope, Signature, UntaggedValue, Value};
use parking_lot::Mutex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::ops::Deref; use std::ops::Deref;
use std::sync::atomic::AtomicBool; use std::sync::atomic::AtomicBool;
@ -54,6 +55,7 @@ impl UnevaluatedCallInfo {
pub struct CommandArgs { pub struct CommandArgs {
pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>, pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>,
pub ctrl_c: Arc<AtomicBool>, pub ctrl_c: Arc<AtomicBool>,
pub current_errors: Arc<Mutex<Vec<ShellError>>>,
pub shell_manager: ShellManager, pub shell_manager: ShellManager,
pub call_info: UnevaluatedCallInfo, pub call_info: UnevaluatedCallInfo,
pub input: InputStream, pub input: InputStream,
@ -65,6 +67,7 @@ pub struct CommandArgs {
pub struct RawCommandArgs { pub struct RawCommandArgs {
pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>, pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>,
pub ctrl_c: Arc<AtomicBool>, pub ctrl_c: Arc<AtomicBool>,
pub current_errors: Arc<Mutex<Vec<ShellError>>>,
pub shell_manager: ShellManager, pub shell_manager: ShellManager,
pub call_info: UnevaluatedCallInfo, pub call_info: UnevaluatedCallInfo,
} }
@ -74,6 +77,7 @@ impl RawCommandArgs {
CommandArgs { CommandArgs {
host: self.host, host: self.host,
ctrl_c: self.ctrl_c, ctrl_c: self.ctrl_c,
current_errors: self.current_errors,
shell_manager: self.shell_manager, shell_manager: self.shell_manager,
call_info: self.call_info, call_info: self.call_info,
input: input.into(), input: input.into(),
@ -151,6 +155,7 @@ pub struct RunnableContext {
pub shell_manager: ShellManager, pub shell_manager: ShellManager,
pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>, pub host: Arc<parking_lot::Mutex<Box<dyn Host>>>,
pub ctrl_c: Arc<AtomicBool>, pub ctrl_c: Arc<AtomicBool>,
pub current_errors: Arc<Mutex<Vec<ShellError>>>,
pub registry: CommandRegistry, pub registry: CommandRegistry,
pub name: Tag, pub name: Tag,
pub raw_input: String, pub raw_input: String,
@ -342,10 +347,9 @@ impl Command {
if args.call_info.switch_present("help") { if args.call_info.switch_present("help") {
let cl = self.0.clone(); let cl = self.0.clone();
let registry = registry.clone(); let registry = registry.clone();
let stream = async_stream! { OutputStream::one(Ok(ReturnSuccess::Value(
yield Ok(ReturnSuccess::Value(UntaggedValue::string(get_help(&*cl, &registry)).into_value(Tag::unknown()))); UntaggedValue::string(get_help(&*cl, &registry)).into_value(Tag::unknown()),
}; )))
stream.to_output_stream()
} else { } else {
match self.0.run(args, registry).await { match self.0.run(args, registry).await {
Ok(stream) => stream, Ok(stream) => stream,

View File

@ -6,8 +6,8 @@ use crate::prelude::*;
use futures::stream::once; use futures::stream::once;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ use nu_protocol::{
hir::Block, hir::Expression, hir::SpannedExpression, hir::Synthetic, ReturnSuccess, Signature, hir::Block, hir::Expression, hir::SpannedExpression, hir::Synthetic, Scope, Signature,
SyntaxShape, UntaggedValue, SyntaxShape, UntaggedValue, Value,
}; };
pub struct Each; pub struct Each;
@ -40,7 +40,7 @@ impl WholeStreamCommand for Each {
args: CommandArgs, args: CommandArgs,
registry: &CommandRegistry, registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
each(args, registry) each(args, registry).await
} }
fn examples(&self) -> Vec<Example> { fn examples(&self) -> Vec<Example> {
@ -76,50 +76,56 @@ fn is_expanded_it_usage(head: &SpannedExpression) -> bool {
} }
} }
fn each(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> { async fn process_row(
let registry = registry.clone(); block: Arc<Block>,
let stream = async_stream! { scope: Arc<Scope>,
let head = raw_args.call_info.args.head.clone(); head: Arc<Box<SpannedExpression>>,
let scope = raw_args.call_info.scope.clone(); mut context: Arc<Context>,
let mut context = Context::from_raw(&raw_args, &registry); input: Value,
let (each_args, mut input): (EachArgs, _) = raw_args.process(&registry).await?; ) -> Result<OutputStream, ShellError> {
let block = each_args.block;
while let Some(input) = input.next().await {
let input_clone = input.clone(); let input_clone = input.clone();
let input_stream = if is_expanded_it_usage(&head) { let input_stream = if is_expanded_it_usage(&head) {
InputStream::empty() InputStream::empty()
} else { } else {
once(async { Ok(input_clone) }).to_input_stream() once(async { Ok(input_clone) }).to_input_stream()
}; };
Ok(run_block(
let result = run_block(
&block, &block,
&mut context, Arc::make_mut(&mut context),
input_stream, input_stream,
&input, &input,
&scope.vars, &scope.vars,
&scope.env &scope.env,
).await; )
.await?
.to_output_stream())
}
match result { async fn each(
Ok(mut stream) => { raw_args: CommandArgs,
while let Some(result) = stream.next().await { registry: &CommandRegistry,
yield Ok(ReturnSuccess::Value(result)); ) -> Result<OutputStream, ShellError> {
} let registry = registry.clone();
let head = Arc::new(raw_args.call_info.args.head.clone());
let errors = context.get_errors(); let scope = Arc::new(raw_args.call_info.scope.clone());
if let Some(error) = errors.first() { let context = Arc::new(Context::from_raw(&raw_args, &registry));
yield Err(error.clone()); let (each_args, input): (EachArgs, _) = raw_args.process(&registry).await?;
let block = Arc::new(each_args.block);
Ok(input
.then(move |input| {
let block = block.clone();
let scope = scope.clone();
let head = head.clone();
let context = context.clone();
async {
match process_row(block, scope, head, context, input).await {
Ok(s) => s,
Err(e) => OutputStream::one(Err(e)),
} }
} }
Err(e) => { })
yield Err(e); .flatten()
} .to_output_stream())
}
}
};
Ok(stream.to_output_stream())
} }
#[cfg(test)] #[cfg(test)]

View File

@ -65,6 +65,7 @@ fn enter(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
let shell_manager = raw_args.shell_manager.clone(); let shell_manager = raw_args.shell_manager.clone();
let head = raw_args.call_info.args.head.clone(); let head = raw_args.call_info.args.head.clone();
let ctrl_c = raw_args.ctrl_c.clone(); let ctrl_c = raw_args.ctrl_c.clone();
let current_errors = raw_args.current_errors.clone();
let host = raw_args.host.clone(); let host = raw_args.host.clone();
let tag = raw_args.call_info.name_tag.clone(); let tag = raw_args.call_info.name_tag.clone();
let (EnterArgs { location }, _) = raw_args.process(&registry).await?; let (EnterArgs { location }, _) = raw_args.process(&registry).await?;
@ -116,6 +117,7 @@ fn enter(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
let new_args = RawCommandArgs { let new_args = RawCommandArgs {
host, host,
ctrl_c, ctrl_c,
current_errors,
shell_manager, shell_manager,
call_info: UnevaluatedCallInfo { call_info: UnevaluatedCallInfo {
args: nu_protocol::hir::Call { args: nu_protocol::hir::Call {

View File

@ -166,6 +166,7 @@ fn save(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
let registry = registry.clone(); let registry = registry.clone();
let host = raw_args.host.clone(); let host = raw_args.host.clone();
let ctrl_c = raw_args.ctrl_c.clone(); let ctrl_c = raw_args.ctrl_c.clone();
let current_errors = raw_args.current_errors.clone();
let shell_manager = raw_args.shell_manager.clone(); let shell_manager = raw_args.shell_manager.clone();
let stream = async_stream! { let stream = async_stream! {
@ -220,6 +221,7 @@ fn save(raw_args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStrea
let new_args = RawCommandArgs { let new_args = RawCommandArgs {
host, host,
ctrl_c, ctrl_c,
current_errors,
shell_manager, shell_manager,
call_info: UnevaluatedCallInfo { call_info: UnevaluatedCallInfo {
args: nu_protocol::hir::Call { args: nu_protocol::hir::Call {

View File

@ -34,6 +34,7 @@ impl WholeStreamCommand for Sum {
shell_manager: args.shell_manager, shell_manager: args.shell_manager,
host: args.host, host: args.host,
ctrl_c: args.ctrl_c, ctrl_c: args.ctrl_c,
current_errors: args.current_errors,
name: args.call_info.name_tag, name: args.call_info.name_tag,
raw_input: args.raw_input, raw_input: args.raw_input,
}) })

View File

@ -93,7 +93,7 @@ impl Context {
Context { Context {
registry: registry.clone(), registry: registry.clone(),
host: raw_args.host.clone(), host: raw_args.host.clone(),
current_errors: Arc::new(Mutex::new(vec![])), current_errors: raw_args.current_errors.clone(),
ctrl_c: raw_args.ctrl_c.clone(), ctrl_c: raw_args.ctrl_c.clone(),
shell_manager: raw_args.shell_manager.clone(), shell_manager: raw_args.shell_manager.clone(),
windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())), windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())),
@ -105,7 +105,7 @@ impl Context {
Context { Context {
registry: registry.clone(), registry: registry.clone(),
host: raw_args.host.clone(), host: raw_args.host.clone(),
current_errors: Arc::new(Mutex::new(vec![])), current_errors: raw_args.current_errors.clone(),
ctrl_c: raw_args.ctrl_c.clone(), ctrl_c: raw_args.ctrl_c.clone(),
shell_manager: raw_args.shell_manager.clone(), shell_manager: raw_args.shell_manager.clone(),
raw_input: String::default(), raw_input: String::default(),
@ -119,7 +119,7 @@ impl Context {
Context { Context {
registry: registry.clone(), registry: registry.clone(),
host: args.host.clone(), host: args.host.clone(),
current_errors: Arc::new(Mutex::new(vec![])), current_errors: args.current_errors.clone(),
ctrl_c: args.ctrl_c.clone(), ctrl_c: args.ctrl_c.clone(),
shell_manager: args.shell_manager.clone(), shell_manager: args.shell_manager.clone(),
windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())), windows_drives_previous_cwd: Arc::new(Mutex::new(std::collections::HashMap::new())),
@ -131,7 +131,7 @@ impl Context {
Context { Context {
registry: registry.clone(), registry: registry.clone(),
host: args.host.clone(), host: args.host.clone(),
current_errors: Arc::new(Mutex::new(vec![])), current_errors: args.current_errors.clone(),
ctrl_c: args.ctrl_c.clone(), ctrl_c: args.ctrl_c.clone(),
shell_manager: args.shell_manager.clone(), shell_manager: args.shell_manager.clone(),
raw_input: String::default(), raw_input: String::default(),
@ -255,6 +255,7 @@ impl Context {
CommandArgs { CommandArgs {
host: self.host.clone(), host: self.host.clone(),
ctrl_c: self.ctrl_c.clone(), ctrl_c: self.ctrl_c.clone(),
current_errors: self.current_errors.clone(),
shell_manager: self.shell_manager.clone(), shell_manager: self.shell_manager.clone(),
call_info: self.call_info(args, name_tag, scope), call_info: self.call_info(args, name_tag, scope),
input, input,