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