mirror of
https://github.com/nushell/nushell
synced 2025-01-15 22:54:16 +00:00
Keep until and while as subcommands of keep (#2197)
This commit is contained in:
parent
f26151e36d
commit
6497421615
10 changed files with 36 additions and 32 deletions
|
@ -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;
|
||||
|
|
|
@ -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<Tagged<usize>>,
|
||||
}
|
||||
|
||||
#[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<OutputStream, ShellError> {
|
||||
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<OutputStr
|
|||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::Keep;
|
||||
use super::Command;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() {
|
||||
use crate::examples::test as test_examples;
|
||||
|
||||
test_examples(Keep {})
|
||||
test_examples(Command {})
|
||||
}
|
||||
}
|
7
crates/nu-cli/src/commands/keep/mod.rs
Normal file
7
crates/nu-cli/src/commands/keep/mod.rs
Normal 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;
|
|
@ -5,20 +5,20 @@ use log::trace;
|
|||
use nu_errors::ShellError;
|
||||
use nu_protocol::{hir::ClassifiedCommand, Signature, SyntaxShape, UntaggedValue, Value};
|
||||
|
||||
pub struct KeepUntil;
|
||||
pub struct SubCommand;
|
||||
|
||||
#[async_trait]
|
||||
impl WholeStreamCommand for KeepUntil {
|
||||
impl WholeStreamCommand for SubCommand {
|
||||
fn name(&self) -> &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 {})
|
||||
}
|
||||
}
|
|
@ -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 {})
|
||||
}
|
||||
}
|
3
crates/nu-cli/tests/commands/keep/mod.rs
Normal file
3
crates/nu-cli/tests/commands/keep/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
|||
mod keep;
|
||||
mod until;
|
||||
mod while_;
|
|
@ -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"
|
|
@ -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
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue