2019-06-18 00:39:57 +00:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::object::{Primitive, Value};
|
|
|
|
use crate::prelude::*;
|
2019-06-22 03:43:37 +00:00
|
|
|
use log::trace;
|
|
|
|
|
|
|
|
// TODO: "Amount remaining" wrapper
|
2019-06-18 00:39:57 +00:00
|
|
|
|
|
|
|
pub fn lines(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
|
|
let input = args.input;
|
2019-07-20 02:27:10 +00:00
|
|
|
let span = args.call_info.name_span;
|
2019-06-18 00:39:57 +00:00
|
|
|
|
|
|
|
let stream = input
|
2019-07-03 20:31:15 +00:00
|
|
|
.values
|
2019-07-08 16:44:53 +00:00
|
|
|
.map(move |v| match v.item {
|
2019-06-18 00:39:57 +00:00
|
|
|
Value::Primitive(Primitive::String(s)) => {
|
2019-06-24 02:00:53 +00:00
|
|
|
let split_result: Vec<_> = s.lines().filter(|s| s.trim() != "").collect();
|
2019-06-22 03:43:37 +00:00
|
|
|
|
|
|
|
trace!("split result = {:?}", split_result);
|
2019-06-18 00:39:57 +00:00
|
|
|
|
|
|
|
let mut result = VecDeque::new();
|
|
|
|
for s in split_result {
|
2019-07-08 16:44:53 +00:00
|
|
|
result.push_back(ReturnSuccess::value(
|
|
|
|
Value::Primitive(Primitive::String(s.into())).spanned_unknown(),
|
|
|
|
));
|
2019-06-18 00:39:57 +00:00
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let mut result = VecDeque::new();
|
2019-07-03 20:31:15 +00:00
|
|
|
result.push_back(Err(ShellError::maybe_labeled_error(
|
|
|
|
"Expected string values from pipeline",
|
|
|
|
"expects strings from pipeline",
|
|
|
|
span,
|
|
|
|
)));
|
2019-06-18 00:39:57 +00:00
|
|
|
result
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.flatten();
|
|
|
|
|
2019-07-03 20:31:15 +00:00
|
|
|
Ok(stream.to_output_stream())
|
2019-06-18 00:39:57 +00:00
|
|
|
}
|