Commands to engine p (#3404)

* Change ansi command

* Change ansi strip command

* Change benchmark to engine-p

* ansi strip removed arg.process()

* benchmark without process args
This commit is contained in:
Fernando Herrera 2021-05-11 05:03:55 +01:00 committed by GitHub
parent 1634d8e087
commit 6dafaa197d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 36 additions and 33 deletions

View File

@ -2,7 +2,7 @@ use crate::prelude::*;
use nu_ansi_term::*; use nu_ansi_term::*;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value}; use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
use nu_source::Tagged; use nu_source::Tagged;
pub struct Command; pub struct Command;
@ -112,7 +112,7 @@ Format: #
] ]
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once()?; let args = args.evaluate_once()?;
let code: Option<Tagged<String>> = args.opt(0)?; let code: Option<Tagged<String>> = args.opt(0)?;
@ -129,9 +129,9 @@ Format: #
)); ));
} }
let output = format!("\x1b[{}", e.item); let output = format!("\x1b[{}", e.item);
return Ok(ActionStream::one(ReturnSuccess::value( return Ok(OutputStream::one(
UntaggedValue::string(output).into_value(e.tag()), UntaggedValue::string(output).into_value(e.tag()),
))); ));
} }
if let Some(o) = osc { if let Some(o) = osc {
@ -147,18 +147,18 @@ Format: #
//Operating system command aka osc ESC ] <- note the right brace, not left brace for osc //Operating system command aka osc ESC ] <- note the right brace, not left brace for osc
// OCS's need to end with a bell '\x07' char // OCS's need to end with a bell '\x07' char
let output = format!("\x1b]{};", o.item); let output = format!("\x1b]{};", o.item);
return Ok(ActionStream::one(ReturnSuccess::value( return Ok(OutputStream::one(
UntaggedValue::string(output).into_value(o.tag()), UntaggedValue::string(output).into_value(o.tag()),
))); ));
} }
if let Some(code) = code { if let Some(code) = code {
let ansi_code = str_to_ansi(&code.item); let ansi_code = str_to_ansi(&code.item);
if let Some(output) = ansi_code { if let Some(output) = ansi_code {
Ok(ActionStream::one(ReturnSuccess::value( Ok(OutputStream::one(
UntaggedValue::string(output).into_value(code.tag()), UntaggedValue::string(output).into_value(code.tag()),
))) ))
} else { } else {
Err(ShellError::labeled_error( Err(ShellError::labeled_error(
"Unknown ansi code", "Unknown ansi code",

View File

@ -2,19 +2,12 @@ use crate::prelude::*;
use nu_engine::WholeStreamCommand; use nu_engine::WholeStreamCommand;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::ShellTypeName; use nu_protocol::ShellTypeName;
use nu_protocol::{ use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value};
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
};
use nu_source::Tag; use nu_source::Tag;
use strip_ansi_escapes::strip; use strip_ansi_escapes::strip;
pub struct SubCommand; pub struct SubCommand;
#[derive(Deserialize)]
struct Arguments {
rest: Vec<ColumnPath>,
}
impl WholeStreamCommand for SubCommand { impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str { fn name(&self) -> &str {
"ansi strip" "ansi strip"
@ -31,7 +24,7 @@ impl WholeStreamCommand for SubCommand {
"strip ansi escape sequences from string" "strip ansi escape sequences from string"
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
operate(args) operate(args)
} }
@ -44,14 +37,16 @@ impl WholeStreamCommand for SubCommand {
} }
} }
fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> { fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
let (Arguments { rest }, input) = args.process()?; let args = args.evaluate_once()?;
let column_paths: Vec<_> = rest;
Ok(input let column_paths: Vec<_> = args.rest_args()?;
let result: Vec<Value> = args
.input
.map(move |v| { .map(move |v| {
if column_paths.is_empty() { if column_paths.is_empty() {
ReturnSuccess::value(action(&v, v.tag())?) action(&v, v.tag())
} else { } else {
let mut ret = v; let mut ret = v;
@ -62,10 +57,12 @@ fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> {
)?; )?;
} }
ReturnSuccess::value(ret) Ok(ret)
} }
}) })
.to_action_stream()) .collect::<Result<Vec<Value>, _>>()?;
Ok(OutputStream::from_stream(result.into_iter()))
} }
fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> { fn action(input: &Value, tag: impl Into<Tag>) -> Result<Value, ShellError> {

View File

@ -50,7 +50,7 @@ impl WholeStreamCommand for Benchmark {
"Runs a block and returns the time it took to execute it." "Runs a block and returns the time it took to execute it."
} }
fn run_with_actions(&self, args: CommandArgs) -> Result<ActionStream, ShellError> { fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
benchmark(args) benchmark(args)
} }
@ -70,11 +70,16 @@ impl WholeStreamCommand for Benchmark {
} }
} }
fn benchmark(args: CommandArgs) -> Result<ActionStream, ShellError> { fn benchmark(args: CommandArgs) -> Result<OutputStream, ShellError> {
let tag = args.call_info.args.span; let tag = args.call_info.args.span;
let mut context = EvaluationContext::from_args(&args); let mut context = EvaluationContext::from_args(&args);
let scope = args.scope().clone(); let scope = args.scope().clone();
let (BenchmarkArgs { block, passthrough }, input) = args.process()?;
let args = args.evaluate_once()?;
let cmd_args = BenchmarkArgs {
block: args.req(0)?,
passthrough: args.get_flag("passthrough")?,
};
let env = scope.get_env_vars(); let env = scope.get_env_vars();
let name = generate_free_name(&env); let name = generate_free_name(&env);
@ -88,11 +93,12 @@ fn benchmark(args: CommandArgs) -> Result<ActionStream, ShellError> {
context.scope.enter_scope(); context.scope.enter_scope();
let result = run_block( let result = run_block(
&block.block, &cmd_args.block.block,
&context, &context,
input, args.input,
ExternalRedirection::StdoutAndStderr, ExternalRedirection::StdoutAndStderr,
); );
context.scope.exit_scope(); context.scope.exit_scope();
let output = result?.into_vec(); let output = result?.into_vec();
@ -109,7 +115,7 @@ fn benchmark(args: CommandArgs) -> Result<ActionStream, ShellError> {
let real_time = into_big_int(end_time - start_time); let real_time = into_big_int(end_time - start_time);
indexmap.insert("real time".to_string(), real_time); indexmap.insert("real time".to_string(), real_time);
benchmark_output(indexmap, output, passthrough, &tag, &mut context) benchmark_output(indexmap, output, cmd_args.passthrough, &tag, &mut context)
} }
// return advanced stats // return advanced stats
// #[cfg(feature = "rich-benchmark")] // #[cfg(feature = "rich-benchmark")]
@ -142,10 +148,10 @@ fn benchmark_output<T, Output>(
passthrough: Option<CapturedBlock>, passthrough: Option<CapturedBlock>,
tag: T, tag: T,
context: &mut EvaluationContext, context: &mut EvaluationContext,
) -> Result<ActionStream, ShellError> ) -> Result<OutputStream, ShellError>
where where
T: Into<Tag> + Copy, T: Into<Tag> + Copy,
Output: Into<ActionStream>, Output: Into<OutputStream>,
{ {
let value = UntaggedValue::Row(Dictionary::from( let value = UntaggedValue::Row(Dictionary::from(
indexmap indexmap
@ -174,7 +180,7 @@ where
Ok(block_output.into()) Ok(block_output.into())
} else { } else {
let benchmark_output = ActionStream::one(value); let benchmark_output = OutputStream::one(value);
Ok(benchmark_output) Ok(benchmark_output)
} }
} }