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

127 lines
4.2 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;
2020-05-27 20:07:53 +02:00
use nu_protocol::hir::Operator;
use nu_protocol::{
Primitive, RangeInclusion, 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) -> Vec<Example> {
vec![
2020-05-12 07:17:17 +02:00
Example {
description: "Put a hello message in the pipeline",
example: "echo 'hello'",
result: Some(vec![Value::from("hello")]),
2020-05-12 07:17:17 +02:00
},
Example {
description: "Print the value of the special '$nu' variable",
example: "echo $nu",
result: None,
2020-05-12 07:17:17 +02:00
},
]
}
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()));
}
}
2020-05-27 20:07:53 +02:00
Value {
value: UntaggedValue::Primitive(Primitive::Range(range)),
tag
} => {
let mut current = range.from.0.item;
while current != range.to.0.item {
yield Ok(ReturnSuccess::Value(UntaggedValue::Primitive(current.clone()).into_value(&tag)));
current = match crate::data::value::compute_values(Operator::Plus, &UntaggedValue::Primitive(current), &UntaggedValue::int(1)) {
Ok(result) => match result {
UntaggedValue::Primitive(p) => p,
_ => {
yield Err(ShellError::unimplemented("Internal error: expected a primitive result from increment"));
return;
}
},
Err((left_type, right_type)) => {
yield Err(ShellError::coerce_error(
left_type.spanned(tag.span),
right_type.spanned(tag.span),
));
return;
}
}
}
match range.to.1 {
RangeInclusion::Inclusive => {
yield Ok(ReturnSuccess::Value(UntaggedValue::Primitive(current.clone()).into_value(&tag)));
}
_ => {}
}
}
_ => {
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())
}
#[cfg(test)]
mod tests {
use super::Echo;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Echo {})
}
}