Port over the shuffle command from nushell (#300)

* initial commit of shuffle

* port the shuffle command from nushell
This commit is contained in:
Michael Angerman 2021-11-06 18:19:57 -07:00 committed by GitHub
parent a1f141d18a
commit 573cb38bab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 40 additions and 0 deletions

1
Cargo.lock generated
View File

@ -695,6 +695,7 @@ dependencies = [
"nu-protocol", "nu-protocol",
"nu-table", "nu-table",
"nu-term-grid", "nu-term-grid",
"rand",
"rayon", "rayon",
"sysinfo", "sysinfo",
"terminal_size", "terminal_size",

View File

@ -32,6 +32,7 @@ dialoguer = "0.9.0"
rayon = "1.5.1" rayon = "1.5.1"
titlecase = "1.1.0" titlecase = "1.1.0"
meval = "0.2.0" meval = "0.2.0"
rand = "0.8"
[features] [features]
trash-support = ["trash"] trash-support = ["trash"]

View File

@ -88,6 +88,7 @@ pub fn create_default_context() -> EngineState {
Range, Range,
Rm, Rm,
Select, Select,
Shuffle,
Size, Size,
ScreamingSnakeCase, ScreamingSnakeCase,
SnakeCase, SnakeCase,

View File

@ -7,6 +7,7 @@ mod lines;
mod par_each; mod par_each;
mod range; mod range;
mod select; mod select;
mod shuffle;
mod where_; mod where_;
mod wrap; mod wrap;
mod zip; mod zip;
@ -20,6 +21,7 @@ pub use lines::Lines;
pub use par_each::ParEach; pub use par_each::ParEach;
pub use range::Range; pub use range::Range;
pub use select::Select; pub use select::Select;
pub use shuffle::Shuffle;
pub use where_::Where; pub use where_::Where;
pub use wrap::Wrap; pub use wrap::Wrap;
pub use zip::Zip; pub use zip::Zip;

View File

@ -0,0 +1,35 @@
use nu_protocol::ast::Call;
use nu_protocol::engine::{Command, EngineState, Stack};
use nu_protocol::{IntoInterruptiblePipelineData, PipelineData, ShellError, Signature};
use rand::prelude::SliceRandom;
use rand::thread_rng;
#[derive(Clone)]
pub struct Shuffle;
impl Command for Shuffle {
fn name(&self) -> &str {
"shuffle"
}
fn signature(&self) -> nu_protocol::Signature {
Signature::build("shuffle")
}
fn usage(&self) -> &str {
"Shuffle rows randomly."
}
fn run(
&self,
engine_state: &EngineState,
_stack: &mut Stack,
_call: &Call,
input: PipelineData,
) -> Result<PipelineData, ShellError> {
let mut v: Vec<_> = input.into_iter().collect();
v.shuffle(&mut thread_rng());
let iter = v.into_iter();
Ok(iter.into_pipeline_data(engine_state.ctrlc.clone()))
}
}