2019-05-30 05:08:42 +00:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::object::{Primitive, Value};
|
2019-06-22 01:36:57 +00:00
|
|
|
use crate::parser::Spanned;
|
2019-05-30 05:08:42 +00:00
|
|
|
use crate::prelude::*;
|
2019-06-01 17:00:42 +00:00
|
|
|
use log::trace;
|
2019-05-30 05:08:42 +00:00
|
|
|
|
|
|
|
// TODO: "Amount remaining" wrapper
|
|
|
|
|
2019-05-31 20:34:15 +00:00
|
|
|
pub fn split_row(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
2019-06-22 01:36:57 +00:00
|
|
|
let positional: Vec<Spanned<Value>> = args.positional_iter().cloned().collect();
|
|
|
|
|
|
|
|
if positional.len() == 0 {
|
|
|
|
if let Some(span) = args.name_span {
|
|
|
|
return Err(ShellError::labeled_error(
|
|
|
|
"split-row requires arguments",
|
|
|
|
"needs parameter",
|
|
|
|
span,
|
|
|
|
));
|
|
|
|
} else {
|
|
|
|
return Err(ShellError::string("split-row requires arguments."));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-30 05:08:42 +00:00
|
|
|
let input = args.input;
|
|
|
|
|
|
|
|
let stream = 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-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-06-01 17:00:42 +00:00
|
|
|
result.push_back(ReturnValue::Value(Value::Primitive(Primitive::String(
|
|
|
|
s.to_string(),
|
|
|
|
))));
|
2019-05-30 05:08:42 +00:00
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
_ => {
|
2019-05-31 20:34:15 +00:00
|
|
|
let result = VecDeque::new();
|
2019-05-30 05:08:42 +00:00
|
|
|
result
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.flatten();
|
|
|
|
|
|
|
|
Ok(stream.boxed())
|
|
|
|
}
|