forked from extern/nushell
Make timeit work with command calls (#8594)
# Description Allows `timeit` to also run commands directly, eg) `timeit ls -la` # User-Facing Changes Additional capabilities to `timeit`. # Tests + Formatting Don't forget to add tests that cover your changes. Make sure you've run and fixed any issues with these commands: - `cargo fmt --all -- --check` to check standard code formatting (`cargo fmt --all` applies these changes) - `cargo clippy --workspace -- -D warnings -D clippy::unwrap_used -A clippy::needless_collect` to check that you're using the standard code style - `cargo test --workspace` to check that all tests pass > **Note** > from `nushell` you can also use the `toolkit` as follows > ```bash > use toolkit.nu # or use an `env_change` hook to activate it automatically > toolkit check pr > ``` # After Submitting If your PR had any user-facing changes, update [the documentation](https://github.com/nushell/nushell.github.io) after the PR is merged, if necessary. This will help us keep the docs up to date.
This commit is contained in:
parent
546c753d1e
commit
85bfdba1e2
@ -1,7 +1,7 @@
|
|||||||
use nu_engine::{eval_block, CallExt};
|
use nu_engine::{eval_block, eval_expression_with_input};
|
||||||
use nu_protocol::{
|
use nu_protocol::{
|
||||||
ast::Call,
|
ast::Call,
|
||||||
engine::{Block, Command, EngineState, Stack},
|
engine::{Command, EngineState, Stack},
|
||||||
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type,
|
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type,
|
||||||
Value,
|
Value,
|
||||||
};
|
};
|
||||||
@ -21,7 +21,11 @@ impl Command for TimeIt {
|
|||||||
|
|
||||||
fn signature(&self) -> nu_protocol::Signature {
|
fn signature(&self) -> nu_protocol::Signature {
|
||||||
Signature::build("timeit")
|
Signature::build("timeit")
|
||||||
.required("block", SyntaxShape::Block, "the block to run")
|
.required(
|
||||||
|
"command",
|
||||||
|
SyntaxShape::OneOf(vec![SyntaxShape::Block, SyntaxShape::Expression]),
|
||||||
|
"the command or block to run",
|
||||||
|
)
|
||||||
.input_output_types(vec![
|
.input_output_types(vec![
|
||||||
(Type::Any, Type::Duration),
|
(Type::Any, Type::Duration),
|
||||||
(Type::Nothing, Type::Duration),
|
(Type::Nothing, Type::Duration),
|
||||||
@ -41,37 +45,36 @@ impl Command for TimeIt {
|
|||||||
call: &Call,
|
call: &Call,
|
||||||
input: PipelineData,
|
input: PipelineData,
|
||||||
) -> Result<PipelineData, ShellError> {
|
) -> Result<PipelineData, ShellError> {
|
||||||
let capture_block: Block = call.req(engine_state, stack, 0)?;
|
let command_to_run = call.positional_nth(0);
|
||||||
let block = engine_state.get_block(capture_block.block_id);
|
|
||||||
|
|
||||||
let redirect_stdout = call.redirect_stdout;
|
|
||||||
let redirect_stderr = call.redirect_stderr;
|
|
||||||
|
|
||||||
// let mut stack = stack.captures_to_stack(&capture_block.captures);
|
|
||||||
|
|
||||||
// In order to provide the pipeline as a positional, it must be converted into a value.
|
|
||||||
// But because pipelines do not have Clone, this one has to be cloned as a value
|
|
||||||
// and then converted back into a pipeline for eval_block().
|
|
||||||
// So, the metadata must be saved here and restored at that point.
|
|
||||||
let input_metadata = input.metadata();
|
|
||||||
let input_val = input.into_value(call.head);
|
|
||||||
|
|
||||||
if let Some(var) = block.signature.get_positional(0) {
|
|
||||||
if let Some(var_id) = &var.var_id {
|
|
||||||
stack.add_var(*var_id, input_val.clone());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Get the start time after all other computation has been done.
|
// Get the start time after all other computation has been done.
|
||||||
let start_time = Instant::now();
|
let start_time = Instant::now();
|
||||||
eval_block(
|
|
||||||
engine_state,
|
if let Some(command_to_run) = command_to_run {
|
||||||
stack,
|
if let Some(block_id) = command_to_run.as_block() {
|
||||||
block,
|
let block = engine_state.get_block(block_id);
|
||||||
input_val.into_pipeline_data_with_metadata(input_metadata),
|
eval_block(
|
||||||
redirect_stdout,
|
engine_state,
|
||||||
redirect_stderr,
|
stack,
|
||||||
)?
|
block,
|
||||||
|
input,
|
||||||
|
call.redirect_stdout,
|
||||||
|
call.redirect_stderr,
|
||||||
|
)?
|
||||||
|
} else {
|
||||||
|
eval_expression_with_input(
|
||||||
|
engine_state,
|
||||||
|
stack,
|
||||||
|
command_to_run,
|
||||||
|
input,
|
||||||
|
call.redirect_stdout,
|
||||||
|
call.redirect_stderr,
|
||||||
|
)
|
||||||
|
.map(|res| res.0)?
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
PipelineData::empty()
|
||||||
|
}
|
||||||
.into_value(call.head);
|
.into_value(call.head);
|
||||||
|
|
||||||
let end_time = Instant::now();
|
let end_time = Instant::now();
|
||||||
@ -96,6 +99,11 @@ impl Command for TimeIt {
|
|||||||
example: "http get https://www.nushell.sh/book/ | timeit { split chars }",
|
example: "http get https://www.nushell.sh/book/ | timeit { split chars }",
|
||||||
result: None,
|
result: None,
|
||||||
},
|
},
|
||||||
|
Example {
|
||||||
|
description: "Times a command invocation",
|
||||||
|
example: "timeit ls -la",
|
||||||
|
result: None,
|
||||||
|
},
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user