2019-08-19 07:16:39 +02:00
|
|
|
use crate::commands::WholeStreamCommand;
|
2019-08-12 07:13:58 +02:00
|
|
|
use crate::errors::ShellError;
|
|
|
|
use crate::parser::CommandRegistry;
|
|
|
|
use crate::prelude::*;
|
|
|
|
|
2019-08-20 05:12:31 +02:00
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct NthArgs {
|
2019-08-20 08:11:11 +02:00
|
|
|
amount: Tagged<i64>,
|
2019-08-20 05:12:31 +02:00
|
|
|
}
|
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
pub struct Nth;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for Nth {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"nth"
|
|
|
|
}
|
2019-08-12 07:13:58 +02:00
|
|
|
|
2019-08-19 07:16:39 +02:00
|
|
|
fn signature(&self) -> Signature {
|
2019-10-28 06:15:35 +01:00
|
|
|
Signature::build("nth").required(
|
|
|
|
"row number",
|
|
|
|
SyntaxShape::Any,
|
|
|
|
"the number of the row to return",
|
|
|
|
)
|
2019-08-12 07:13:58 +02:00
|
|
|
}
|
2019-08-30 00:52:32 +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()
|
|
|
|
}
|
2019-08-19 07:16:39 +02:00
|
|
|
}
|
|
|
|
|
2019-08-20 05:12:31 +02:00
|
|
|
fn nth(
|
2019-08-20 08:11:11 +02:00
|
|
|
NthArgs { amount }: NthArgs,
|
2019-08-20 05:12:31 +02:00
|
|
|
RunnableContext { input, .. }: RunnableContext,
|
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-08-12 07:13:58 +02:00
|
|
|
Ok(OutputStream::from_input(
|
2019-08-20 05:12:31 +02:00
|
|
|
input.values.skip(amount.item as u64).take(1),
|
2019-08-12 07:13:58 +02:00
|
|
|
))
|
|
|
|
}
|