mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
Remove the -n flag from shuffle (#1823)
This commit is contained in:
parent
334685af23
commit
b6cdfb1b19
@ -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 New Issue
Block a user