2019-05-25 01:20:03 +00:00
|
|
|
use crate::errors::ShellError;
|
2019-05-26 06:54:41 +00:00
|
|
|
use crate::object::{Primitive, Value};
|
2019-05-25 01:20:03 +00:00
|
|
|
use crate::prelude::*;
|
2019-06-01 17:00:42 +00:00
|
|
|
use log::trace;
|
2019-05-25 01:20:03 +00:00
|
|
|
|
|
|
|
// TODO: "Amount remaining" wrapper
|
|
|
|
|
2019-05-31 20:34:15 +00:00
|
|
|
pub fn split_column(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2019-06-22 01:36:57 +00:00
|
|
|
let positional: Vec<_> = args.positional_iter().cloned().collect();
|
|
|
|
|
|
|
|
if positional.len() == 0 {
|
|
|
|
if let Some(span) = args.name_span {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"split-column requires arguments",
|
|
|
|
"needs parameter",
|
|
|
|
span,
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
return Err(ShellError::string("split-column requires arguments."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-25 01:20:03 +00:00
|
|
|
let input = args.input;
|
|
|
|
|
|
|
|
Ok(input
|
|
|
|
.map(move |v| match v {
|
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
2019-06-22 01:36:57 +00:00
|
|
|
let splitter = positional[0].as_string().unwrap().replace("\\n", "\n");
|
2019-06-01 17:00:42 +00:00
|
|
|
trace!("splitting with {:?}", splitter);
|
2019-05-25 01:20:03 +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-26 06:54:41 +00:00
|
|
|
|
2019-05-28 02:01:37 +00:00
|
|
|
// If they didn't provide column names, make up our own
|
2019-06-22 01:36:57 +00:00
|
|
|
if (positional.len() - 1) == 0 {
|
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));
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut dict = crate::object::Dictionary::default();
|
|
|
|
for (k, v) in split_result.iter().zip(gen_columns.iter()) {
|
|
|
|
dict.add(
|
|
|
|
v.clone(),
|
|
|
|
Value::Primitive(Primitive::String(k.to_string())),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
ReturnValue::Value(Value::Object(dict))
|
2019-06-22 01:36:57 +00:00
|
|
|
} else if split_result.len() == (positional.len() - 1) {
|
2019-05-25 01:20:03 +00:00
|
|
|
let mut dict = crate::object::Dictionary::default();
|
2019-06-22 01:36:57 +00:00
|
|
|
for (k, v) in split_result.iter().zip(positional.iter().skip(1)) {
|
2019-05-26 06:54:41 +00:00
|
|
|
dict.add(
|
|
|
|
v.as_string().unwrap(),
|
|
|
|
Value::Primitive(Primitive::String(k.to_string())),
|
|
|
|
);
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
|
|
|
ReturnValue::Value(Value::Object(dict))
|
|
|
|
} else {
|
|
|
|
let mut dict = crate::object::Dictionary::default();
|
2019-06-22 01:36:57 +00:00
|
|
|
for k in positional.iter().skip(1) {
|
2019-05-26 06:54:41 +00:00
|
|
|
dict.add(
|
|
|
|
k.as_string().unwrap().trim(),
|
|
|
|
Value::Primitive(Primitive::String("".to_string())),
|
|
|
|
);
|
2019-05-25 01:20:03 +00:00
|
|
|
}
|
|
|
|
ReturnValue::Value(Value::Object(dict))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => ReturnValue::Value(Value::Object(crate::object::Dictionary::default())),
|
|
|
|
})
|
|
|
|
.boxed())
|
|
|
|
}
|