2019-08-19 07:16:39 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-05-30 07:08:42 +02:00
|
|
|
use crate::prelude::*;
|
2019-06-01 19:00:42 +02:00
|
|
|
use log::trace;
|
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;
|
2019-11-30 01:21:05 +01:00
|
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
2019-11-21 15:33:14 +01:00
|
|
|
use nu_source::Tagged;
|
2019-05-30 07:08:42 +02:00
|
|
|
|
2019-08-20 05:15:05 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SplitRowArgs {
|
2019-08-20 08:11:11 +02:00
|
|
|
separator: Tagged<String>,
|
2019-08-20 05:15:05 +02:00
|
|
|
}
|
|
|
|
|
2020-05-24 08:41:30 +02:00
|
|
|
pub struct SubCommand;
|
2019-08-19 07:16:39 +02:00
|
|
|
|
2020-05-24 08:41:30 +02:00
|
|
|
impl WholeStreamCommand for SubCommand {
|
2019-08-30 00:52:32 +02:00
|
|
|
fn name(&self) -> &str {
|
2020-05-24 08:41:30 +02:00
|
|
|
"split row"
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2020-05-24 08:41:30 +02:00
|
|
|
Signature::build("split row").required(
|
2019-10-28 06:15:35 +01:00
|
|
|
"separator",
|
|
|
|
SyntaxShape::Any,
|
|
|
|
"the character that denotes what separates rows",
|
|
|
|
)
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2020-05-24 08:41:30 +02:00
|
|
|
"splits contents over multiple rows via the separator."
|
2019-08-30 00:52:32 +02:00
|
|
|
}
|
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
split_row(args, registry)
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-16 05:18:24 +02:00
|
|
|
fn split_row(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
|
|
|
let registry = registry.clone();
|
|
|
|
let stream = async_stream! {
|
|
|
|
let name = args.call_info.name_tag.clone();
|
|
|
|
let (SplitRowArgs { separator }, mut input) = args.process(®istry).await?;
|
|
|
|
while let Some(v) = input.next().await {
|
2019-12-03 07:44:59 +01:00
|
|
|
if let Ok(s) = v.as_string() {
|
2019-08-20 08:11:11 +02:00
|
|
|
let splitter = separator.item.replace("\\n", "\n");
|
2019-06-01 19:00:42 +02:00
|
|
|
trace!("splitting with {:?}", splitter);
|
2019-05-30 07:08:42 +02:00
|
|
|
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
|
|
|
|
|
2019-06-01 19:00:42 +02:00
|
|
|
trace!("split result = {:?}", split_result);
|
2019-05-30 07:08:42 +02:00
|
|
|
|
|
|
|
for s in split_result {
|
2020-05-16 05:18:24 +02:00
|
|
|
yield ReturnSuccess::value(
|
2019-11-21 15:33:14 +01:00
|
|
|
UntaggedValue::Primitive(Primitive::String(s.into())).into_value(&v.tag),
|
2020-05-16 05:18:24 +02:00
|
|
|
);
|
2019-05-30 07:08:42 +02:00
|
|
|
}
|
2019-12-03 07:44:59 +01:00
|
|
|
} else {
|
2020-05-16 05:18:24 +02:00
|
|
|
yield Err(ShellError::labeled_error_with_secondary(
|
2019-08-05 10:54:29 +02:00
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-11-21 15:33:14 +01:00
|
|
|
name.span,
|
2019-08-05 10:54:29 +02:00
|
|
|
"value originates from here",
|
2019-11-21 15:33:14 +01:00
|
|
|
v.tag.span,
|
2020-05-16 05:18:24 +02:00
|
|
|
));
|
2019-05-30 07:08:42 +02:00
|
|
|
}
|
2020-05-16 05:18:24 +02:00
|
|
|
}
|
|
|
|
};
|
2019-05-30 07:08:42 +02:00
|
|
|
|
2019-07-03 22:31:15 +02:00
|
|
|
Ok(stream.to_output_stream())
|
2019-05-30 07:08:42 +02:00
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-05-24 08:41:30 +02:00
|
|
|
use super::SubCommand;
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
2020-05-24 08:41:30 +02:00
|
|
|
test_examples(SubCommand {})
|
2020-05-18 14:56:01 +02:00
|
|
|
}
|
|
|
|
}
|