mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
Merge shuffle nu plugin as core command. (#1475)
This commit is contained in:
parent
f716f61fc1
commit
d3718d00db
13
Cargo.lock
generated
13
Cargo.lock
generated
@ -2341,6 +2341,7 @@ dependencies = [
|
|||||||
"prettytable-rs",
|
"prettytable-rs",
|
||||||
"ptree",
|
"ptree",
|
||||||
"query_interface",
|
"query_interface",
|
||||||
|
"rand",
|
||||||
"regex",
|
"regex",
|
||||||
"roxmltree",
|
"roxmltree",
|
||||||
"rusqlite",
|
"rusqlite",
|
||||||
@ -2619,18 +2620,6 @@ dependencies = [
|
|||||||
"pin-utils",
|
"pin-utils",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "nu_plugin_shuffle"
|
|
||||||
version = "0.11.0"
|
|
||||||
dependencies = [
|
|
||||||
"nu-build",
|
|
||||||
"nu-errors",
|
|
||||||
"nu-plugin",
|
|
||||||
"nu-protocol",
|
|
||||||
"nu-source",
|
|
||||||
"rand",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "nu_plugin_str"
|
name = "nu_plugin_str"
|
||||||
version = "0.11.0"
|
version = "0.11.0"
|
||||||
|
@ -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_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_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_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_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_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 }
|
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"]
|
fetch = ["nu_plugin_fetch"]
|
||||||
match = ["nu_plugin_match"]
|
match = ["nu_plugin_match"]
|
||||||
post = ["nu_plugin_post"]
|
post = ["nu_plugin_post"]
|
||||||
# shuffle = ["nu_plugin_shuffle"]
|
|
||||||
sum = ["nu_plugin_sum"]
|
sum = ["nu_plugin_sum"]
|
||||||
trace = ["nu-parser/trace"]
|
trace = ["nu-parser/trace"]
|
||||||
tree = ["nu_plugin_tree"]
|
tree = ["nu_plugin_tree"]
|
||||||
@ -169,11 +167,6 @@ name = "nu_plugin_stable_post"
|
|||||||
path = "src/plugins/nu_plugin_stable_post.rs"
|
path = "src/plugins/nu_plugin_stable_post.rs"
|
||||||
required-features = ["post"]
|
required-features = ["post"]
|
||||||
|
|
||||||
[[bin]]
|
|
||||||
name = "nu_plugin_stable_shuffle"
|
|
||||||
path = "src/plugins/nu_plugin_stable_shuffle.rs"
|
|
||||||
required-features = ["shuffle"]
|
|
||||||
|
|
||||||
[[bin]]
|
[[bin]]
|
||||||
name = "nu_plugin_stable_sum"
|
name = "nu_plugin_stable_sum"
|
||||||
path = "src/plugins/nu_plugin_stable_sum.rs"
|
path = "src/plugins/nu_plugin_stable_sum.rs"
|
||||||
|
@ -63,6 +63,7 @@ pretty_env_logger = "0.4.0"
|
|||||||
prettytable-rs = "0.8.0"
|
prettytable-rs = "0.8.0"
|
||||||
ptree = {version = "0.2" }
|
ptree = {version = "0.2" }
|
||||||
query_interface = "0.3.5"
|
query_interface = "0.3.5"
|
||||||
|
rand = "0.7"
|
||||||
regex = "1"
|
regex = "1"
|
||||||
roxmltree = "0.9.1"
|
roxmltree = "0.9.1"
|
||||||
rustyline = "6.0.0"
|
rustyline = "6.0.0"
|
||||||
|
@ -305,6 +305,7 @@ pub fn create_default_context(
|
|||||||
whole_stream_command(Rename),
|
whole_stream_command(Rename),
|
||||||
whole_stream_command(Uniq),
|
whole_stream_command(Uniq),
|
||||||
// Table manipulation
|
// Table manipulation
|
||||||
|
whole_stream_command(Shuffle),
|
||||||
whole_stream_command(Wrap),
|
whole_stream_command(Wrap),
|
||||||
whole_stream_command(Pivot),
|
whole_stream_command(Pivot),
|
||||||
// Data processing
|
// Data processing
|
||||||
|
@ -73,6 +73,7 @@ pub(crate) mod reverse;
|
|||||||
pub(crate) mod rm;
|
pub(crate) mod rm;
|
||||||
pub(crate) mod save;
|
pub(crate) mod save;
|
||||||
pub(crate) mod shells;
|
pub(crate) mod shells;
|
||||||
|
pub(crate) mod shuffle;
|
||||||
pub(crate) mod size;
|
pub(crate) mod size;
|
||||||
pub(crate) mod skip;
|
pub(crate) mod skip;
|
||||||
pub(crate) mod skip_while;
|
pub(crate) mod skip_while;
|
||||||
@ -176,6 +177,7 @@ pub(crate) use reverse::Reverse;
|
|||||||
pub(crate) use rm::Remove;
|
pub(crate) use rm::Remove;
|
||||||
pub(crate) use save::Save;
|
pub(crate) use save::Save;
|
||||||
pub(crate) use shells::Shells;
|
pub(crate) use shells::Shells;
|
||||||
|
pub(crate) use shuffle::Shuffle;
|
||||||
pub(crate) use size::Size;
|
pub(crate) use size::Size;
|
||||||
pub(crate) use skip::Skip;
|
pub(crate) use skip::Skip;
|
||||||
pub(crate) use skip_while::SkipWhile;
|
pub(crate) use skip_while::SkipWhile;
|
||||||
|
69
crates/nu-cli/src/commands/shuffle.rs
Normal file
69
crates/nu-cli/src/commands/shuffle.rs
Normal file
@ -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<Tagged<u64>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
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<OutputStream, ShellError> {
|
||||||
|
args.process(registry, shuffle)?.run()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn shuffle(
|
||||||
|
Arguments { limit }: Arguments,
|
||||||
|
RunnableContext { input, .. }: RunnableContext,
|
||||||
|
) -> Result<OutputStream, ShellError> {
|
||||||
|
let stream = async_stream! {
|
||||||
|
let mut values: Vec<Value> = 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())
|
||||||
|
}
|
@ -1,22 +0,0 @@
|
|||||||
[package]
|
|
||||||
name = "nu_plugin_shuffle"
|
|
||||||
description = "Nushell plugin to shuffle input"
|
|
||||||
version = "0.11.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.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" }
|
|
@ -1,3 +0,0 @@
|
|||||||
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
nu_build::build()
|
|
||||||
}
|
|
@ -1,3 +0,0 @@
|
|||||||
mod nu;
|
|
||||||
|
|
||||||
pub use nu::Shuffle;
|
|
@ -1,6 +0,0 @@
|
|||||||
use nu_plugin::serve_plugin;
|
|
||||||
use nu_plugin_shuffle::Shuffle;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
serve_plugin(&mut Shuffle::new());
|
|
||||||
}
|
|
@ -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<ReturnValue>,
|
|
||||||
limit: Option<u64>,
|
|
||||||
}
|
|
||||||
|
|
||||||
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<Signature, ShellError> {
|
|
||||||
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<Vec<ReturnValue>, ShellError> {
|
|
||||||
self.setup(call_info)?;
|
|
||||||
Ok(vec![])
|
|
||||||
}
|
|
||||||
|
|
||||||
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();
|
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,6 +0,0 @@
|
|||||||
use nu_plugin::serve_plugin;
|
|
||||||
use nu_plugin_shuffle::Shuffle;
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
serve_plugin(&mut Shuffle::new());
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user