mirror of
https://github.com/nushell/nushell
synced 2024-12-28 14:03:09 +00:00
Remove the -n flag from shuffle (#1823)
This commit is contained in:
parent
334685af23
commit
b6cdfb1b19
1 changed files with 4 additions and 24 deletions
|
@ -2,34 +2,18 @@ use crate::commands::WholeStreamCommand;
|
|||
use crate::context::CommandRegistry;
|
||||
use crate::prelude::*;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{ReturnSuccess, ReturnValue, Signature, SyntaxShape, Value};
|
||||
use nu_source::Tagged;
|
||||
use nu_protocol::{ReturnSuccess, ReturnValue, Value};
|
||||
|
||||
use rand::seq::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
|
||||
pub struct Shuffle;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
pub struct Arguments {
|
||||
#[serde(rename = "num")]
|
||||
limit: Option<Tagged<u64>>,
|
||||
}
|
||||
|
||||
impl WholeStreamCommand for Shuffle {
|
||||
fn name(&self) -> &str {
|
||||
"shuffle"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("shuffle").named(
|
||||
"num",
|
||||
SyntaxShape::Int,
|
||||
"Limit `num` number of rows",
|
||||
Some('n'),
|
||||
)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"Shuffle rows randomly."
|
||||
}
|
||||
|
@ -43,16 +27,12 @@ impl WholeStreamCommand for Shuffle {
|
|||
}
|
||||
}
|
||||
|
||||
fn shuffle(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||
let registry = registry.clone();
|
||||
fn shuffle(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||
let stream = async_stream! {
|
||||
let (Arguments { limit }, mut input) = args.process(®istry).await?;
|
||||
let mut input = args.input;
|
||||
let mut values: Vec<Value> = input.collect().await;
|
||||
|
||||
let out = if let Some(n) = limit {
|
||||
let (shuffled, _) = values.partial_shuffle(&mut thread_rng(), *n as usize);
|
||||
shuffled.to_vec()
|
||||
} else {
|
||||
let out = {
|
||||
values.shuffle(&mut thread_rng());
|
||||
values.clone()
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue