nushell/src/commands/split_row.rs
Yehuda Katz 5d4097073b Add --loglevel and --develop
--loglevel sets the log level for all of nu
--develop takes a list of modules and turns on trace mode for them
2019-06-01 10:00:42 -07:00

38 lines
1.1 KiB
Rust

use crate::errors::ShellError;
use crate::object::{Primitive, Value};
use crate::prelude::*;
use log::trace;
// TODO: "Amount remaining" wrapper
pub fn split_row(args: CommandArgs) -> Result<OutputStream, ShellError> {
let input = args.input;
let args = args.positional;
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);
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
trace!("split result = {:?}", split_result);
let mut result = VecDeque::new();
for s in split_result {
result.push_back(ReturnValue::Value(Value::Primitive(Primitive::String(
s.to_string(),
))));
}
result
}
_ => {
let result = VecDeque::new();
result
}
})
.flatten();
Ok(stream.boxed())
}