From f4ed4fa7e30c81bd008b0c8bca7c36ae20a5a291 Mon Sep 17 00:00:00 2001 From: Antonio Natilla Date: Mon, 1 Nov 2021 09:12:48 +0100 Subject: [PATCH 1/3] Implementing Command for Echo, no examples Referring to: https://github.com/nushell/nushell/blob/main/crates/nu-command/src/commands/core_commands/echo.rs as the original implementation. --- crates/nu-command/src/core_commands/echo.rs | 32 +++++++++++++++++++++ crates/nu-command/src/core_commands/mod.rs | 2 ++ crates/nu-command/src/default_context.rs | 1 + 3 files changed, 35 insertions(+) create mode 100644 crates/nu-command/src/core_commands/echo.rs diff --git a/crates/nu-command/src/core_commands/echo.rs b/crates/nu-command/src/core_commands/echo.rs new file mode 100644 index 000000000..af9fd57b0 --- /dev/null +++ b/crates/nu-command/src/core_commands/echo.rs @@ -0,0 +1,32 @@ +use nu_protocol::ast::Call; +use nu_protocol::engine::{Command, EngineState, Stack}; +use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape}; +//TODO: add Example + +#[derive(Clone)] +pub struct Echo; + +impl Command for Echo { + fn name(&self) -> &str { + "echo" + } + + fn usage(&self) -> &str { + "Echo the arguments back to the user." + } + + fn signature(&self) -> Signature { + Signature::build("echo").rest("rest", SyntaxShape::Any, "the values to echo") + } + + fn run( + &self, + _engine_state: &EngineState, + _stack: &mut Stack, + _call: &Call, + input: PipelineData, + ) -> Result { + Ok(input) + } + //TODO: implement fn examples(&self) -> Vec +} diff --git a/crates/nu-command/src/core_commands/mod.rs b/crates/nu-command/src/core_commands/mod.rs index a124ffcf1..78e360496 100644 --- a/crates/nu-command/src/core_commands/mod.rs +++ b/crates/nu-command/src/core_commands/mod.rs @@ -1,6 +1,7 @@ mod alias; mod def; mod do_; +mod echo; mod export_def; mod for_; mod help; @@ -14,6 +15,7 @@ mod use_; pub use alias::Alias; pub use def::Def; pub use do_::Do; +pub use echo::Echo; pub use export_def::ExportDef; pub use for_::For; pub use help::Help; diff --git a/crates/nu-command/src/default_context.rs b/crates/nu-command/src/default_context.rs index cab72fac8..c1c54243d 100644 --- a/crates/nu-command/src/default_context.rs +++ b/crates/nu-command/src/default_context.rs @@ -30,6 +30,7 @@ pub fn create_default_context() -> EngineState { Def, Do, Each, + Echo, ExportDef, External, For, From 89225cf55c323baefea50f68067825c23743e957 Mon Sep 17 00:00:00 2001 From: Antonio Natilla Date: Mon, 1 Nov 2021 09:37:07 +0100 Subject: [PATCH 2/3] Adding examples and test for Echo --- crates/nu-command/src/core_commands/echo.rs | 29 ++++++++++++++++++--- 1 file changed, 26 insertions(+), 3 deletions(-) diff --git a/crates/nu-command/src/core_commands/echo.rs b/crates/nu-command/src/core_commands/echo.rs index af9fd57b0..be921b62d 100644 --- a/crates/nu-command/src/core_commands/echo.rs +++ b/crates/nu-command/src/core_commands/echo.rs @@ -1,7 +1,6 @@ use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{PipelineData, ShellError, Signature, SyntaxShape}; -//TODO: add Example +use nu_protocol::{Example, PipelineData, ShellError, Signature, SyntaxShape, Value}; #[derive(Clone)] pub struct Echo; @@ -28,5 +27,29 @@ impl Command for Echo { ) -> Result { Ok(input) } - //TODO: implement fn examples(&self) -> Vec + + fn examples(&self) -> Vec { + vec![ + Example { + description: "Put a hello message in the pipeline", + example: "echo 'hello'", + result: Some(Value::test_string("hello")), + }, + Example { + description: "Print the value of the special '$nu' variable", + example: "echo $nu", + result: None, + }, + ] + } +} + +#[cfg(test)] +mod test { + #[test] + fn test_examples() { + use super::Echo; + use crate::test_examples; + test_examples(Echo {}) + } } From 1c2741c5988f5c336ce9f2ee408bd56b9237d023 Mon Sep 17 00:00:00 2001 From: Antonio Natilla Date: Mon, 1 Nov 2021 12:51:46 +0100 Subject: [PATCH 3/3] Fixing run implementation for Echo Values to echo need to be extracted from the call, and then converted into PipelineData. I also updated the first example so that its result is a List, as in the reference implementation. --- crates/nu-command/src/core_commands/echo.rs | 25 +++++++++++++++------ 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/crates/nu-command/src/core_commands/echo.rs b/crates/nu-command/src/core_commands/echo.rs index be921b62d..af0c99489 100644 --- a/crates/nu-command/src/core_commands/echo.rs +++ b/crates/nu-command/src/core_commands/echo.rs @@ -1,6 +1,9 @@ +use nu_engine::CallExt; use nu_protocol::ast::Call; use nu_protocol::engine::{Command, EngineState, Stack}; -use nu_protocol::{Example, PipelineData, ShellError, Signature, SyntaxShape, Value}; +use nu_protocol::{ + Example, PipelineData, ShellError, Signature, Span, SyntaxShape, Value, ValueStream, +}; #[derive(Clone)] pub struct Echo; @@ -20,12 +23,17 @@ impl Command for Echo { fn run( &self, - _engine_state: &EngineState, - _stack: &mut Stack, - _call: &Call, - input: PipelineData, + engine_state: &EngineState, + stack: &mut Stack, + call: &Call, + _input: PipelineData, ) -> Result { - Ok(input) + call.rest(engine_state, stack, 0).map(|to_be_echoed| { + PipelineData::Stream(ValueStream::from_stream( + to_be_echoed.into_iter(), + engine_state.ctrlc.clone(), + )) + }) } fn examples(&self) -> Vec { @@ -33,7 +41,10 @@ impl Command for Echo { Example { description: "Put a hello message in the pipeline", example: "echo 'hello'", - result: Some(Value::test_string("hello")), + result: Some(Value::List { + vals: vec![Value::test_string("hello")], + span: Span::new(0, 0), + }), }, Example { description: "Print the value of the special '$nu' variable",