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 raw = call.has_flag(engine_state, stack, "raw")?;
59
60        // if we're in the LSP *always* print to stderr
61        let to_stderr = if engine_state.is_lsp {
62            true
63        } else {
64            call.has_flag(engine_state, stack, "stderr")?
65        };
66
67        // This will allow for easy printing of pipelines as well
68        if !args.is_empty() {
69            for arg in args {
70                if raw {
71                    arg.into_pipeline_data()
72                        .print_raw(engine_state, no_newline, to_stderr)?;
73                } else {
74                    arg.into_pipeline_data().print_table(
75                        engine_state,
76                        stack,
77                        no_newline,
78                        to_stderr,
79                    )?;
80                }
81            }
82        } else if !input.is_nothing() {
83            if let PipelineData::ByteStream(stream, _) = &mut input
84                && let ByteStreamSource::Child(child) = stream.source_mut()
85            {
86                child.ignore_error(true);
87            }
88            if raw {
89                input.print_raw(engine_state, no_newline, to_stderr)?;
90            } else {
91                input.print_table(engine_state, stack, no_newline, to_stderr)?;
92            }
93        }
94
95        Ok(PipelineData::empty())
96    }
97
98    fn examples(&self) -> Vec<Example<'_>> {
99        vec![
100            Example {
101                description: "Print 'hello world'",
102                example: r#"print "hello world""#,
103                result: None,
104            },
105            Example {
106                description: "Print the sum of 2 and 3",
107                example: r#"print (2 + 3)"#,
108                result: None,
109            },
110            Example {
111                description: "Print 'ABC' from binary data",
112                example: r#"0x[41 42 43] | print --raw"#,
113                result: None,
114            },
115        ]
116    }
117}