nushell/src/commands/split_column.rs

118 lines
4.0 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
use crate::data::TaggedDictBuilder;
2019-05-25 03:20:03 +02:00
use crate::prelude::*;
use log::trace;
use nu_errors::ShellError;
2019-11-30 01:21:05 +01:00
use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged;
2019-05-25 03:20:03 +02:00
#[derive(Deserialize)]
struct SplitColumnArgs {
2019-08-20 08:11:11 +02:00
separator: Tagged<String>,
rest: Vec<Tagged<String>>,
2019-08-27 13:30:09 +02:00
#[serde(rename(deserialize = "collapse-empty"))]
collapse_empty: bool,
}
pub struct SplitColumn;
impl WholeStreamCommand for SplitColumn {
fn name(&self) -> &str {
"split-column"
}
fn signature(&self) -> Signature {
2019-08-20 08:11:11 +02:00
Signature::build("split-column")
2019-10-28 06:15:35 +01: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")
}
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()
}
}
fn split_column(
2019-09-11 16:36:50 +02:00
SplitColumnArgs {
separator,
rest,
collapse_empty,
}: SplitColumnArgs,
RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> {
let name_span = name.span;
2019-05-25 03:20:03 +02:00
Ok(input
.values
.map(move |v| match v.value {
UntaggedValue::Primitive(Primitive::String(ref s)) => {
2019-08-20 08:11:11 +02:00
let splitter = separator.replace("\\n", "\n");
trace!("splitting with {:?}", splitter);
2019-05-25 03:20:03 +02:00
2019-08-27 13:30:09 +02:00
let split_result: Vec<_> = if collapse_empty {
s.split(&splitter).filter(|s| !s.is_empty()).collect()
} else {
s.split(&splitter).collect()
};
trace!("split result = {:?}", split_result);
2019-05-26 08:54:41 +02:00
2019-08-20 08:11:11 +02:00
let positional: Vec<_> = rest.iter().map(|f| f.item.clone()).collect();
2019-05-28 04:01:37 +02:00
// If they didn't provide column names, make up our own
2019-08-20 08:11:11 +02:00
if positional.len() == 0 {
2019-05-28 04:01:37 +02:00
let mut gen_columns = vec![];
for i in 0..split_result.len() {
gen_columns.push(format!("Column{}", i + 1));
}
let mut dict = TaggedDictBuilder::new(&v.tag);
2019-06-22 22:46:16 +02:00
for (&k, v) in split_result.iter().zip(gen_columns.iter()) {
dict.insert_untagged(v.clone(), Primitive::String(k.into()));
2019-05-28 04:01:37 +02:00
}
2019-07-09 06:31:26 +02:00
ReturnSuccess::value(dict.into_value())
2019-08-20 08:11:11 +02:00
} else if split_result.len() == positional.len() {
let mut dict = TaggedDictBuilder::new(&v.tag);
2019-08-20 08:11:11 +02:00
for (&k, v) in split_result.iter().zip(positional.iter()) {
dict.insert_untagged(
v,
UntaggedValue::Primitive(Primitive::String(k.into())),
);
2019-05-25 03:20:03 +02:00
}
ReturnSuccess::value(dict.into_value())
2019-05-25 03:20:03 +02:00
} else {
let mut dict = TaggedDictBuilder::new(&v.tag);
2019-08-20 08:11:11 +02:00
for (&k, v) in split_result.iter().zip(positional.iter()) {
dict.insert_untagged(
v,
UntaggedValue::Primitive(Primitive::String(k.into())),
);
2019-05-25 03:20:03 +02:00
}
ReturnSuccess::value(dict.into_value())
2019-05-25 03:20:03 +02:00
}
}
_ => 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 03:20:03 +02:00
})
.to_output_stream())
2019-05-25 03:20:03 +02:00
}