2020-03-10 23:00:08 +01:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::context::CommandRegistry;
|
|
|
|
use crate::prelude::*;
|
|
|
|
use nu_errors::ShellError;
|
2020-05-18 09:12:35 +02:00
|
|
|
use nu_protocol::{ReturnSuccess, ReturnValue, Value};
|
2020-03-10 23:00:08 +01:00
|
|
|
|
|
|
|
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<OutputStream, ShellError> {
|
2020-05-16 05:18:24 +02:00
|
|
|
shuffle(args, registry)
|
2020-03-10 23:00:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-18 09:12:35 +02:00
|
|
|
fn shuffle(args: CommandArgs, _registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
2020-03-10 23:00:08 +01:00
|
|
|
let stream = async_stream! {
|
2020-05-18 09:12:35 +02:00
|
|
|
let mut input = args.input;
|
Move external closer to internal (#1611)
* Refactor InputStream and affected commands.
First, making `values` private and leaning on the `Stream` implementation makes
consumes of `InputStream` less likely to have to change in the future, if we
change what an `InputStream` is internally.
Second, we're dropping `Option<InputStream>` as the input to pipelines,
internals, and externals. Instead, `InputStream.is_empty` can be used to check
for "emptiness". Empty streams are typically only ever used as the first input
to a pipeline.
* Add run_external internal command.
We want to push external commands closer to internal commands, eventually
eliminating the concept of "external" completely. This means we can consolidate
a couple of things:
- Variable evaluation (for example, `$it`, `$nu`, alias vars)
- Behaviour of whole stream vs per-item external execution
It should also make it easier for us to start introducing argument signatures
for external commands,
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
* Update run_external.rs
Co-authored-by: Jonathan Turner <jonathandturner@users.noreply.github.com>
2020-04-20 05:30:44 +02:00
|
|
|
let mut values: Vec<Value> = input.collect().await;
|
2020-03-10 23:00:08 +01:00
|
|
|
|
2020-05-18 09:12:35 +02:00
|
|
|
let out = {
|
2020-03-10 23:00:08 +01:00
|
|
|
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())
|
|
|
|
}
|
2020-05-18 14:56:01 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::Shuffle;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn examples_work_as_expected() {
|
|
|
|
use crate::examples::test as test_examples;
|
|
|
|
|
|
|
|
test_examples(Shuffle {})
|
|
|
|
}
|
|
|
|
}
|