nushell/crates/nu-cli/src/commands/echo.rs

79 lines
2.1 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
2019-09-08 01:43:53 +02:00
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
2019-09-08 01:43:53 +02:00
pub struct Echo;
#[derive(Deserialize)]
pub struct EchoArgs {
pub rest: Vec<Value>,
}
impl WholeStreamCommand for Echo {
2019-09-08 01:43:53 +02:00
fn name(&self) -> &str {
"echo"
}
fn signature(&self) -> Signature {
2019-10-28 06:15:35 +01:00
Signature::build("echo").rest(SyntaxShape::Any, "the values to echo")
2019-09-08 01:43:53 +02:00
}
fn usage(&self) -> &str {
2019-09-25 00:15:53 +02:00
"Echo the arguments back to the user."
2019-09-08 01:43:53 +02:00
}
fn run(
&self,
args: CommandArgs,
2019-09-08 01:43:53 +02:00
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
echo(args, registry)
2019-09-08 01:43:53 +02:00
}
2020-05-12 07:17:17 +02:00
fn examples(&self) -> &[Example] {
&[
Example {
description: "Put a hello message in the pipeline",
example: "echo 'hello'",
},
Example {
description: "Print the value of the special '$nu' variable",
example: "echo $nu",
},
]
}
2019-09-08 01:43:53 +02:00
}
fn echo(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let stream = async_stream! {
let (args, _): (EchoArgs, _) = args.process(&registry).await?;
2019-09-08 01:43:53 +02:00
for i in args.rest {
match i.as_string() {
Ok(s) => {
yield Ok(ReturnSuccess::Value(
UntaggedValue::string(s).into_value(i.tag.clone()),
));
}
_ => match i {
Value {
value: UntaggedValue::Table(table),
..
} => {
for value in table {
yield Ok(ReturnSuccess::Value(value.clone()));
}
}
_ => {
yield Ok(ReturnSuccess::Value(i.clone()));
}
},
}
2019-09-08 01:43:53 +02:00
}
};
2019-09-08 01:43:53 +02:00
Ok(stream.to_output_stream())
}