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
7 changed files with 85 additions and 1 deletions

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