2019-08-19 05:16:39 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-05-25 01:20:03 +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-12-04 19:52:31 +00:00
|
|
|
use nu_protocol::{
|
|
|
|
Primitive, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue,
|
|
|
|
};
|
2019-11-21 14:33:14 +00:00
|
|
|
use nu_source::Tagged;
|
2019-05-25 01:20:03 +00:00
|
|
|
|
2019-08-20 03:15:05 +00:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct SplitColumnArgs {
|
2019-08-20 06:11:11 +00:00
|
|
|
separator: Tagged<String>,
|
2019-08-20 03:15:05 +00:00
|
|
|
rest: Vec<Tagged<String>>,
|
2019-08-27 11:30:09 +00:00
|
|
|
#[serde(rename(deserialize = "collapse-empty"))]
|
|
|
|
collapse_empty: bool,
|
2019-08-20 03:15:05 +00:00
|
|
|
}
|
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
pub struct SplitColumn;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for SplitColumn {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"split-column"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2019-08-20 06:11:11 +00:00
|
|
|
Signature::build("split-column")
|
2019-10-28 05:15:35 +00:00
|
|
|
.required(
|
|
|
|
"separator",
|
|
|
|
SyntaxShape::Any,
|
|
|
|
"the character that denotes what separates columns",
|
|
|
|
)
|
|
|
|
.switch("collapse-empty", "remove empty columns")
|
|
|
|
.rest(SyntaxShape::Member, "column names to give the new columns")
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
2019-08-29 22:52:32 +00:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Split row contents across multiple columns via the separator."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
args.process(registry, split_column)?.run()
|
|
|
|
}
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
|
|
|
|
2019-08-20 03:15:05 +00:00
|
|
|
fn split_column(
|
2019-09-11 14:36:50 +00:00
|
|
|
SplitColumnArgs {
|
|
|
|
separator,
|
|
|
|
rest,
|
|
|
|
collapse_empty,
|
|
|
|
}: SplitColumnArgs,
|
2019-08-20 03:15:05 +00:00
|
|
|
RunnableContext { input, name, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-11-21 14:33:14 +00:00
|
|
|
let name_span = name.span;
|
|
|
|
|
2019-05-25 01:20:03 +00:00
|
|
|
Ok(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.replace("\\n", "\n");
|
2019-06-01 17:00:42 +00:00
|
|
|
trace!("splitting with {:?}", splitter);
|
2019-05-25 01:20:03 +00:00
|
|
|
|
2019-08-27 11:30:09 +00:00
|
|
|
let split_result: Vec<_> = if collapse_empty {
|
|
|
|
s.split(&splitter).filter(|s| !s.is_empty()).collect()
|
2019-08-26 11:45:29 +00:00
|
|
|
} else {
|
|
|
|
s.split(&splitter).collect()
|
|
|
|
};
|
|
|
|
|
2019-06-01 17:00:42 +00:00
|
|
|
trace!("split result = {:?}", split_result);
|
2019-05-26 06:54:41 +00:00
|
|
|
|
2019-08-20 06:11:11 +00:00
|
|
|
let positional: Vec<_> = rest.iter().map(|f| f.item.clone()).collect();
|
|
|
|
|
2019-05-28 02:01:37 +00:00
|
|
|
// If they didn't provide column names, make up our own
|
2019-12-06 15:28:26 +00:00
|
|
|
if positional.is_empty() {
|
2019-05-28 02:01:37 +00:00
|
|
|
let mut gen_columns = vec![];
|
|
|
|
for i in 0..split_result.len() {
|
|
|
|
gen_columns.push(format!("Column{}", i + 1));
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
let mut dict = TaggedDictBuilder::new(&v.tag);
|
2019-06-22 20:46:16 +00:00
|
|
|
for (&k, v) in split_result.iter().zip(gen_columns.iter()) {
|
2019-11-21 14:33:14 +00:00
|
|
|
dict.insert_untagged(v.clone(), Primitive::String(k.into()));
|
2019-05-28 02:01:37 +00:00
|
|
|
}
|
2019-07-09 04:31:26 +00:00
|
|
|
|
2019-11-21 14:33:14 +00:00
|
|
|
ReturnSuccess::value(dict.into_value())
|
2019-05-25 01:20:03 +00:00
|
|
|
} else {
|
2019-11-21 14:33:14 +00:00
|
|
|
let mut dict = TaggedDictBuilder::new(&v.tag);
|
2019-08-20 06:11:11 +00:00
|
|
|
for (&k, v) in split_result.iter().zip(positional.iter()) {
|
2019-11-21 14:33:14 +00:00
|
|
|
dict.insert_untagged(
|
|
|
|
v,
|
|
|
|
UntaggedValue::Primitive(Primitive::String(k.into())),
|
|
|
|
);
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
2019-11-21 14:33:14 +00:00
|
|
|
ReturnSuccess::value(dict.into_value())
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
2019-12-03 06:44:59 +00:00
|
|
|
} else {
|
|
|
|
Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
|
|
|
name_span,
|
|
|
|
"value originates from here",
|
|
|
|
v.tag.span,
|
|
|
|
))
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
|
|
|
})
|
2019-07-03 20:31:15 +00:00
|
|
|
.to_output_stream())
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|