From 14e47f3d2c34d4383290e7453a19e9f571cb2650 Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Mon, 12 Aug 2019 17:13:58 +1200 Subject: [PATCH] Add nth command --- src/cli.rs | 1 + src/commands.rs | 1 + src/commands/first.rs | 2 -- src/commands/nth.rs | 32 ++++++++++++++++++++++++++++++++ 4 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 src/commands/nth.rs diff --git a/src/cli.rs b/src/cli.rs index af8105872..d5b87efa0 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -158,6 +158,7 @@ pub async fn cli() -> Result<(), Box> { command("size", Box::new(size::size)), command("from-yaml", Box::new(from_yaml::from_yaml)), command("enter", Box::new(enter::enter)), + command("nth", Box::new(nth::nth)), command("n", Box::new(next::next)), command("p", Box::new(prev::prev)), command("lines", Box::new(lines::lines)), diff --git a/src/commands.rs b/src/commands.rs index 9366257a8..b3f7b3b00 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -24,6 +24,7 @@ crate mod lines; crate mod ls; crate mod mkdir; crate mod next; +crate mod nth; crate mod open; crate mod pick; crate mod plugin; diff --git a/src/commands/first.rs b/src/commands/first.rs index f34b9dba0..a48e55593 100644 --- a/src/commands/first.rs +++ b/src/commands/first.rs @@ -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 { let args = args.evaluate_once(registry)?; diff --git a/src/commands/nth.rs b/src/commands/nth.rs new file mode 100644 index 000000000..96e29f009 --- /dev/null +++ b/src/commands/nth.rs @@ -0,0 +1,32 @@ +use crate::errors::ShellError; +use crate::parser::CommandRegistry; +use crate::prelude::*; + +pub fn nth(args: CommandArgs, registry: &CommandRegistry) -> Result { + 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), + )) +}