'first' gets first row if no amount desired given.

This commit is contained in:
Andrés N. Robalino 2019-10-15 04:17:55 -05:00
parent ed39377840
commit 3f60c9d416
2 changed files with 27 additions and 10 deletions

View File

@ -7,7 +7,7 @@ pub struct First;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct FirstArgs { pub struct FirstArgs {
amount: Tagged<u64>, rows: Option<Tagged<u64>>,
} }
impl WholeStreamCommand for First { impl WholeStreamCommand for First {
@ -16,7 +16,7 @@ impl WholeStreamCommand for First {
} }
fn signature(&self) -> Signature { fn signature(&self) -> Signature {
Signature::build("first").required("amount", SyntaxShape::Int) Signature::build("first").optional("rows", SyntaxShape::Int)
} }
fn usage(&self) -> &str { fn usage(&self) -> &str {
@ -33,8 +33,15 @@ impl WholeStreamCommand for First {
} }
fn first( fn first(
FirstArgs { amount }: FirstArgs, FirstArgs { rows }: FirstArgs,
context: RunnableContext, context: RunnableContext,
) -> Result<OutputStream, ShellError> { ) -> Result<OutputStream, ShellError> {
Ok(OutputStream::from_input(context.input.values.take(*amount)))
let rows_desired = if let Some(quantity) = rows {
*quantity
} else {
1
};
Ok(OutputStream::from_input(context.input.values.take(rows_desired)))
} }

View File

@ -32,13 +32,23 @@ fn first_gets_first_rows_by_amount() {
} }
#[test] #[test]
fn first_requires_an_amount() { fn first_gets_first_row_when_no_amount_given() {
Playground::setup("first_test_2", |dirs, _| { Playground::setup("first_test_2", |dirs, sandbox| {
let actual = nu_error!( sandbox.with_files(vec![EmptyFile("los-tres-amigos.PASSTEST.txt")]);
cwd: dirs.test(), "ls | first"
);
assert!(actual.contains("requires amount parameter")); let actual = nu!(
cwd: dirs.test(), h::pipeline(
r#"
ls
| get name
| first
| split-column "."
| get Column2
| echo $it
"#
));
assert_eq!(actual, "PASSTEST");
}) })
} }