2021-09-29 20:25:05 +02:00
|
|
|
use nu_protocol::ast::Call;
|
2021-10-25 18:58:58 +02:00
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
2021-11-17 05:22:37 +01:00
|
|
|
use nu_protocol::{
|
|
|
|
Category, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Value,
|
|
|
|
};
|
2021-09-29 20:25:05 +02:00
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
#[derive(Clone)]
|
2021-09-29 20:25:05 +02:00
|
|
|
pub struct Lines;
|
|
|
|
|
|
|
|
const SPLIT_CHAR: char = '\n';
|
|
|
|
|
|
|
|
impl Command for Lines {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"lines"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Converts input to lines"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
2021-11-17 05:22:37 +01:00
|
|
|
Signature::build("lines").category(Category::Filters)
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2021-10-28 06:13:10 +02:00
|
|
|
engine_state: &EngineState,
|
2021-10-25 08:31:39 +02:00
|
|
|
_stack: &mut Stack,
|
2021-09-29 20:25:05 +02:00
|
|
|
call: &Call,
|
2021-10-25 06:01:02 +02:00
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
2021-09-29 20:25:05 +02:00
|
|
|
match input {
|
|
|
|
#[allow(clippy::needless_collect)]
|
|
|
|
// Collect is needed because the string may not live long enough for
|
|
|
|
// the Rc structure to continue using it. If split could take ownership
|
|
|
|
// of the split values, then this wouldn't be needed
|
2021-10-25 06:01:02 +02:00
|
|
|
PipelineData::Value(Value::String { val, span }) => {
|
2021-09-29 20:25:05 +02:00
|
|
|
let lines = val
|
|
|
|
.split(SPLIT_CHAR)
|
|
|
|
.map(|s| s.to_string())
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
|
|
let iter = lines.into_iter().filter_map(move |s| {
|
|
|
|
if !s.is_empty() {
|
2021-10-20 07:58:25 +02:00
|
|
|
Some(Value::string(s, span))
|
2021-09-29 20:25:05 +02:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-10-28 06:13:10 +02:00
|
|
|
Ok(iter.into_pipeline_data(engine_state.ctrlc.clone()))
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
2021-10-25 06:01:02 +02:00
|
|
|
PipelineData::Stream(stream) => {
|
2021-09-29 20:25:05 +02:00
|
|
|
let iter = stream
|
|
|
|
.into_iter()
|
|
|
|
.filter_map(|value| {
|
|
|
|
if let Value::String { val, span } = value {
|
|
|
|
let inner = val
|
|
|
|
.split(SPLIT_CHAR)
|
|
|
|
.filter_map(|s| {
|
|
|
|
if !s.is_empty() {
|
|
|
|
Some(Value::String {
|
2021-10-02 23:56:11 +02:00
|
|
|
val: s.into(),
|
2021-09-29 20:25:05 +02:00
|
|
|
span,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<Value>>();
|
|
|
|
|
|
|
|
Some(inner)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.flatten();
|
|
|
|
|
2021-10-28 06:13:10 +02:00
|
|
|
Ok(iter.into_pipeline_data(engine_state.ctrlc.clone()))
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
2021-10-25 06:01:02 +02:00
|
|
|
PipelineData::Value(val) => Err(ShellError::UnsupportedInput(
|
2021-09-29 20:25:05 +02:00
|
|
|
format!("Not supported input: {}", val.as_string()?),
|
|
|
|
call.head,
|
|
|
|
)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|