mirror of
https://github.com/nushell/nushell.git
synced 2025-06-30 22:50:14 +02:00
Add shuffle plugin (#1443)
* Add shuffle plugin see #1437 * Change plugin to integrate into nu structure and build system
This commit is contained in:
committed by
GitHub
parent
7304d06c0b
commit
ed7d3fed66
22
crates/nu_plugin_shuffle/Cargo.toml
Normal file
22
crates/nu_plugin_shuffle/Cargo.toml
Normal file
@ -0,0 +1,22 @@
|
||||
[package]
|
||||
name = "nu_plugin_shuffle"
|
||||
description = "Nushell plugin to shuffle input"
|
||||
version = "0.10.0"
|
||||
license = "MIT"
|
||||
authors = ["Falco Hirschenberger <falco.hirschenberger@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
[lib]
|
||||
doctest = false
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
nu-plugin = { path = "../nu-plugin", version = "0.10.0" }
|
||||
nu-protocol = { path = "../nu-protocol", version = "0.10.0" }
|
||||
nu-source = { path = "../nu-source", version = "0.10.0" }
|
||||
nu-errors = { path = "../nu-errors", version = "0.10.0" }
|
||||
rand = "0.7"
|
||||
|
||||
[build-dependencies]
|
||||
nu-build = { version = "0.10.0", path = "../nu-build" }
|
3
crates/nu_plugin_shuffle/build.rs
Normal file
3
crates/nu_plugin_shuffle/build.rs
Normal file
@ -0,0 +1,3 @@
|
||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
||||
nu_build::build()
|
||||
}
|
3
crates/nu_plugin_shuffle/src/lib.rs
Normal file
3
crates/nu_plugin_shuffle/src/lib.rs
Normal file
@ -0,0 +1,3 @@
|
||||
mod nu;
|
||||
|
||||
pub use nu::Shuffle;
|
6
crates/nu_plugin_shuffle/src/main.rs
Normal file
6
crates/nu_plugin_shuffle/src/main.rs
Normal file
@ -0,0 +1,6 @@
|
||||
use nu_plugin::serve_plugin;
|
||||
use nu_plugin_shuffle::Shuffle;
|
||||
|
||||
fn main() {
|
||||
serve_plugin(&mut Shuffle::new());
|
||||
}
|
36
crates/nu_plugin_shuffle/src/nu/mod.rs
Normal file
36
crates/nu_plugin_shuffle/src/nu/mod.rs
Normal file
@ -0,0 +1,36 @@
|
||||
use nu_errors::ShellError;
|
||||
use nu_plugin::Plugin;
|
||||
use nu_protocol::{ReturnValue, Signature, Value};
|
||||
|
||||
use rand::seq::SliceRandom;
|
||||
use rand::thread_rng;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Shuffle {
|
||||
values: Vec<ReturnValue>,
|
||||
}
|
||||
|
||||
impl Shuffle {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl Plugin for Shuffle {
|
||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||
Ok(Signature::build("shuffle")
|
||||
.desc("Shuffle input randomly")
|
||||
.filter())
|
||||
}
|
||||
|
||||
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
self.values.push(input.into());
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
fn end_filter(&mut self) -> Result<Vec<ReturnValue>, ShellError> {
|
||||
let mut rng = thread_rng();
|
||||
self.values.shuffle(&mut rng);
|
||||
Ok(self.values.clone())
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user