Add shuffle plugin (#1443)

* Add shuffle plugin

see #1437

* Change plugin to integrate into nu structure and build system
This commit is contained in:
Falco Hirschenberger 2020-03-02 20:44:12 +01:00 committed by GitHub
parent 7304d06c0b
commit ed7d3fed66
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 85 additions and 1 deletions

View File

@ -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"

View 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" }

View File

@ -0,0 +1,3 @@
fn main() -> Result<(), Box<dyn std::error::Error>> {
nu_build::build()
}

View File

@ -0,0 +1,3 @@
mod nu;
pub use nu::Shuffle;

View File

@ -0,0 +1,6 @@
use nu_plugin::serve_plugin;
use nu_plugin_shuffle::Shuffle;
fn main() {
serve_plugin(&mut Shuffle::new());
}

View 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())
}
}

View File

@ -0,0 +1,6 @@
use nu_plugin::serve_plugin;
use nu_plugin_shuffle::Shuffle;
fn main() {
serve_plugin(&mut Shuffle::new());
}