From 6497421615131a4df3cab539ff690c5eff293bab Mon Sep 17 00:00:00 2001 From: k-brk <25877802+k-brk@users.noreply.github.com> Date: Fri, 17 Jul 2020 21:06:48 +0200 Subject: [PATCH] Keep until and while as subcommands of keep (#2197) --- crates/nu-cli/src/commands.rs | 6 +----- .../src/commands/{keep.rs => keep/command.rs} | 14 +++++++------- crates/nu-cli/src/commands/keep/mod.rs | 7 +++++++ .../src/commands/{keep_until.rs => keep/until.rs} | 14 +++++++------- .../src/commands/{keep_while.rs => keep/while_.rs} | 14 +++++++------- crates/nu-cli/tests/commands/{ => keep}/keep.rs | 0 crates/nu-cli/tests/commands/keep/mod.rs | 3 +++ .../commands/{keep_until.rs => keep/until.rs} | 4 ++-- .../commands/{keep_while.rs => keep/while_.rs} | 4 ++-- crates/nu-cli/tests/commands/mod.rs | 2 -- 10 files changed, 36 insertions(+), 32 deletions(-) rename crates/nu-cli/src/commands/{keep.rs => keep/command.rs} (87%) create mode 100644 crates/nu-cli/src/commands/keep/mod.rs rename crates/nu-cli/src/commands/{keep_until.rs => keep/until.rs} (93%) rename crates/nu-cli/src/commands/{keep_while.rs => keep/while_.rs} (93%) rename crates/nu-cli/tests/commands/{ => keep}/keep.rs (100%) create mode 100644 crates/nu-cli/tests/commands/keep/mod.rs rename crates/nu-cli/tests/commands/{keep_until.rs => keep/until.rs} (92%) rename crates/nu-cli/tests/commands/{keep_while.rs => keep/while_.rs} (92%) diff --git a/crates/nu-cli/src/commands.rs b/crates/nu-cli/src/commands.rs index 97436598d2..45e06f115e 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -67,8 +67,6 @@ pub(crate) mod if_; pub(crate) mod insert; pub(crate) mod is_empty; pub(crate) mod keep; -pub(crate) mod keep_until; -pub(crate) mod keep_while; pub(crate) mod last; pub(crate) mod lines; pub(crate) mod ls; @@ -196,9 +194,7 @@ pub(crate) use help::Help; pub(crate) use histogram::Histogram; pub(crate) use history::History; pub(crate) use insert::Insert; -pub(crate) use keep::Keep; -pub(crate) use keep_until::KeepUntil; -pub(crate) use keep_while::KeepWhile; +pub(crate) use keep::{Keep, KeepUntil, KeepWhile}; pub(crate) use last::Last; pub(crate) use lines::Lines; pub(crate) use ls::Ls; diff --git a/crates/nu-cli/src/commands/keep.rs b/crates/nu-cli/src/commands/keep/command.rs similarity index 87% rename from crates/nu-cli/src/commands/keep.rs rename to crates/nu-cli/src/commands/keep/command.rs index 5ba94cd60e..ec3484c5cb 100644 --- a/crates/nu-cli/src/commands/keep.rs +++ b/crates/nu-cli/src/commands/keep/command.rs @@ -5,15 +5,15 @@ use nu_errors::ShellError; use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; use nu_source::Tagged; -pub struct Keep; +pub struct Command; #[derive(Deserialize)] -pub struct KeepArgs { +pub struct Arguments { rows: Option>, } #[async_trait] -impl WholeStreamCommand for Keep { +impl WholeStreamCommand for Command { fn name(&self) -> &str { "keep" } @@ -22,7 +22,7 @@ impl WholeStreamCommand for Keep { Signature::build("keep").optional( "rows", SyntaxShape::Int, - "starting from the front, the number of rows to keep", + "Starting from the front, the number of rows to keep", ) } @@ -61,7 +61,7 @@ impl WholeStreamCommand for Keep { async fn keep(args: CommandArgs, registry: &CommandRegistry) -> Result { let registry = registry.clone(); - let (KeepArgs { rows }, input) = args.process(®istry).await?; + let (Arguments { rows }, input) = args.process(®istry).await?; let rows_desired = if let Some(quantity) = rows { *quantity } else { @@ -73,12 +73,12 @@ async fn keep(args: CommandArgs, registry: &CommandRegistry) -> Result &str { - "keep-until" + "keep until" } fn signature(&self) -> Signature { - Signature::build("keep-until") + Signature::build("keep until") .required( "condition", SyntaxShape::Math, - "the condition that must be met to stop keeping rows", + "The condition that must be met to stop keeping rows", ) .filter() } @@ -109,12 +109,12 @@ impl WholeStreamCommand for KeepUntil { #[cfg(test)] mod tests { - use super::KeepUntil; + use super::SubCommand; #[test] fn examples_work_as_expected() { use crate::examples::test as test_examples; - test_examples(KeepUntil {}) + test_examples(SubCommand {}) } } diff --git a/crates/nu-cli/src/commands/keep_while.rs b/crates/nu-cli/src/commands/keep/while_.rs similarity index 93% rename from crates/nu-cli/src/commands/keep_while.rs rename to crates/nu-cli/src/commands/keep/while_.rs index 42554d3330..bc60788552 100644 --- a/crates/nu-cli/src/commands/keep_while.rs +++ b/crates/nu-cli/src/commands/keep/while_.rs @@ -5,20 +5,20 @@ use log::trace; use nu_errors::ShellError; use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value}; -pub struct KeepWhile; +pub struct SubCommand; #[async_trait] -impl WholeStreamCommand for KeepWhile { +impl WholeStreamCommand for SubCommand { fn name(&self) -> &str { - "keep-while" + "keep while" } fn signature(&self) -> Signature { - Signature::build("keep-while") + Signature::build("keep while") .required( "condition", SyntaxShape::Math, - "the condition that must be met to keep rows", + "The condition that must be met to keep rows", ) .filter() } @@ -109,12 +109,12 @@ impl WholeStreamCommand for KeepWhile { #[cfg(test)] mod tests { - use super::KeepWhile; + use super::SubCommand; #[test] fn examples_work_as_expected() { use crate::examples::test as test_examples; - test_examples(KeepWhile {}) + test_examples(SubCommand {}) } } diff --git a/crates/nu-cli/tests/commands/keep.rs b/crates/nu-cli/tests/commands/keep/keep.rs similarity index 100% rename from crates/nu-cli/tests/commands/keep.rs rename to crates/nu-cli/tests/commands/keep/keep.rs diff --git a/crates/nu-cli/tests/commands/keep/mod.rs b/crates/nu-cli/tests/commands/keep/mod.rs new file mode 100644 index 0000000000..3202079b15 --- /dev/null +++ b/crates/nu-cli/tests/commands/keep/mod.rs @@ -0,0 +1,3 @@ +mod keep; +mod until; +mod while_; diff --git a/crates/nu-cli/tests/commands/keep_until.rs b/crates/nu-cli/tests/commands/keep/until.rs similarity index 92% rename from crates/nu-cli/tests/commands/keep_until.rs rename to crates/nu-cli/tests/commands/keep/until.rs index 8df548d5e9..a7914f7cc4 100644 --- a/crates/nu-cli/tests/commands/keep_until.rs +++ b/crates/nu-cli/tests/commands/keep/until.rs @@ -4,7 +4,7 @@ use nu_test_support::{nu, pipeline}; #[test] fn condition_is_met() { - Playground::setup("keep-until_test_1", |dirs, sandbox| { + Playground::setup("keep_until_test_1", |dirs, sandbox| { sandbox.with_files(vec![FileWithContentToBeTrimmed( "caballeros.txt", r#" @@ -38,7 +38,7 @@ fn condition_is_met() { | split column ',' | headers | skip while "Chicken Collection" != "Blue Chickens" - | keep-until "Chicken Collection" == "Red Chickens" + | keep until "Chicken Collection" == "Red Chickens" | skip 1 | str to-int "31/04/2020" | get "31/04/2020" diff --git a/crates/nu-cli/tests/commands/keep_while.rs b/crates/nu-cli/tests/commands/keep/while_.rs similarity index 92% rename from crates/nu-cli/tests/commands/keep_while.rs rename to crates/nu-cli/tests/commands/keep/while_.rs index 87eeaade48..77440484de 100644 --- a/crates/nu-cli/tests/commands/keep_while.rs +++ b/crates/nu-cli/tests/commands/keep/while_.rs @@ -4,7 +4,7 @@ use nu_test_support::{nu, pipeline}; #[test] fn condition_is_met() { - Playground::setup("keep-while_test_1", |dirs, sandbox| { + Playground::setup("keep_while_test_1", |dirs, sandbox| { sandbox.with_files(vec![FileWithContentToBeTrimmed( "caballeros.txt", r#" @@ -38,7 +38,7 @@ fn condition_is_met() { | split column ',' | headers | skip 1 - | keep-while "Chicken Collection" != "Blue Chickens" + | keep while "Chicken Collection" != "Blue Chickens" | str to-int "31/04/2020" | get "31/04/2020" | math sum diff --git a/crates/nu-cli/tests/commands/mod.rs b/crates/nu-cli/tests/commands/mod.rs index 208d910b0a..9e1361227d 100644 --- a/crates/nu-cli/tests/commands/mod.rs +++ b/crates/nu-cli/tests/commands/mod.rs @@ -23,8 +23,6 @@ mod histogram; mod insert; mod is_empty; mod keep; -mod keep_until; -mod keep_while; mod last; mod lines; mod ls;