nushell/src/commands/split_row.rs

38 lines
1.1 KiB
Rust
Raw Normal View History

2019-05-30 07:08:42 +02:00
use crate::errors::ShellError;
use crate::object::{Primitive, Value};
use crate::prelude::*;
use log::trace;
2019-05-30 07:08:42 +02:00
// TODO: "Amount remaining" wrapper
2019-05-31 22:34:15 +02:00
pub fn split_row(args: CommandArgs) -> Result<OutputStream, ShellError> {
2019-05-30 07:08:42 +02:00
let input = args.input;
2019-06-01 09:05:57 +02:00
let args = args.positional;
2019-05-30 07:08:42 +02:00
let stream = input
.map(move |v| match v {
Value::Primitive(Primitive::String(s)) => {
let splitter = args[0].as_string().unwrap().replace("\\n", "\n");
trace!("splitting with {:?}", splitter);
2019-05-30 07:08:42 +02:00
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
trace!("split result = {:?}", split_result);
2019-05-30 07:08:42 +02:00
let mut result = VecDeque::new();
for s in split_result {
result.push_back(ReturnValue::Value(Value::Primitive(Primitive::String(
s.to_string(),
))));
2019-05-30 07:08:42 +02:00
}
result
}
_ => {
2019-05-31 22:34:15 +02:00
let result = VecDeque::new();
2019-05-30 07:08:42 +02:00
result
}
})
.flatten();
Ok(stream.boxed())
}