use crate::commands::WholeStreamCommand; use crate::context::CommandRegistry; use crate::prelude::*; use nu_errors::ShellError; use nu_protocol::{ReturnSuccess, ReturnValue, Value}; use rand::seq::SliceRandom; use rand::thread_rng; pub struct Shuffle; impl WholeStreamCommand for Shuffle { fn name(&self) -> &str { "shuffle" } fn usage(&self) -> &str { "Shuffle rows randomly." } fn run( &self, args: CommandArgs, registry: &CommandRegistry, ) -> Result { shuffle(args, registry) } } fn shuffle(args: CommandArgs, _registry: &CommandRegistry) -> Result { let stream = async_stream! { let mut input = args.input; let mut values: Vec = input.collect().await; let out = { values.shuffle(&mut thread_rng()); values.clone() }; for val in out.into_iter() { yield ReturnSuccess::value(val); } }; let stream: BoxStream<'static, ReturnValue> = stream.boxed(); Ok(stream.to_output_stream()) }