Skip to main content

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.
43In protocol modes (`--lsp`, `--mcp`), `print` always writes to stderr to keep stdout reserved for protocol messages."#
44    }
45
46    fn search_terms(&self) -> Vec<&str> {
47        vec!["display"]
48    }
49
50    fn run(
51        &self,
52        engine_state: &EngineState,
53        stack: &mut Stack,
54        call: &Call,
55        mut input: PipelineData,
56    ) -> Result<PipelineData, ShellError> {
57        let args: Vec<Value> = call.rest(engine_state, stack, 0)?;
58        let no_newline = call.has_flag(engine_state, stack, "no-newline")?;
59        let raw = call.has_flag(engine_state, stack, "raw")?;
60
61        // In protocol modes (LSP, MCP), reserve stdout for protocol messages.
62        let to_stderr = if engine_state.is_lsp || engine_state.is_mcp {
63            true
64        } else {
65            call.has_flag(engine_state, stack, "stderr")?
66        };
67
68        // This will allow for easy printing of pipelines as well
69        if !args.is_empty() {
70            for arg in args {
71                if raw {
72                    arg.into_pipeline_data()
73                        .print_raw(engine_state, no_newline, to_stderr)?;
74                } else {
75                    arg.into_pipeline_data().print_table(
76                        engine_state,
77                        stack,
78                        no_newline,
79                        to_stderr,
80                    )?;
81                }
82            }
83        } else if !input.is_nothing() {
84            if let PipelineData::ByteStream(stream, _) = &mut input
85                && let ByteStreamSource::Child(child) = stream.source_mut()
86            {
87                child.ignore_error(true);
88            }
89            if raw {
90                input.print_raw(engine_state, no_newline, to_stderr)?;
91            } else {
92                input.print_table(engine_state, stack, no_newline, to_stderr)?;
93            }
94        }
95
96        Ok(PipelineData::empty())
97    }
98
99    fn examples(&self) -> Vec<Example<'_>> {
100        vec![
101            Example {
102                description: "Print 'hello world'",
103                example: r#"print "hello world""#,
104                result: None,
105            },
106            Example {
107                description: "Print the sum of 2 and 3",
108                example: "print (2 + 3)",
109                result: None,
110            },
111            Example {
112                description: "Print 'ABC' from binary data",
113                example: "0x[41 42 43] | print --raw",
114                result: None,
115            },
116        ]
117    }
118}