diff --git a/Cargo.lock b/Cargo.lock index a859178a53..cee4ea9107 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2341,6 +2341,7 @@ dependencies = [ "prettytable-rs", "ptree", "query_interface", + "rand", "regex", "roxmltree", "rusqlite", @@ -2619,18 +2620,6 @@ dependencies = [ "pin-utils", ] -[[package]] -name = "nu_plugin_shuffle" -version = "0.11.0" -dependencies = [ - "nu-build", - "nu-errors", - "nu-plugin", - "nu-protocol", - "nu-source", - "rand", -] - [[package]] name = "nu_plugin_str" version = "0.11.0" diff --git a/Cargo.toml b/Cargo.toml index bde37ca2cd..4abc5f20e7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,7 +32,6 @@ nu_plugin_inc = { version = "0.11.0", path = "./crates/nu_plugin_inc", optional= nu_plugin_match = { version = "0.11.0", path = "./crates/nu_plugin_match", optional=true } nu_plugin_post = { version = "0.11.0", path = "./crates/nu_plugin_post", optional=true } nu_plugin_ps = { version = "0.11.0", path = "./crates/nu_plugin_ps", optional=true } -# nu_plugin_shuffle = { version = "0.11.0", path = "./crates/nu_plugin_shuffle", optional=true } nu_plugin_str = { version = "0.11.0", path = "./crates/nu_plugin_str", optional=true } nu_plugin_sum = { version = "0.11.0", path = "./crates/nu_plugin_sum", optional=true } nu_plugin_sys = { version = "0.11.0", path = "./crates/nu_plugin_sys", optional=true } @@ -82,7 +81,6 @@ binaryview = ["nu_plugin_binaryview"] fetch = ["nu_plugin_fetch"] match = ["nu_plugin_match"] post = ["nu_plugin_post"] -# shuffle = ["nu_plugin_shuffle"] sum = ["nu_plugin_sum"] trace = ["nu-parser/trace"] tree = ["nu_plugin_tree"] @@ -169,11 +167,6 @@ 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-cli/Cargo.toml b/crates/nu-cli/Cargo.toml index efd8a37481..c3b5cb4090 100644 --- a/crates/nu-cli/Cargo.toml +++ b/crates/nu-cli/Cargo.toml @@ -63,6 +63,7 @@ pretty_env_logger = "0.4.0" prettytable-rs = "0.8.0" ptree = {version = "0.2" } query_interface = "0.3.5" +rand = "0.7" regex = "1" roxmltree = "0.9.1" rustyline = "6.0.0" diff --git a/crates/nu-cli/src/cli.rs b/crates/nu-cli/src/cli.rs index 76aaa54f6f..75f9484c8c 100644 --- a/crates/nu-cli/src/cli.rs +++ b/crates/nu-cli/src/cli.rs @@ -305,6 +305,7 @@ pub fn create_default_context( whole_stream_command(Rename), whole_stream_command(Uniq), // Table manipulation + whole_stream_command(Shuffle), whole_stream_command(Wrap), whole_stream_command(Pivot), // Data processing diff --git a/crates/nu-cli/src/commands.rs b/crates/nu-cli/src/commands.rs index 06cba20bf0..58822ddf32 100644 --- a/crates/nu-cli/src/commands.rs +++ b/crates/nu-cli/src/commands.rs @@ -73,6 +73,7 @@ pub(crate) mod reverse; pub(crate) mod rm; pub(crate) mod save; pub(crate) mod shells; +pub(crate) mod shuffle; pub(crate) mod size; pub(crate) mod skip; pub(crate) mod skip_while; @@ -176,6 +177,7 @@ pub(crate) use reverse::Reverse; pub(crate) use rm::Remove; pub(crate) use save::Save; pub(crate) use shells::Shells; +pub(crate) use shuffle::Shuffle; pub(crate) use size::Size; pub(crate) use skip::Skip; pub(crate) use skip_while::SkipWhile; diff --git a/crates/nu-cli/src/commands/shuffle.rs b/crates/nu-cli/src/commands/shuffle.rs new file mode 100644 index 0000000000..ee023f98c1 --- /dev/null +++ b/crates/nu-cli/src/commands/shuffle.rs @@ -0,0 +1,69 @@ +use crate::commands::WholeStreamCommand; +use crate::context::CommandRegistry; +use crate::prelude::*; +use nu_errors::ShellError; +use nu_protocol::{ReturnSuccess, ReturnValue, Signature, SyntaxShape, Value}; +use nu_source::Tagged; + +use rand::seq::SliceRandom; +use rand::thread_rng; + +pub struct Shuffle; + +#[derive(Deserialize)] +pub struct Arguments { + #[serde(rename = "num")] + limit: Option>, +} + +impl WholeStreamCommand for Shuffle { + fn name(&self) -> &str { + "shuffle" + } + + fn signature(&self) -> Signature { + Signature::build("shuffle").named( + "num", + SyntaxShape::Int, + "Limit `num` number of rows", + Some('n'), + ) + } + + fn usage(&self) -> &str { + "Shuffle rows randomly." + } + + fn run( + &self, + args: CommandArgs, + registry: &CommandRegistry, + ) -> Result { + args.process(registry, shuffle)?.run() + } +} + +fn shuffle( + Arguments { limit }: Arguments, + RunnableContext { input, .. }: RunnableContext, +) -> Result { + let stream = async_stream! { + let mut values: Vec = input.values.collect().await; + + let out = if let Some(n) = limit { + let (shuffled, _) = values.partial_shuffle(&mut thread_rng(), *n as usize); + shuffled.to_vec() + } else { + values.shuffle(&mut thread_rng()); + values.clone() + }; + + for val in out.into_iter() { + yield ReturnSuccess::value(val); + } + }; + + let stream: BoxStream<'static, ReturnValue> = stream.boxed(); + + Ok(stream.to_output_stream()) +} diff --git a/crates/nu_plugin_shuffle/Cargo.toml b/crates/nu_plugin_shuffle/Cargo.toml deleted file mode 100644 index c5690ae66a..0000000000 --- a/crates/nu_plugin_shuffle/Cargo.toml +++ /dev/null @@ -1,22 +0,0 @@ -[package] -name = "nu_plugin_shuffle" -description = "Nushell plugin to shuffle input" -version = "0.11.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.11.0" } -nu-protocol = { path = "../nu-protocol", version = "0.11.0" } -nu-source = { path = "../nu-source", version = "0.11.0" } -nu-errors = { path = "../nu-errors", version = "0.11.0" } -rand = "0.7" - -[build-dependencies] -nu-build = { version = "0.11.0", path = "../nu-build" } diff --git a/crates/nu_plugin_shuffle/build.rs b/crates/nu_plugin_shuffle/build.rs deleted file mode 100644 index b7511cfc6a..0000000000 --- a/crates/nu_plugin_shuffle/build.rs +++ /dev/null @@ -1,3 +0,0 @@ -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 deleted file mode 100644 index 4bcac9cc1c..0000000000 --- a/crates/nu_plugin_shuffle/src/lib.rs +++ /dev/null @@ -1,3 +0,0 @@ -mod nu; - -pub use nu::Shuffle; diff --git a/crates/nu_plugin_shuffle/src/main.rs b/crates/nu_plugin_shuffle/src/main.rs deleted file mode 100644 index 72bfaad757..0000000000 --- a/crates/nu_plugin_shuffle/src/main.rs +++ /dev/null @@ -1,6 +0,0 @@ -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 deleted file mode 100644 index 287fb0ac7c..0000000000 --- a/crates/nu_plugin_shuffle/src/nu/mod.rs +++ /dev/null @@ -1,64 +0,0 @@ -use nu_errors::ShellError; -use nu_plugin::Plugin; -use nu_protocol::{ - CallInfo, ReturnSuccess, ReturnValue, Signature, SyntaxShape, UntaggedValue, Value, -}; - -use rand::seq::SliceRandom; -use rand::thread_rng; - -#[derive(Default)] -pub struct Shuffle { - values: Vec, - limit: Option, -} - -impl Shuffle { - pub fn new() -> Self { - Self::default() - } - - pub fn setup(&mut self, call_info: CallInfo) -> ReturnValue { - self.limit = if let Some(value) = call_info.args.get("num") { - Some(value.as_u64()?) - } else { - None - }; - ReturnSuccess::value(UntaggedValue::nothing().into_untagged_value()) - } -} - -impl Plugin for Shuffle { - fn config(&mut self) -> Result { - Ok(Signature::build("shuffle") - .desc("Shuffle input randomly") - .named( - "num", - SyntaxShape::Int, - "Limit output to `num` number of values", - Some('n'), - ) - .filter()) - } - - fn begin_filter(&mut self, call_info: CallInfo) -> Result, ShellError> { - self.setup(call_info)?; - Ok(vec![]) - } - - 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(); - if let Some(n) = self.limit { - let (shuffled, _) = self.values.partial_shuffle(&mut rng, n as usize); - Ok(shuffled.to_vec()) - } else { - 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 deleted file mode 100644 index 72bfaad757..0000000000 --- a/src/plugins/nu_plugin_stable_shuffle.rs +++ /dev/null @@ -1,6 +0,0 @@ -use nu_plugin::serve_plugin; -use nu_plugin_shuffle::Shuffle; - -fn main() { - serve_plugin(&mut Shuffle::new()); -}