mirror of
https://github.com/nushell/nushell.git
synced 2025-01-05 13:59:46 +01:00
Port over the shuffle command from nushell (#300)
* initial commit of shuffle * port the shuffle command from nushell
This commit is contained in:
parent
a1f141d18a
commit
573cb38bab
1
Cargo.lock
generated
1
Cargo.lock
generated
@ -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",
|
||||||
|
@ -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"]
|
||||||
|
@ -88,6 +88,7 @@ pub fn create_default_context() -> EngineState {
|
|||||||
Range,
|
Range,
|
||||||
Rm,
|
Rm,
|
||||||
Select,
|
Select,
|
||||||
|
Shuffle,
|
||||||
Size,
|
Size,
|
||||||
ScreamingSnakeCase,
|
ScreamingSnakeCase,
|
||||||
SnakeCase,
|
SnakeCase,
|
||||||
|
@ -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;
|
||||||
|
35
crates/nu-command/src/filters/shuffle.rs
Normal file
35
crates/nu-command/src/filters/shuffle.rs
Normal 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()))
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user