nushell/src/commands/split_column.rs

88 lines
3.2 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
2019-05-25 03:20:03 +02:00
use crate::errors::ShellError;
2019-08-01 03:58:42 +02:00
use crate::object::{Primitive, TaggedDictBuilder, Value};
2019-05-25 03:20:03 +02:00
use crate::prelude::*;
use log::trace;
2019-05-25 03:20:03 +02:00
pub struct SplitColumn;
impl WholeStreamCommand for SplitColumn {
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
split_column(args, registry)
}
fn name(&self) -> &str {
"split-column"
}
fn signature(&self) -> Signature {
// TODO: Signature?
// TODO: Improve error. Old error had extra info:
//
// needs parameter (e.g. split-column ",")
Signature::build("split-column").required("delimeter", SyntaxType::Any)
}
}
fn split_column(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
2019-07-24 00:22:11 +02:00
let args = args.evaluate_once(registry)?;
let span = args.name_span();
let (input, args) = args.parts();
let positional: Vec<_> = args.positional.iter().flatten().cloned().collect();
2019-05-25 03:20:03 +02:00
Ok(input
.values
2019-07-08 18:44:53 +02:00
.map(move |v| match v.item {
2019-08-01 03:58:42 +02:00
Value::Primitive(Primitive::String(ref s)) => {
2019-06-22 05:43:37 +02:00
let splitter = positional[0].as_string().unwrap().replace("\\n", "\n");
trace!("splitting with {:?}", splitter);
2019-05-25 03:20:03 +02:00
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
trace!("split result = {:?}", split_result);
2019-05-26 08:54:41 +02:00
2019-05-28 04:01:37 +02:00
// If they didn't provide column names, make up our own
2019-06-22 05:43:37 +02:00
if (positional.len() - 1) == 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()) {
2019-07-09 06:31:26 +02:00
dict.insert(v.clone(), Primitive::String(k.into()));
2019-05-28 04:01:37 +02:00
}
2019-07-09 06:31:26 +02:00
2019-08-01 03:58:42 +02:00
ReturnSuccess::value(dict.into_tagged_value())
2019-06-22 05:43:37 +02:00
} else if split_result.len() == (positional.len() - 1) {
let mut dict = TaggedDictBuilder::new(v.tag());
2019-06-22 22:46:16 +02:00
for (&k, v) in split_result.iter().zip(positional.iter().skip(1)) {
2019-07-09 06:31:26 +02:00
dict.insert(
2019-05-26 08:54:41 +02:00
v.as_string().unwrap(),
2019-06-22 22:46:16 +02:00
Value::Primitive(Primitive::String(k.into())),
2019-05-26 08:54:41 +02:00
);
2019-05-25 03:20:03 +02:00
}
2019-08-01 03:58:42 +02:00
ReturnSuccess::value(dict.into_tagged_value())
2019-05-25 03:20:03 +02:00
} else {
let mut dict = TaggedDictBuilder::new(v.tag());
2019-06-22 05:43:37 +02:00
for k in positional.iter().skip(1) {
2019-07-09 06:31:26 +02:00
dict.insert(k.as_string().unwrap().trim(), Primitive::String("".into()));
2019-05-25 03:20:03 +02:00
}
2019-08-01 03:58:42 +02:00
ReturnSuccess::value(dict.into_tagged_value())
2019-05-25 03:20:03 +02:00
}
}
_ => Err(ShellError::labeled_error_with_secondary(
"Expected a string from pipeline",
"requires string input",
2019-06-16 01:03:49 +02:00
span,
"value originates from here",
v.span(),
)),
2019-05-25 03:20:03 +02:00
})
.to_output_stream())
2019-05-25 03:20:03 +02:00
}