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 {
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<nu_protocol::PipelineData, nu_protocol::ShellError> {
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::<Vec<String>>();
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::<Vec<Value>>();

View File

@ -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]