From 85bfdba1e21d467c5fb3ab1b24bcd7a57d90ef94 Mon Sep 17 00:00:00 2001 From: JT <547158+jntrnr@users.noreply.github.com> Date: Fri, 24 Mar 2023 22:34:03 +1300 Subject: [PATCH] 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. --- crates/nu-command/src/debug/timeit.rs | 70 +++++++++++++++------------ 1 file changed, 39 insertions(+), 31 deletions(-) diff --git a/crates/nu-command/src/debug/timeit.rs b/crates/nu-command/src/debug/timeit.rs index 90ab5fa7b8..8b7fc2c6df 100644 --- a/crates/nu-command/src/debug/timeit.rs +++ b/crates/nu-command/src/debug/timeit.rs @@ -1,7 +1,7 @@ -use nu_engine::{eval_block, CallExt}; +use nu_engine::{eval_block, eval_expression_with_input}; use nu_protocol::{ ast::Call, - engine::{Block, Command, EngineState, Stack}, + engine::{Command, EngineState, Stack}, Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Type, Value, }; @@ -21,7 +21,11 @@ impl Command for TimeIt { fn signature(&self) -> nu_protocol::Signature { 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![ (Type::Any, Type::Duration), (Type::Nothing, Type::Duration), @@ -41,37 +45,36 @@ impl Command for TimeIt { call: &Call, input: PipelineData, ) -> Result { - let capture_block: Block = call.req(engine_state, stack, 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()); - } - } + let command_to_run = call.positional_nth(0); // Get the start time after all other computation has been done. let start_time = Instant::now(); - eval_block( - engine_state, - stack, - block, - input_val.into_pipeline_data_with_metadata(input_metadata), - redirect_stdout, - redirect_stderr, - )? + + if let Some(command_to_run) = command_to_run { + if let Some(block_id) = command_to_run.as_block() { + let block = engine_state.get_block(block_id); + eval_block( + engine_state, + 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); let end_time = Instant::now(); @@ -96,6 +99,11 @@ impl Command for TimeIt { example: "http get https://www.nushell.sh/book/ | timeit { split chars }", result: None, }, + Example { + description: "Times a command invocation", + example: "timeit ls -la", + result: None, + }, ] } }