From ed7d3fed66f21824eb1ff1a90fc13747ef09616b Mon Sep 17 00:00:00 2001 From: Falco Hirschenberger Date: Mon, 2 Mar 2020 20:44:12 +0100 Subject: [PATCH] Add shuffle plugin (#1443) * Add shuffle plugin see #1437 * Change plugin to integrate into nu structure and build system --- Cargo.toml | 10 ++++++- crates/nu_plugin_shuffle/Cargo.toml | 22 +++++++++++++++ crates/nu_plugin_shuffle/build.rs | 3 +++ crates/nu_plugin_shuffle/src/lib.rs | 3 +++ crates/nu_plugin_shuffle/src/main.rs | 6 +++++ crates/nu_plugin_shuffle/src/nu/mod.rs | 36 +++++++++++++++++++++++++ src/plugins/nu_plugin_stable_shuffle.rs | 6 +++++ 7 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 crates/nu_plugin_shuffle/Cargo.toml create mode 100644 crates/nu_plugin_shuffle/build.rs create mode 100644 crates/nu_plugin_shuffle/src/lib.rs create mode 100644 crates/nu_plugin_shuffle/src/main.rs create mode 100644 crates/nu_plugin_shuffle/src/nu/mod.rs create mode 100644 src/plugins/nu_plugin_stable_shuffle.rs diff --git a/Cargo.toml b/Cargo.toml index bb4f1e905a..1567f26e2b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ members = [ "crates/nu_plugin_match", "crates/nu_plugin_post", "crates/nu_plugin_ps", + "crates/nu_plugin_shuffle", "crates/nu_plugin_str", "crates/nu_plugin_sum", "crates/nu_plugin_sys", @@ -53,6 +54,7 @@ nu_plugin_inc = { version = "0.10.0", path = "./crates/nu_plugin_inc", optional= nu_plugin_match = { version = "0.10.0", path = "./crates/nu_plugin_match", optional=true } nu_plugin_post = { version = "0.10.0", path = "./crates/nu_plugin_post", optional=true } nu_plugin_ps = { version = "0.10.0", path = "./crates/nu_plugin_ps", optional=true } +nu_plugin_shuffle = { version = "0.10.0", path = "./crates/nu_plugin_shuffle", optional=true } nu_plugin_str = { version = "0.10.0", path = "./crates/nu_plugin_str", optional=true } nu_plugin_sum = { version = "0.10.0", path = "./crates/nu_plugin_sum", optional=true } nu_plugin_sys = { version = "0.10.0", path = "./crates/nu_plugin_sys", optional=true } @@ -143,7 +145,7 @@ users = "0.9" test-bins = [] default = ["sys", "ps", "textview", "inc", "str"] -stable = ["default", "starship-prompt", "binaryview", "match", "tree", "average", "sum", "post", "fetch", "clipboard"] +stable = ["default", "starship-prompt", "binaryview", "match", "tree", "average", "shuffle", "sum", "post", "fetch", "clipboard"] # Default textview = ["crossterm", "syntect", "onig_sys", "url", "nu_plugin_textview"] @@ -159,6 +161,7 @@ fetch = ["nu_plugin_fetch"] match = ["nu_plugin_match"] post = ["nu_plugin_post"] starship-prompt = ["starship"] +shuffle = ["nu_plugin_shuffle"] sum = ["nu_plugin_sum"] trace = ["nu-parser/trace"] tree = ["nu_plugin_tree"] @@ -260,6 +263,11 @@ name = "nu_plugin_stable_post" path = "src/plugins/nu_plugin_stable_post.rs" required-features = ["post"] +[[bin]] +name = "nu_plugin_stable_shuffle" +path = "src/plugins/nu_plugin_stable_shuffle.rs" +required-features = ["shuffle"] + [[bin]] name = "nu_plugin_stable_sum" path = "src/plugins/nu_plugin_stable_sum.rs" diff --git a/crates/nu_plugin_shuffle/Cargo.toml b/crates/nu_plugin_shuffle/Cargo.toml new file mode 100644 index 0000000000..152cb2dd46 --- /dev/null +++ b/crates/nu_plugin_shuffle/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "nu_plugin_shuffle" +description = "Nushell plugin to shuffle input" +version = "0.10.0" +license = "MIT" +authors = ["Falco Hirschenberger "] +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" } diff --git a/crates/nu_plugin_shuffle/build.rs b/crates/nu_plugin_shuffle/build.rs new file mode 100644 index 0000000000..b7511cfc6a --- /dev/null +++ b/crates/nu_plugin_shuffle/build.rs @@ -0,0 +1,3 @@ +fn main() -> Result<(), Box> { + nu_build::build() +} diff --git a/crates/nu_plugin_shuffle/src/lib.rs b/crates/nu_plugin_shuffle/src/lib.rs new file mode 100644 index 0000000000..4bcac9cc1c --- /dev/null +++ b/crates/nu_plugin_shuffle/src/lib.rs @@ -0,0 +1,3 @@ +mod nu; + +pub use nu::Shuffle; diff --git a/crates/nu_plugin_shuffle/src/main.rs b/crates/nu_plugin_shuffle/src/main.rs new file mode 100644 index 0000000000..72bfaad757 --- /dev/null +++ b/crates/nu_plugin_shuffle/src/main.rs @@ -0,0 +1,6 @@ +use nu_plugin::serve_plugin; +use nu_plugin_shuffle::Shuffle; + +fn main() { + serve_plugin(&mut Shuffle::new()); +} diff --git a/crates/nu_plugin_shuffle/src/nu/mod.rs b/crates/nu_plugin_shuffle/src/nu/mod.rs new file mode 100644 index 0000000000..e9eb975cc0 --- /dev/null +++ b/crates/nu_plugin_shuffle/src/nu/mod.rs @@ -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, +} + +impl Shuffle { + pub fn new() -> Self { + Self::default() + } +} + +impl Plugin for Shuffle { + fn config(&mut self) -> Result { + Ok(Signature::build("shuffle") + .desc("Shuffle input randomly") + .filter()) + } + + fn filter(&mut self, input: Value) -> Result, ShellError> { + self.values.push(input.into()); + Ok(vec![]) + } + + fn end_filter(&mut self) -> Result, ShellError> { + let mut rng = thread_rng(); + self.values.shuffle(&mut rng); + Ok(self.values.clone()) + } +} diff --git a/src/plugins/nu_plugin_stable_shuffle.rs b/src/plugins/nu_plugin_stable_shuffle.rs new file mode 100644 index 0000000000..72bfaad757 --- /dev/null +++ b/src/plugins/nu_plugin_stable_shuffle.rs @@ -0,0 +1,6 @@ +use nu_plugin::serve_plugin; +use nu_plugin_shuffle::Shuffle; + +fn main() { + serve_plugin(&mut Shuffle::new()); +}