Keep until and while as subcommands of keep (#2197)

This commit is contained in:
k-brk 2020-07-17 21:06:48 +02:00 committed by GitHub
parent f26151e36d
commit 6497421615
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 36 additions and 32 deletions

View File

@ -67,8 +67,6 @@ pub(crate) mod if_;
pub(crate) mod insert; pub(crate) mod insert;
pub(crate) mod is_empty; pub(crate) mod is_empty;
pub(crate) mod keep; pub(crate) mod keep;
pub(crate) mod keep_until;
pub(crate) mod keep_while;
pub(crate) mod last; pub(crate) mod last;
pub(crate) mod lines; pub(crate) mod lines;
pub(crate) mod ls; pub(crate) mod ls;
@ -196,9 +194,7 @@ pub(crate) use help::Help;
pub(crate) use histogram::Histogram; pub(crate) use histogram::Histogram;
pub(crate) use history::History; pub(crate) use history::History;
pub(crate) use insert::Insert; pub(crate) use insert::Insert;
pub(crate) use keep::Keep; pub(crate) use keep::{Keep, KeepUntil, KeepWhile};
pub(crate) use keep_until::KeepUntil;
pub(crate) use keep_while::KeepWhile;
pub(crate) use last::Last; pub(crate) use last::Last;
pub(crate) use lines::Lines; pub(crate) use lines::Lines;
pub(crate) use ls::Ls; pub(crate) use ls::Ls;

View File

@ -5,15 +5,15 @@ use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape, UntaggedValue}; use nu_protocol::{Signature, SyntaxShape, UntaggedValue};
use nu_source::Tagged; use nu_source::Tagged;
pub struct Keep; pub struct Command;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct KeepArgs { pub struct Arguments {
rows: Option<Tagged<usize>>, rows: Option<Tagged<usize>>,
} }
#[async_trait] #[async_trait]
impl WholeStreamCommand for Keep { impl WholeStreamCommand for Command {
fn name(&self) -> &str { fn name(&self) -> &str {
"keep" "keep"
} }
@ -22,7 +22,7 @@ impl WholeStreamCommand for Keep {
Signature::build("keep").optional( Signature::build("keep").optional(
"rows", "rows",
SyntaxShape::Int, 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<OutputStream, ShellError> { async fn keep(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let registry = registry.clone(); let registry = registry.clone();
let (KeepArgs { rows }, input) = args.process(&registry).await?; let (Arguments { rows }, input) = args.process(&registry).await?;
let rows_desired = if let Some(quantity) = rows { let rows_desired = if let Some(quantity) = rows {
*quantity *quantity
} else { } else {
@ -73,12 +73,12 @@ async fn keep(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStr
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::Keep; use super::Command;
#[test] #[test]
fn examples_work_as_expected() { fn examples_work_as_expected() {
use crate::examples::test as test_examples; use crate::examples::test as test_examples;
test_examples(Keep {}) test_examples(Command {})
} }
} }

View File

@ -0,0 +1,7 @@
mod command;
mod until;
mod while_;
pub use command::Command as Keep;
pub use until::SubCommand as KeepUntil;
pub use while_::SubCommand as KeepWhile;

View File

@ -5,20 +5,20 @@ use log::trace;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value}; use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value};
pub struct KeepUntil; pub struct SubCommand;
#[async_trait] #[async_trait]
impl WholeStreamCommand for KeepUntil { impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str { fn name(&self) -> &str {
"keep-until" "keep until"
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("keep-until") Signature::build("keep until")
.required( .required(
"condition", "condition",
SyntaxShape::Math, SyntaxShape::Math,
"the condition that must be met to stop keeping rows", "The condition that must be met to stop keeping rows",
) )
.filter() .filter()
} }
@ -109,12 +109,12 @@ impl WholeStreamCommand for KeepUntil {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::KeepUntil; use super::SubCommand;
#[test] #[test]
fn examples_work_as_expected() { fn examples_work_as_expected() {
use crate::examples::test as test_examples; use crate::examples::test as test_examples;
test_examples(KeepUntil {}) test_examples(SubCommand {})
} }
} }

View File

@ -5,20 +5,20 @@ use log::trace;
use nu_errors::ShellError; use nu_errors::ShellError;
use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value}; use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value};
pub struct KeepWhile; pub struct SubCommand;
#[async_trait] #[async_trait]
impl WholeStreamCommand for KeepWhile { impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str { fn name(&self) -> &str {
"keep-while" "keep while"
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("keep-while") Signature::build("keep while")
.required( .required(
"condition", "condition",
SyntaxShape::Math, SyntaxShape::Math,
"the condition that must be met to keep rows", "The condition that must be met to keep rows",
) )
.filter() .filter()
} }
@ -109,12 +109,12 @@ impl WholeStreamCommand for KeepWhile {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::KeepWhile; use super::SubCommand;
#[test] #[test]
fn examples_work_as_expected() { fn examples_work_as_expected() {
use crate::examples::test as test_examples; use crate::examples::test as test_examples;
test_examples(KeepWhile {}) test_examples(SubCommand {})
} }
} }

View File

@ -0,0 +1,3 @@
mod keep;
mod until;
mod while_;

View File

@ -4,7 +4,7 @@ use nu_test_support::{nu, pipeline};
#[test] #[test]
fn condition_is_met() { 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( sandbox.with_files(vec![FileWithContentToBeTrimmed(
"caballeros.txt", "caballeros.txt",
r#" r#"
@ -38,7 +38,7 @@ fn condition_is_met() {
| split column ',' | split column ','
| headers | headers
| skip while "Chicken Collection" != "Blue Chickens" | skip while "Chicken Collection" != "Blue Chickens"
| keep-until "Chicken Collection" == "Red Chickens" | keep until "Chicken Collection" == "Red Chickens"
| skip 1 | skip 1
| str to-int "31/04/2020" | str to-int "31/04/2020"
| get "31/04/2020" | get "31/04/2020"

View File

@ -4,7 +4,7 @@ use nu_test_support::{nu, pipeline};
#[test] #[test]
fn condition_is_met() { 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( sandbox.with_files(vec![FileWithContentToBeTrimmed(
"caballeros.txt", "caballeros.txt",
r#" r#"
@ -38,7 +38,7 @@ fn condition_is_met() {
| split column ',' | split column ','
| headers | headers
| skip 1 | skip 1
| keep-while "Chicken Collection" != "Blue Chickens" | keep while "Chicken Collection" != "Blue Chickens"
| str to-int "31/04/2020" | str to-int "31/04/2020"
| get "31/04/2020" | get "31/04/2020"
| math sum | math sum

View File

@ -23,8 +23,6 @@ mod histogram;
mod insert; mod insert;
mod is_empty; mod is_empty;
mod keep; mod keep;
mod keep_until;
mod keep_while;
mod last; mod last;
mod lines; mod lines;
mod ls; mod ls;