From a93a9b9029c44aef535765bc4541402151b4b264 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C8=98tefan?= <71919805+onthebridgetonowhere@users.noreply.github.com> Date: Tue, 21 Dec 2021 21:24:11 +0100 Subject: [PATCH] Add skip-empty flag to lines command (#543) * Add skip-empty flag to lines command * Fix failing length test --- crates/nu-command/src/filters/lines.rs | 19 +++++++++++-------- src/tests.rs | 2 +- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/crates/nu-command/src/filters/lines.rs b/crates/nu-command/src/filters/lines.rs index d0f1f599ea..aaa985be8c 100644 --- a/crates/nu-command/src/filters/lines.rs +++ b/crates/nu-command/src/filters/lines.rs @@ -19,7 +19,9 @@ impl Command for Lines { } 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( @@ -29,6 +31,7 @@ impl Command for Lines { call: &Call, input: PipelineData, ) -> Result { + let skip_empty = call.has_flag("skip-emtpy"); match input { #[allow(clippy::needless_collect)] // Collect is needed because the string may not live long enough for @@ -41,10 +44,10 @@ impl Command for Lines { .collect::>(); let iter = lines.into_iter().filter_map(move |s| { - if !s.is_empty() { - Some(Value::string(s, span)) - } else { + if skip_empty && s.is_empty() { None + } else { + Some(Value::string(s, span)) } }); @@ -53,18 +56,18 @@ impl Command for Lines { PipelineData::Stream(stream, ..) => { let iter = stream .into_iter() - .filter_map(|value| { + .filter_map(move |value| { if let Value::String { val, span } = value { let inner = val .split(SPLIT_CHAR) .filter_map(|s| { - if !s.is_empty() { + if skip_empty && s.is_empty() { + None + } else { Some(Value::String { val: s.into(), span, }) - } else { - None } }) .collect::>(); diff --git a/src/tests.rs b/src/tests.rs index fc5bf33838..097080a6d4 100644 --- a/src/tests.rs +++ b/src/tests.rs @@ -1038,7 +1038,7 @@ fn long_flag() -> TestResult { #[test] fn help_works_with_missing_requirements() -> TestResult { - run_test(r#"each --help | lines | length"#, "10") + run_test(r#"each --help | lines | length"#, "15") } #[test]