2019-08-19 05:16:39 +00:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-09-05 16:23:42 +00:00
|
|
|
use crate::data::{Primitive, Value};
|
2019-09-11 14:36:50 +00:00
|
|
|
use crate::errors::ShellError;
|
2019-06-18 00:39:57 +00:00
|
|
|
use crate::prelude::*;
|
2019-06-22 03:43:37 +00:00
|
|
|
use log::trace;
|
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
pub struct Lines;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for Lines {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"lines"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("lines")
|
|
|
|
}
|
2019-08-29 22:52:32 +00:00
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Split single string into rows, one per line."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
args: CommandArgs,
|
|
|
|
registry: &CommandRegistry,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
|
|
|
lines(args, registry)
|
|
|
|
}
|
2019-08-19 05:16:39 +00:00
|
|
|
}
|
|
|
|
|
2019-06-22 03:43:37 +00:00
|
|
|
// TODO: "Amount remaining" wrapper
|
2019-06-18 00:39:57 +00:00
|
|
|
|
2019-08-19 05:16:39 +00:00
|
|
|
fn lines(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
2019-07-23 22:22:11 +00:00
|
|
|
let args = args.evaluate_once(registry)?;
|
2019-09-14 16:30:24 +00:00
|
|
|
let tag = args.name_tag();
|
2019-06-18 00:39:57 +00:00
|
|
|
let input = args.input;
|
2019-07-23 22:22:11 +00:00
|
|
|
|
|
|
|
let input: InputStream = trace_stream!(target: "nu::trace_stream::lines", "input" = input);
|
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(
|
2019-08-01 01:58:42 +00:00
|
|
|
Value::Primitive(Primitive::String(s.into())).tagged_unknown(),
|
2019-07-08 16:44:53 +00:00
|
|
|
));
|
2019-06-18 00:39:57 +00:00
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
let mut result = VecDeque::new();
|
2019-08-05 08:54:29 +00:00
|
|
|
result.push_back(Err(ShellError::labeled_error_with_secondary(
|
|
|
|
"Expected a string from pipeline",
|
|
|
|
"requires string input",
|
2019-10-13 04:12:43 +00:00
|
|
|
&tag,
|
2019-08-05 08:54:29 +00:00
|
|
|
"value originates from here",
|
2019-09-14 16:30:24 +00:00
|
|
|
v.tag(),
|
2019-07-03 20:31:15 +00:00
|
|
|
)));
|
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
|
|
|
}
|