nu_cmd_lang/core_commands/
echo.rs1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct Echo;
5
6impl Command for Echo {
7 fn name(&self) -> &str {
8 "echo"
9 }
10
11 fn description(&self) -> &str {
12 "Returns its arguments, ignoring the piped-in value."
13 }
14
15 fn signature(&self) -> Signature {
16 Signature::build("echo")
17 .input_output_types(vec![(Type::Nothing, Type::Any)])
18 .rest("rest", SyntaxShape::Any, "The values to echo.")
19 .category(Category::Core)
20 }
21
22 fn extra_description(&self) -> &str {
23 r#"Unlike `print`, which prints unstructured text to stdout, `echo` is like an
24identity function and simply returns its arguments. When given no arguments,
25it returns an empty string. When given one argument, it returns it as a
26nushell value. Otherwise, it returns a list of the arguments. There is usually
27little reason to use this over just writing the values as-is."#
28 }
29
30 fn run(
31 &self,
32 engine_state: &EngineState,
33 stack: &mut Stack,
34 call: &Call,
35 _input: PipelineData,
36 ) -> Result<PipelineData, ShellError> {
37 let args = call.rest(engine_state, stack, 0)?;
38 echo_impl(args, call.head)
39 }
40
41 fn run_const(
42 &self,
43 working_set: &StateWorkingSet,
44 call: &Call,
45 _input: PipelineData,
46 ) -> Result<PipelineData, ShellError> {
47 let args = call.rest_const(working_set, 0)?;
48 echo_impl(args, call.head)
49 }
50
51 fn is_const(&self) -> bool {
52 true
53 }
54
55 fn examples(&self) -> Vec<Example<'_>> {
56 vec![
57 Example {
58 description: "Put a list of numbers in the pipeline. This is the same as [1 2 3].",
59 example: "echo 1 2 3",
60 result: Some(Value::list(
61 vec![Value::test_int(1), Value::test_int(2), Value::test_int(3)],
62 Span::test_data(),
63 )),
64 },
65 Example {
66 description: "Returns the piped-in value, by using the special $in variable to obtain it.",
67 example: "echo $in",
68 result: None,
69 },
70 ]
71 }
72}
73
74fn echo_impl(mut args: Vec<Value>, head: Span) -> Result<PipelineData, ShellError> {
75 let value = match args.len() {
76 0 => Value::string("", head),
77 1 => args.pop().expect("one element"),
78 _ => Value::list(args, head),
79 };
80 Ok(value.into_pipeline_data())
81}
82
83#[cfg(test)]
84mod test {
85 #[test]
86 fn test_examples() {
87 use super::Echo;
88 use crate::test_examples;
89 test_examples(Echo {})
90 }
91}