2019-08-19 05:16:39 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-05-30 05:08:42 +00:00
|
|
|
use crate::prelude::*;
|
2019-06-01 17:00:42 +00: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 02:30:48 +00:00
|
|
|
use nu_errors::ShellError;
|
2019-11-30 00:21:05 +00:00
|
|
|
use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
2019-11-21 14:33:14 +00:00
|
|
|
use nu_source::Tagged;
|
2019-05-30 05:08:42 +00:00
|
|
|
|
2019-08-20 03:15:05 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SplitRowArgs {
|
2019-08-20 06:11:11 +00:00
|
|
|
separator: Tagged<String>,
|
2019-08-20 03:15:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
pub struct SplitRow;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for SplitRow {
|
2019-08-29 22:52:32 +00:00
|
|
|
fn name(&self) -> &str {
|
|
|
|
"split-row"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2019-10-28 05:15:35 +00:00
|
|
|
Signature::build("split-row").required(
|
|
|
|
"separator",
|
|
|
|
SyntaxShape::Any,
|
|
|
|
"the character that denotes what separates rows",
|
|
|
|
)
|
2019-08-29 22:52:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Split row contents over multiple rows via the separator."
|
|
|
|
}
|
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-08-20 03:15:05 +00:00
|
|
|
args.process(registry, split_row)?.run()
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-20 03:15:05 +00:00
|
|
|
fn split_row(
|
2019-08-20 06:11:11 +00:00
|
|
|
SplitRowArgs { separator }: SplitRowArgs,
|
2019-08-20 03:15:05 +00:00
|
|
|
RunnableContext { input, name, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-05-30 05:08:42 +00:00
|
|
|
let stream = input
|
2019-07-03 20:31:15 +00:00
|
|
|
.values
|
2019-12-03 06:44:59 +00:00
|
|
|
.map(move |v| {
|
|
|
|
if let Ok(s) = v.as_string() {
|
2019-08-20 06:11:11 +00:00
|
|
|
let splitter = separator.item.replace("\\n", "\n");
|
2019-06-01 17:00:42 +00:00
|
|
|
trace!("splitting with {:?}", splitter);
|
2019-05-30 05:08:42 +00:00
|
|
|
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
|
|
|
|
|
2019-06-01 17:00:42 +00:00
|
|
|
trace!("split result = {:?}", split_result);
|
2019-05-30 05:08:42 +00:00
|
|
|
|
|
|
|
let mut result = VecDeque::new();
|
|
|
|
for s in split_result {
|
2019-07-08 16:44:53 +00:00
|
|
|
result.push_back(ReturnSuccess::value(
|
2019-11-21 14:33:14 +00:00
|
|
|
UntaggedValue::Primitive(Primitive::String(s.into())).into_value(&v.tag),
|
2019-07-08 16:44:53 +00:00
|
|
|
));
|
2019-05-30 05:08:42 +00:00
|
|
|
}
|
|
|
|
result
|
2019-12-03 06:44:59 +00:00
|
|
|
} else {
|
2019-06-15 23:03:49 +00:00
|
|
|
let mut result = VecDeque::new();
|
2019-08-05 08:54:29 +00:00
|
|
|
result.push_back(Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-11-21 14:33:14 +00:00
|
|
|
name.span,
|
2019-08-05 08:54:29 +00:00
|
|
|
"value originates from here",
|
2019-11-21 14:33:14 +00:00
|
|
|
v.tag.span,
|
2019-07-03 20:31:15 +00:00
|
|
|
)));
|
2019-05-30 05:08:42 +00:00
|
|
|
result
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.flatten();
|
|
|
|
|
2019-07-03 20:31:15 +00:00
|
|
|
Ok(stream.to_output_stream())
|
2019-05-30 05:08:42 +00:00
|
|
|
}
|