Add nth command

This commit is contained in:
Jonathan Turner
2019-08-12 17:13:58 +12:00
parent 6cf3dc92fc
commit 14e47f3d2c
4 changed files with 34 additions and 2 deletions

View File

@@ -2,8 +2,6 @@ use crate::errors::ShellError;
use crate::parser::CommandRegistry;
use crate::prelude::*;
// TODO: "Amount remaining" wrapper
pub fn first(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(registry)?;

32
src/commands/nth.rs Normal file
View File

@@ -0,0 +1,32 @@
use crate::errors::ShellError;
use crate::parser::CommandRegistry;
use crate::prelude::*;
pub fn nth(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(registry)?;
if args.len() == 0 {
return Err(ShellError::labeled_error(
"Nth requires an amount",
"needs amount",
args.name_span(),
));
}
let amount = args.expect_nth(0)?.as_i64();
let amount = match amount {
Ok(o) => o,
Err(_) => {
return Err(ShellError::labeled_error(
"Value is not a number",
"expected integer",
args.expect_nth(0)?.span(),
))
}
};
Ok(OutputStream::from_input(
args.input.values.skip(amount as u64).take(1),
))
}