nushell/src/commands/first.rs

54 lines
1.1 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
2019-05-15 18:12:38 +02:00
use crate::errors::ShellError;
2019-07-24 00:22:11 +02:00
use crate::parser::CommandRegistry;
2019-05-15 18:12:38 +02:00
use crate::prelude::*;
use nu_source::Tagged;
2019-05-15 18:12:38 +02:00
pub struct First;
#[derive(Deserialize)]
pub struct FirstArgs {
rows: Option<Tagged<u64>>,
}
impl WholeStreamCommand for First {
fn name(&self) -> &str {
"first"
}
fn signature(&self) -> Signature {
2019-10-28 06:15:35 +01:00
Signature::build("first").optional(
"rows",
SyntaxShape::Int,
"starting from the front, the number of rows to return",
)
}
fn usage(&self) -> &str {
"Show only the first number of rows."
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, first)?.run()
}
}
fn first(
FirstArgs { rows }: FirstArgs,
context: RunnableContext,
) -> Result<OutputStream, ShellError> {
let rows_desired = if let Some(quantity) = rows {
*quantity
} else {
1
};
2019-10-15 12:42:24 +02:00
Ok(OutputStream::from_input(
context.input.values.take(rows_desired),
))
2019-05-15 18:12:38 +02:00
}