forked from extern/nushell
Add support for skip and where
This commit is contained in:
29
src/commands/skip.rs
Normal file
29
src/commands/skip.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use crate::errors::ShellError;
|
||||
use crate::prelude::*;
|
||||
use derive_new::new;
|
||||
|
||||
#[derive(new)]
|
||||
pub struct Skip;
|
||||
|
||||
// TODO: "Amount remaining" wrapper
|
||||
|
||||
impl crate::Command for Skip {
|
||||
fn run(&self, args: CommandArgs<'caller>) -> Result<VecDeque<ReturnValue>, ShellError> {
|
||||
let amount = args.args[0].as_int()?;
|
||||
|
||||
let amount = if args.input.len() > amount as usize {
|
||||
amount as usize
|
||||
} else {
|
||||
args.input.len()
|
||||
};
|
||||
|
||||
let out: VecDeque<ReturnValue> = args
|
||||
.input
|
||||
.into_iter()
|
||||
.skip(amount)
|
||||
.map(|v| ReturnValue::Value(v))
|
||||
.collect();
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
}
|
30
src/commands/where_.rs
Normal file
30
src/commands/where_.rs
Normal file
@@ -0,0 +1,30 @@
|
||||
use crate::errors::ShellError;
|
||||
use crate::object::base::find;
|
||||
use crate::prelude::*;
|
||||
use derive_new::new;
|
||||
|
||||
#[derive(new)]
|
||||
pub struct Where;
|
||||
|
||||
impl crate::Command for Where {
|
||||
fn run(&self, args: CommandArgs<'caller>) -> Result<VecDeque<ReturnValue>, ShellError> {
|
||||
if args.args.is_empty() {
|
||||
return Err(ShellError::string("select requires a field"));
|
||||
}
|
||||
|
||||
let field: Result<String, _> = args.args[0].as_string();
|
||||
let field = field?;
|
||||
|
||||
let op: Result<String, _> = args.args[1].as_string();
|
||||
let op = op?;
|
||||
|
||||
let objects = args
|
||||
.input
|
||||
.iter()
|
||||
.filter(|item| find(&item, &field, &op, &args.args[2]))
|
||||
.map(|item| ReturnValue::Value(item.copy()))
|
||||
.collect();
|
||||
|
||||
Ok(objects)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user