Add skip-empty flag to lines command (#543)

* Add skip-empty flag to lines command

* Fix failing length test
This commit is contained in:
Ștefan 2021-12-21 21:24:11 +01:00 committed by GitHub
parent 6a35e6b7b6
commit a93a9b9029
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 9 deletions

View File

@ -19,7 +19,9 @@ impl Command for Lines {
} }
fn signature(&self) -> nu_protocol::Signature { fn signature(&self) -> nu_protocol::Signature {
Signature::build("lines").category(Category::Filters) Signature::build("lines")
.switch("skip-empty", "skip empty lines", Some('s'))
.category(Category::Filters)
} }
fn run( fn run(
@ -29,6 +31,7 @@ impl Command for Lines {
call: &Call, call: &Call,
input: PipelineData, input: PipelineData,
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> { ) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
let skip_empty = call.has_flag("skip-emtpy");
match input { match input {
#[allow(clippy::needless_collect)] #[allow(clippy::needless_collect)]
// Collect is needed because the string may not live long enough for // Collect is needed because the string may not live long enough for
@ -41,10 +44,10 @@ impl Command for Lines {
.collect::<Vec<String>>(); .collect::<Vec<String>>();
let iter = lines.into_iter().filter_map(move |s| { let iter = lines.into_iter().filter_map(move |s| {
if !s.is_empty() { if skip_empty && s.is_empty() {
Some(Value::string(s, span))
} else {
None None
} else {
Some(Value::string(s, span))
} }
}); });
@ -53,18 +56,18 @@ impl Command for Lines {
PipelineData::Stream(stream, ..) => { PipelineData::Stream(stream, ..) => {
let iter = stream let iter = stream
.into_iter() .into_iter()
.filter_map(|value| { .filter_map(move |value| {
if let Value::String { val, span } = value { if let Value::String { val, span } = value {
let inner = val let inner = val
.split(SPLIT_CHAR) .split(SPLIT_CHAR)
.filter_map(|s| { .filter_map(|s| {
if !s.is_empty() { if skip_empty && s.is_empty() {
None
} else {
Some(Value::String { Some(Value::String {
val: s.into(), val: s.into(),
span, span,
}) })
} else {
None
} }
}) })
.collect::<Vec<Value>>(); .collect::<Vec<Value>>();

View File

@ -1038,7 +1038,7 @@ fn long_flag() -> TestResult {
#[test] #[test]
fn help_works_with_missing_requirements() -> TestResult { fn help_works_with_missing_requirements() -> TestResult {
run_test(r#"each --help | lines | length"#, "10") run_test(r#"each --help | lines | length"#, "15")
} }
#[test] #[test]