mirror of
https://github.com/nushell/nushell
synced 2024-11-14 17:07:07 +00:00
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:
parent
1634d8e087
commit
6dafaa197d
3 changed files with 36 additions and 33 deletions
|
@ -2,7 +2,7 @@ use crate::prelude::*;
|
|||
use nu_ansi_term::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
||||
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
|
||||
use nu_source::Tagged;
|
||||
|
||||
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 code: Option<Tagged<String>> = args.opt(0)?;
|
||||
|
@ -129,9 +129,9 @@ Format: #
|
|||
));
|
||||
}
|
||||
let output = format!("\x1b[{}", e.item);
|
||||
return Ok(ActionStream::one(ReturnSuccess::value(
|
||||
return Ok(OutputStream::one(
|
||||
UntaggedValue::string(output).into_value(e.tag()),
|
||||
)));
|
||||
));
|
||||
}
|
||||
|
||||
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
|
||||
// OCS's need to end with a bell '\x07' char
|
||||
let output = format!("\x1b]{};", o.item);
|
||||
return Ok(ActionStream::one(ReturnSuccess::value(
|
||||
return Ok(OutputStream::one(
|
||||
UntaggedValue::string(output).into_value(o.tag()),
|
||||
)));
|
||||
));
|
||||
}
|
||||
|
||||
if let Some(code) = code {
|
||||
let ansi_code = str_to_ansi(&code.item);
|
||||
|
||||
if let Some(output) = ansi_code {
|
||||
Ok(ActionStream::one(ReturnSuccess::value(
|
||||
Ok(OutputStream::one(
|
||||
UntaggedValue::string(output).into_value(code.tag()),
|
||||
)))
|
||||
))
|
||||
} else {
|
||||
Err(ShellError::labeled_error(
|
||||
"Unknown ansi code",
|
||||
|
|
|
@ -2,19 +2,12 @@ use crate::prelude::*;
|
|||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::ShellTypeName;
|
||||
use nu_protocol::{
|
||||
ColumnPath, Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value,
|
||||
};
|
||||
use nu_protocol::{Primitive, Signature, SyntaxShape, UntaggedValue, Value};
|
||||
use nu_source::Tag;
|
||||
use strip_ansi_escapes::strip;
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Arguments {
|
||||
rest: Vec<ColumnPath>,
|
||||
}
|
||||
|
||||
impl WholeStreamCommand for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"ansi strip"
|
||||
|
@ -31,7 +24,7 @@ impl WholeStreamCommand for SubCommand {
|
|||
"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)
|
||||
}
|
||||
|
||||
|
@ -44,14 +37,16 @@ impl WholeStreamCommand for SubCommand {
|
|||
}
|
||||
}
|
||||
|
||||
fn operate(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
||||
let (Arguments { rest }, input) = args.process()?;
|
||||
let column_paths: Vec<_> = rest;
|
||||
fn operate(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
let args = args.evaluate_once()?;
|
||||
|
||||
Ok(input
|
||||
let column_paths: Vec<_> = args.rest_args()?;
|
||||
|
||||
let result: Vec<Value> = args
|
||||
.input
|
||||
.map(move |v| {
|
||||
if column_paths.is_empty() {
|
||||
ReturnSuccess::value(action(&v, v.tag())?)
|
||||
action(&v, v.tag())
|
||||
} else {
|
||||
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> {
|
||||
|
|
|
@ -50,7 +50,7 @@ impl WholeStreamCommand for Benchmark {
|
|||
"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)
|
||||
}
|
||||
|
||||
|
@ -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 mut context = EvaluationContext::from_args(&args);
|
||||
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 name = generate_free_name(&env);
|
||||
|
@ -88,11 +93,12 @@ fn benchmark(args: CommandArgs) -> Result<ActionStream, ShellError> {
|
|||
|
||||
context.scope.enter_scope();
|
||||
let result = run_block(
|
||||
&block.block,
|
||||
&cmd_args.block.block,
|
||||
&context,
|
||||
input,
|
||||
args.input,
|
||||
ExternalRedirection::StdoutAndStderr,
|
||||
);
|
||||
|
||||
context.scope.exit_scope();
|
||||
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);
|
||||
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
|
||||
// #[cfg(feature = "rich-benchmark")]
|
||||
|
@ -142,10 +148,10 @@ fn benchmark_output<T, Output>(
|
|||
passthrough: Option<CapturedBlock>,
|
||||
tag: T,
|
||||
context: &mut EvaluationContext,
|
||||
) -> Result<ActionStream, ShellError>
|
||||
) -> Result<OutputStream, ShellError>
|
||||
where
|
||||
T: Into<Tag> + Copy,
|
||||
Output: Into<ActionStream>,
|
||||
Output: Into<OutputStream>,
|
||||
{
|
||||
let value = UntaggedValue::Row(Dictionary::from(
|
||||
indexmap
|
||||
|
@ -174,7 +180,7 @@ where
|
|||
|
||||
Ok(block_output.into())
|
||||
} else {
|
||||
let benchmark_output = ActionStream::one(value);
|
||||
let benchmark_output = OutputStream::one(value);
|
||||
Ok(benchmark_output)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue