nushell/src/commands/nth.rs

43 lines
908 B
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::*;
#[derive(Deserialize)]
struct NthArgs {
2019-08-20 08:11:11 +02:00
amount: Tagged<i64>,
}
pub struct Nth;
impl WholeStreamCommand for Nth {
fn name(&self) -> &str {
"nth"
}
2019-08-12 07:13:58 +02:00
fn signature(&self) -> Signature {
Signature::build("nth").required("amount", SyntaxShape::Any)
2019-08-12 07:13:58 +02:00
}
fn usage(&self) -> &str {
"Return only the selected row"
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, nth)?.run()
}
}
fn nth(
2019-08-20 08:11:11 +02:00
NthArgs { amount }: NthArgs,
RunnableContext { input, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> {
2019-08-12 07:13:58 +02:00
Ok(OutputStream::from_input(
input.values.skip(amount.item as u64).take(1),
2019-08-12 07:13:58 +02:00
))
}