nushell/crates/nu-command/src/filters/lines.rs

152 lines
5.5 KiB
Rust
Raw Normal View History

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};
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 {
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,
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> {
let head = call.head;
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
PipelineData::Value(Value::String { val, span }, ..) => {
let split_char = if val.contains("\r\n") { "\r\n" } else { "\n" };
2021-09-29 20:25:05 +02:00
let lines = val
.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| {
if skip_empty && s.is_empty() {
2021-09-29 20:25:05 +02:00
None
} 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
}
PipelineData::ListStream(stream, ..) => {
let mut split_char = "\n";
2021-09-29 20:25:05 +02:00
let iter = stream
.into_iter()
.filter_map(move |value| {
2021-09-29 20:25:05 +02:00
if let Value::String { val, span } = value {
if split_char != "\r\n" && val.contains("\r\n") {
split_char = "\r\n";
}
2021-09-29 20:25:05 +02:00
let inner = val
.split(split_char)
2021-09-29 20:25:05 +02:00
.filter_map(|s| {
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
}
PipelineData::StringStream(stream, span, ..) => {
let mut split_char = "\n";
let iter = stream
.into_iter()
.map(move |value| match value {
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>>()
}
Err(err) => vec![Value::Error { error: err }],
})
.flatten();
Ok(iter.into_pipeline_data(engine_state.ctrlc.clone()))
}
PipelineData::Value(val, ..) => Err(ShellError::UnsupportedInput(
2021-09-29 20:25:05 +02:00
format!("Not supported input: {}", val.as_string()?),
call.head,
)),
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)?;
let split_char = if s.contains("\r\n") { "\r\n" } else { "\n" };
let lines = s
.split(split_char)
.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
}
}
}