Stream results of drop command (#2114)

* Stream results of drop command

* When the amount of rows to drop is equal to or greaten than the size of the table, output nothing
This commit is contained in:
Joseph T. Lyons 2020-07-05 13:46:06 -04:00 committed by GitHub
parent 74717582ac
commit 85d848dd7d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 15 deletions

View File

@ -35,20 +35,7 @@ impl WholeStreamCommand for Drop {
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let (DropArgs { rows }, input) = args.process(&registry).await?;
let mut v: Vec<_> = input.into_vec().await;
let rows_to_drop = if let Some(quantity) = rows {
*quantity as usize
} else {
1
};
for _ in 0..rows_to_drop {
v.pop();
}
Ok(futures::stream::iter(v).to_output_stream())
drop(args, registry).await
}
fn examples(&self) -> Vec<Example> {
@ -70,6 +57,31 @@ impl WholeStreamCommand for Drop {
}
}
async fn drop(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let (DropArgs { rows }, input) = args.process(&registry).await?;
let v: Vec<_> = input.into_vec().await;
let rows_to_drop = if let Some(quantity) = rows {
*quantity as usize
} else {
1
};
Ok(if rows_to_drop == 0 {
futures::stream::iter(v).to_output_stream()
} else {
let k = if v.len() < rows_to_drop {
0
} else {
v.len() - rows_to_drop
};
let iter = v.into_iter().take(k);
futures::stream::iter(iter).to_output_stream()
})
}
#[cfg(test)]
mod tests {
use super::Drop;

View File

@ -1,4 +1,4 @@
use nu_test_support::nu;
use nu_test_support::{nu, pipeline};
#[test]
fn drop_rows() {
@ -9,3 +9,15 @@ fn drop_rows() {
assert_eq!(actual.out, "3");
}
#[test]
fn drop_more_rows_than_table_has() {
let actual = nu!(
cwd: ".", pipeline(
r#"
date | drop 50 | count
"#
));
assert_eq!(actual.out, "0");
}