2020-04-27 04:04:54 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-09-08 01:43:53 +02:00
|
|
|
use crate::prelude::*;
|
Extract core stuff into own crates
This commit extracts five new crates:
- nu-source, which contains the core source-code handling logic in Nu,
including Text, Span, and also the pretty.rs-based debug logic
- nu-parser, which is the parser and expander logic
- nu-protocol, which is the bulk of the types and basic conveniences
used by plugins
- nu-errors, which contains ShellError, ParseError and error handling
conveniences
- nu-textview, which is the textview plugin extracted into a crate
One of the major consequences of this refactor is that it's no longer
possible to `impl X for Spanned<Y>` outside of the `nu-source` crate, so
a lot of types became more concrete (Value became a concrete type
instead of Spanned<Value>, for example).
This also turned a number of inherent methods in the main nu crate into
plain functions (impl Value {} became a bunch of functions in the
`value` namespace in `crate::data::value`).
2019-11-26 03:30:48 +01:00
|
|
|
use nu_errors::ShellError;
|
2020-04-27 04:04:54 +02:00
|
|
|
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, UntaggedValue, Value};
|
2019-09-08 01:43:53 +02:00
|
|
|
|
|
|
|
pub struct Echo;
|
|
|
|
|
2020-04-27 04:04:54 +02:00
|
|
|
#[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,
|
2020-04-27 04:04:54 +02:00
|
|
|
args: CommandArgs,
|
2019-09-08 01:43:53 +02:00
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
echo(args, registry)
|
2019-09-08 01:43:53 +02:00
|
|
|
}
|
2020-05-12 07:17:17 +02:00
|
|
|
|
2020-05-18 14:56:01 +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'",
|
2020-05-18 14:56:01 +02:00
|
|
|
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",
|
2020-05-18 14:56:01 +02:00
|
|
|
result: None,
|
2020-05-12 07:17:17 +02:00
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2019-09-08 01:43:53 +02:00
|
|
|
}
|
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
fn echo(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let registry = registry.clone();
|
|
|
|
let stream = async_stream! {
|
|
|
|
let (args, _): (EchoArgs, _) = args.process(®istry).await?;
|
2019-09-08 01:43:53 +02:00
|
|
|
|
2020-05-16 05:18:24 +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()),
|
|
|
|
));
|
2020-04-27 04:04:54 +02:00
|
|
|
}
|
2020-05-16 05:18:24 +02:00
|
|
|
_ => 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
|
|
|
}
|
2020-05-16 05:18:24 +02:00
|
|
|
};
|
2019-09-08 01:43:53 +02:00
|
|
|
|
|
|
|
Ok(stream.to_output_stream())
|
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Echo;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
|
|
|
test_examples(Echo {})
|
|
|
|
}
|
|
|
|
}
|