Adds drop number of rows command (#1663)

* Fix access to columns with quoted names

* Add a drop number of rows from end of table
This commit is contained in:
Jonathan Turner 2020-04-26 18:34:45 +12:00 committed by GitHub
parent df90d9e4b6
commit 2ac501f88e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 0 deletions

View File

@ -297,6 +297,7 @@ pub fn create_default_context(
whole_stream_command(Last), whole_stream_command(Last),
whole_stream_command(Skip), whole_stream_command(Skip),
whole_stream_command(Nth), whole_stream_command(Nth),
whole_stream_command(Drop),
per_item_command(Format), per_item_command(Format),
per_item_command(Where), per_item_command(Where),
whole_stream_command(Compact), whole_stream_command(Compact),

View File

@ -20,6 +20,7 @@ pub(crate) mod cp;
pub(crate) mod date; pub(crate) mod date;
pub(crate) mod debug; pub(crate) mod debug;
pub(crate) mod default; pub(crate) mod default;
pub(crate) mod drop;
pub(crate) mod du; pub(crate) mod du;
pub(crate) mod each; pub(crate) mod each;
pub(crate) mod echo; pub(crate) mod echo;
@ -129,6 +130,7 @@ pub(crate) use cp::Cpy;
pub(crate) use date::Date; pub(crate) use date::Date;
pub(crate) use debug::Debug; pub(crate) use debug::Debug;
pub(crate) use default::Default; pub(crate) use default::Default;
pub(crate) use drop::Drop;
pub(crate) use du::Du; pub(crate) use du::Du;
pub(crate) use each::Each; pub(crate) use each::Each;
pub(crate) use echo::Echo; pub(crate) use echo::Echo;

View File

@ -0,0 +1,60 @@
use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, SyntaxShape, Value};
use nu_source::Tagged;
pub struct Drop;
#[derive(Deserialize)]
pub struct DropArgs {
rows: Option<Tagged<u64>>,
}
impl WholeStreamCommand for Drop {
fn name(&self) -> &str {
"drop"
}
fn signature(&self) -> Signature {
Signature::build("drop").optional(
"rows",
SyntaxShape::Number,
"starting from the back, the number of rows to drop",
)
}
fn usage(&self) -> &str {
"Drop the last number of rows."
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, drop)?.run()
}
}
fn drop(DropArgs { rows }: DropArgs, context: RunnableContext) -> Result<OutputStream, ShellError> {
let stream = async_stream! {
let v: Vec<_> = context.input.into_vec().await;
let rows_to_drop = if let Some(quantity) = rows {
*quantity as usize
} else {
1
};
if rows_to_drop < v.len() {
let k = v.len() - rows_to_drop;
for x in v[0..k].iter() {
let y: Value = x.clone();
yield ReturnSuccess::value(y)
}
}
};
Ok(stream.to_output_stream())
}

View File

@ -0,0 +1,11 @@
use nu_test_support::nu;
#[test]
fn drop_rows() {
let actual = nu!(
cwd: "tests/fixtures/formats",
r#"echo '[{"foo": 3}, {"foo": 8}, {"foo": 4}]' | from-json | drop 2 | get foo | sum | echo $it"#
);
assert_eq!(actual, "3");
}

View File

@ -5,6 +5,7 @@ mod cd;
mod compact; mod compact;
mod cp; mod cp;
mod default; mod default;
mod drop;
mod each; mod each;
mod edit; mod edit;
mod enter; mod enter;