nushell/src/commands/nth.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
2019-08-12 07:13:58 +02:00
use crate::errors::ShellError;
use crate::parser::CommandRegistry;
use crate::prelude::*;
pub struct Nth;
impl WholeStreamCommand for Nth {
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
nth(args, registry)
}
fn name(&self) -> &str {
"nth"
}
2019-08-12 07:13:58 +02:00
fn signature(&self) -> Signature {
Signature::build("nth").required("amount", SyntaxType::Literal)
2019-08-12 07:13:58 +02:00
}
}
fn nth(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(registry)?;
2019-08-12 07:13:58 +02:00
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),
))
}