2022-02-18 19:43:34 +01:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::ast::Call;
|
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
|
|
use nu_protocol::{
|
|
|
|
Category, Example, IntoPipelineData, PipelineData, ShellError, Signature, SyntaxShape, Value,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Print;
|
|
|
|
|
|
|
|
impl Command for Print {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"print"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("print")
|
|
|
|
.rest("rest", SyntaxShape::Any, "the values to print")
|
2022-05-06 22:33:00 +02:00
|
|
|
.switch(
|
2022-06-06 16:42:13 +02:00
|
|
|
"no-newline",
|
2022-05-06 22:33:00 +02:00
|
|
|
"print without inserting a newline for the line ending",
|
|
|
|
Some('n'),
|
|
|
|
)
|
2022-07-02 16:54:49 +02:00
|
|
|
.switch("stderr", "print to stderr instead of stdout", Some('e'))
|
2022-02-18 19:43:34 +01:00
|
|
|
.category(Category::Strings)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2022-06-30 00:43:46 +02:00
|
|
|
"Print the given values to stdout"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extra_usage(&self) -> &str {
|
|
|
|
r#"Unlike `echo`, this command does not return any value (`print | describe` will return "nothing").
|
|
|
|
Since this command has no output, there is no point in piping it with other commands.
|
|
|
|
|
|
|
|
`print` may be used inside blocks of code (e.g.: hooks) to display text during execution without interfering with the pipeline."#
|
2022-02-18 19:43:34 +01:00
|
|
|
}
|
|
|
|
|
2022-06-06 15:47:09 +02:00
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
|
|
|
vec!["display"]
|
|
|
|
}
|
|
|
|
|
2022-02-18 19:43:34 +01:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
_input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let args: Vec<Value> = call.rest(engine_state, stack, 0)?;
|
2022-06-06 16:42:13 +02:00
|
|
|
let no_newline = call.has_flag("no-newline");
|
2022-07-02 16:54:49 +02:00
|
|
|
let to_stderr = call.has_flag("stderr");
|
2022-02-18 19:43:34 +01:00
|
|
|
let head = call.head;
|
|
|
|
|
|
|
|
for arg in args {
|
2022-05-06 22:33:00 +02:00
|
|
|
arg.into_pipeline_data()
|
2022-07-02 16:54:49 +02:00
|
|
|
.print(engine_state, stack, no_newline, to_stderr)?;
|
2022-02-18 19:43:34 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Ok(PipelineData::new(head))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Print 'hello world'",
|
|
|
|
example: r#"print "hello world""#,
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Print the sum of 2 and 3",
|
|
|
|
example: r#"print (2 + 3)"#,
|
|
|
|
result: None,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
}
|