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;
|
|
|
|
|
|
|
|
impl Command for Lines {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"lines"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Converts input to lines"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
2021-12-21 21:24:11 +01:00
|
|
|
Signature::build("lines")
|
|
|
|
.switch("skip-empty", "skip empty lines", Some('s'))
|
|
|
|
.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-12-24 08:22:11 +01: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-12-24 08:22:11 +01:00
|
|
|
let head = call.head;
|
2021-12-21 21:24:11 +01:00
|
|
|
let skip_empty = call.has_flag("skip-emtpy");
|
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-12-02 06:59:10 +01:00
|
|
|
PipelineData::Value(Value::String { val, span }, ..) => {
|
2021-12-27 00:11:18 +01:00
|
|
|
let split_char = if val.contains("\r\n") { "\r\n" } else { "\n" };
|
|
|
|
|
2021-09-29 20:25:05 +02:00
|
|
|
let lines = val
|
2021-12-27 00:11:18 +01:00
|
|
|
.split(split_char)
|
2021-09-29 20:25:05 +02:00
|
|
|
.map(|s| s.to_string())
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
|
|
let iter = lines.into_iter().filter_map(move |s| {
|
2021-12-21 21:24:11 +01:00
|
|
|
if skip_empty && s.is_empty() {
|
2021-09-29 20:25:05 +02:00
|
|
|
None
|
2021-12-21 21:24:11 +01:00
|
|
|
} else {
|
|
|
|
Some(Value::string(s, span))
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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-12-24 08:22:11 +01:00
|
|
|
PipelineData::ListStream(stream, ..) => {
|
2021-12-27 00:11:18 +01:00
|
|
|
let mut split_char = "\n";
|
|
|
|
|
2021-09-29 20:25:05 +02:00
|
|
|
let iter = stream
|
|
|
|
.into_iter()
|
2021-12-21 21:24:11 +01:00
|
|
|
.filter_map(move |value| {
|
2021-09-29 20:25:05 +02:00
|
|
|
if let Value::String { val, span } = value {
|
2021-12-27 00:11:18 +01:00
|
|
|
if split_char != "\r\n" && val.contains("\r\n") {
|
|
|
|
split_char = "\r\n";
|
|
|
|
}
|
|
|
|
|
2021-09-29 20:25:05 +02:00
|
|
|
let inner = val
|
2021-12-27 00:11:18 +01:00
|
|
|
.split(split_char)
|
2021-09-29 20:25:05 +02:00
|
|
|
.filter_map(|s| {
|
2021-12-21 21:24:11 +01:00
|
|
|
if skip_empty && s.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
2021-09-29 20:25:05 +02:00
|
|
|
Some(Value::String {
|
2021-10-02 23:56:11 +02:00
|
|
|
val: s.into(),
|
2021-09-29 20:25:05 +02:00
|
|
|
span,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.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-12-24 08:22:11 +01:00
|
|
|
PipelineData::StringStream(stream, span, ..) => {
|
2021-12-27 00:11:18 +01:00
|
|
|
let mut split_char = "\n";
|
|
|
|
|
2021-12-24 08:22:11 +01:00
|
|
|
let iter = stream
|
|
|
|
.into_iter()
|
|
|
|
.map(move |value| match value {
|
2021-12-27 00:11:18 +01:00
|
|
|
Ok(value) => {
|
|
|
|
if split_char != "\r\n" && value.contains("\r\n") {
|
|
|
|
split_char = "\r\n";
|
|
|
|
}
|
|
|
|
value
|
|
|
|
.split(split_char)
|
|
|
|
.filter_map(|s| {
|
|
|
|
if !s.is_empty() {
|
|
|
|
Some(Value::String {
|
|
|
|
val: s.into(),
|
|
|
|
span,
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<Value>>()
|
|
|
|
}
|
2021-12-24 08:22:11 +01:00
|
|
|
Err(err) => vec![Value::Error { error: err }],
|
|
|
|
})
|
|
|
|
.flatten();
|
|
|
|
|
|
|
|
Ok(iter.into_pipeline_data(engine_state.ctrlc.clone()))
|
|
|
|
}
|
2021-12-02 06:59:10 +01:00
|
|
|
PipelineData::Value(val, ..) => Err(ShellError::UnsupportedInput(
|
2021-09-29 20:25:05 +02:00
|
|
|
format!("Not supported input: {}", val.as_string()?),
|
|
|
|
call.head,
|
|
|
|
)),
|
2021-12-24 08:22:11 +01:00
|
|
|
PipelineData::ByteStream(..) => {
|
|
|
|
let config = stack.get_config()?;
|
|
|
|
|
|
|
|
//FIXME: Make sure this can fail in the future to let the user
|
|
|
|
//know to use a different encoding
|
|
|
|
let s = input.collect_string("", &config)?;
|
|
|
|
|
2021-12-27 00:11:18 +01:00
|
|
|
let split_char = if s.contains("\r\n") { "\r\n" } else { "\n" };
|
|
|
|
|
2021-12-24 08:22:11 +01:00
|
|
|
let lines = s
|
2021-12-27 00:11:18 +01:00
|
|
|
.split(split_char)
|
2021-12-24 08:22:11 +01:00
|
|
|
.map(|s| s.to_string())
|
|
|
|
.collect::<Vec<String>>();
|
|
|
|
|
|
|
|
let iter = lines.into_iter().filter_map(move |s| {
|
|
|
|
if skip_empty && s.is_empty() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Value::string(s, head))
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Ok(iter.into_pipeline_data(engine_state.ctrlc.clone()))
|
|
|
|
}
|
2021-09-29 20:25:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|