mirror of
https://github.com/nushell/nushell.git
synced 2025-02-22 21:41:26 +01:00
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:
parent
74717582ac
commit
85d848dd7d
@ -35,20 +35,7 @@ impl WholeStreamCommand for Drop {
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
let (DropArgs { rows }, input) = args.process(®istry).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(®istry).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;
|
||||
|
@ -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");
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user