mirror of
https://github.com/nushell/nushell.git
synced 2025-02-22 21:41:26 +01:00
Add --num
parameter to limit the number of output lines (#1455)
Add `--num` parameter to limit the numer of returned elements
This commit is contained in:
parent
f88674f353
commit
db24ad8f36
@ -1,6 +1,8 @@
|
|||||||
use nu_errors::ShellError;
|
use nu_errors::ShellError;
|
||||||
use nu_plugin::Plugin;
|
use nu_plugin::Plugin;
|
||||||
use nu_protocol::{ReturnValue, Signature, Value};
|
use nu_protocol::{
|
||||||
|
CallInfo, ReturnSuccess, ReturnValue, Signature, SyntaxShape, UntaggedValue, Value,
|
||||||
|
};
|
||||||
|
|
||||||
use rand::seq::SliceRandom;
|
use rand::seq::SliceRandom;
|
||||||
use rand::thread_rng;
|
use rand::thread_rng;
|
||||||
@ -8,21 +10,42 @@ use rand::thread_rng;
|
|||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct Shuffle {
|
pub struct Shuffle {
|
||||||
values: Vec<ReturnValue>,
|
values: Vec<ReturnValue>,
|
||||||
|
limit: Option<u64>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Shuffle {
|
impl Shuffle {
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self::default()
|
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 {
|
impl Plugin for Shuffle {
|
||||||
fn config(&mut self) -> Result<Signature, ShellError> {
|
fn config(&mut self) -> Result<Signature, ShellError> {
|
||||||
Ok(Signature::build("shuffle")
|
Ok(Signature::build("shuffle")
|
||||||
.desc("Shuffle input randomly")
|
.desc("Shuffle input randomly")
|
||||||
|
.named(
|
||||||
|
"num",
|
||||||
|
SyntaxShape::Int,
|
||||||
|
"Limit output to `num` number of values",
|
||||||
|
Some('n'),
|
||||||
|
)
|
||||||
.filter())
|
.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> {
|
fn filter(&mut self, input: Value) -> Result<Vec<ReturnValue>, ShellError> {
|
||||||
self.values.push(input.into());
|
self.values.push(input.into());
|
||||||
Ok(vec![])
|
Ok(vec![])
|
||||||
@ -30,7 +53,12 @@ impl Plugin for Shuffle {
|
|||||||
|
|
||||||
fn end_filter(&mut self) -> Result<Vec<ReturnValue>, ShellError> {
|
fn end_filter(&mut self) -> Result<Vec<ReturnValue>, ShellError> {
|
||||||
let mut rng = thread_rng();
|
let mut rng = thread_rng();
|
||||||
self.values.shuffle(&mut rng);
|
if let Some(n) = self.limit {
|
||||||
Ok(self.values.clone())
|
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())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user