nu_cli/
print.rs

1use nu_engine::command_prelude::*;
2use nu_protocol::ByteStreamSource;
3
4#[derive(Clone)]
5pub struct Print;
6
7impl Command for Print {
8    fn name(&self) -> &str {
9        "print"
10    }
11
12    fn signature(&self) -> Signature {
13        Signature::build("print")
14            .input_output_types(vec![
15                (Type::Nothing, Type::Nothing),
16                (Type::Any, Type::Nothing),
17            ])
18            .allow_variants_without_examples(true)
19            .rest("rest", SyntaxShape::Any, "the values to print")
20            .switch(
21                "no-newline",
22                "print without inserting a newline for the line ending",
23                Some('n'),
24            )
25            .switch("stderr", "print to stderr instead of stdout", Some('e'))
26            .switch(
27                "raw",
28                "print without formatting (including binary data)",
29                Some('r'),
30            )
31            .category(Category::Strings)
32    }
33
34    fn description(&self) -> &str {
35        "Print the given values to stdout."
36    }
37
38    fn extra_description(&self) -> &str {
39        r#"Unlike `echo`, this command does not return any value (`print | describe` will return "nothing").
40Since this command has no output, there is no point in piping it with other commands.
41
42`print` may be used inside blocks of code (e.g.: hooks) to display text during execution without interfering with the pipeline."#
43    }
44
45    fn search_terms(&self) -> Vec<&str> {
46        vec!["display"]
47    }
48
49    fn run(
50        &self,
51        engine_state: &EngineState,
52        stack: &mut Stack,
53        call: &Call,
54        mut input: PipelineData,
55    ) -> Result<PipelineData, ShellError> {
56        let args: Vec<Value> = call.rest(engine_state, stack, 0)?;
57        let no_newline = call.has_flag(engine_state, stack, "no-newline")?;
58        let to_stderr = call.has_flag(engine_state, stack, "stderr")?;
59        let raw = call.has_flag(engine_state, stack, "raw")?;
60
61        // This will allow for easy printing of pipelines as well
62        if !args.is_empty() {
63            for arg in args {
64                if raw {
65                    arg.into_pipeline_data()
66                        .print_raw(engine_state, no_newline, to_stderr)?;
67                } else {
68                    arg.into_pipeline_data().print_table(
69                        engine_state,
70                        stack,
71                        no_newline,
72                        to_stderr,
73                    )?;
74                }
75            }
76        } else if !input.is_nothing() {
77            if let PipelineData::ByteStream(stream, _) = &mut input {
78                if let ByteStreamSource::Child(child) = stream.source_mut() {
79                    child.ignore_error(true);
80                }
81            }
82            if raw {
83                input.print_raw(engine_state, no_newline, to_stderr)?;
84            } else {
85                input.print_table(engine_state, stack, no_newline, to_stderr)?;
86            }
87        }
88
89        Ok(PipelineData::empty())
90    }
91
92    fn examples(&self) -> Vec<Example> {
93        vec![
94            Example {
95                description: "Print 'hello world'",
96                example: r#"print "hello world""#,
97                result: None,
98            },
99            Example {
100                description: "Print the sum of 2 and 3",
101                example: r#"print (2 + 3)"#,
102                result: None,
103            },
104            Example {
105                description: "Print 'ABC' from binary data",
106                example: r#"0x[41 42 43] | print --raw"#,
107                result: None,
108            },
109        ]
110    }
111}