nushell/src/commands/lines.rs

44 lines
1.3 KiB
Rust
Raw Normal View History

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;
let span = args.call_info.name_span;
2019-06-18 02:39:57 +02:00
let stream = input
.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();
result.push_back(Err(ShellError::labeled_error_with_secondary(
"Expected a string from pipeline",
"requires string input",
span,
"value originates from here",
v.span(),
)));
2019-06-18 02:39:57 +02:00
result
}
})
.flatten();
Ok(stream.to_output_stream())
2019-06-18 02:39:57 +02:00
}