mirror of
https://github.com/nushell/nushell.git
synced 2025-01-11 16:58:41 +01:00
Merge pull request #1011 from andrasio/nth-checks
nth can select more than one row at a time.
This commit is contained in:
commit
be140382cf
@ -281,7 +281,7 @@ Nu adheres closely to a set of goals that make up its design philosophy. As feat
|
|||||||
| inc (column-or-column-path) | Increment a value or version. Optionally use the column of a table |
|
| inc (column-or-column-path) | Increment a value or version. Optionally use the column of a table |
|
||||||
| insert column-or-column-path value | Insert a new column to the table |
|
| insert column-or-column-path value | Insert a new column to the table |
|
||||||
| last amount | Show only the last number of rows |
|
| last amount | Show only the last number of rows |
|
||||||
| nth row-number | Return only the selected row |
|
| nth ...row-numbers | Return only the selected rows |
|
||||||
| pick ...columns | Down-select table to only these columns |
|
| pick ...columns | Down-select table to only these columns |
|
||||||
| pivot --header-row <headers> | Pivot the tables, making columns into rows and vice versa |
|
| pivot --header-row <headers> | Pivot the tables, making columns into rows and vice versa |
|
||||||
| prepend row-data | Prepend a row to the beginning of the table |
|
| prepend row-data | Prepend a row to the beginning of the table |
|
||||||
|
@ -5,7 +5,8 @@ use crate::prelude::*;
|
|||||||
|
|
||||||
#[derive(Deserialize)]
|
#[derive(Deserialize)]
|
||||||
struct NthArgs {
|
struct NthArgs {
|
||||||
amount: Tagged<i64>,
|
row_number: Tagged<u64>,
|
||||||
|
rest: Vec<Tagged<u64>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct Nth;
|
pub struct Nth;
|
||||||
@ -16,15 +17,17 @@ impl WholeStreamCommand for Nth {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn signature(&self) -> Signature {
|
fn signature(&self) -> Signature {
|
||||||
Signature::build("nth").required(
|
Signature::build("nth")
|
||||||
|
.required(
|
||||||
"row number",
|
"row number",
|
||||||
SyntaxShape::Any,
|
SyntaxShape::Any,
|
||||||
"the number of the row to return",
|
"the number of the row to return",
|
||||||
)
|
)
|
||||||
|
.rest(SyntaxShape::Any, "Optionally return more rows")
|
||||||
}
|
}
|
||||||
|
|
||||||
fn usage(&self) -> &str {
|
fn usage(&self) -> &str {
|
||||||
"Return only the selected row"
|
"Return only the selected rows"
|
||||||
}
|
}
|
||||||
|
|
||||||
fn run(
|
fn run(
|
||||||
@ -37,10 +40,35 @@ impl WholeStreamCommand for Nth {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn nth(
|
fn nth(
|
||||||
NthArgs { amount }: NthArgs,
|
NthArgs {
|
||||||
|
row_number,
|
||||||
|
rest: and_rows,
|
||||||
|
}: NthArgs,
|
||||||
RunnableContext { input, .. }: RunnableContext,
|
RunnableContext { input, .. }: RunnableContext,
|
||||||
) -> Result<OutputStream, ShellError> {
|
) -> Result<OutputStream, ShellError> {
|
||||||
Ok(OutputStream::from_input(
|
let stream = input
|
||||||
input.values.skip(amount.item as u64).take(1),
|
.values
|
||||||
))
|
.enumerate()
|
||||||
|
.map(move |(idx, item)| {
|
||||||
|
let row_number = vec![row_number.clone()];
|
||||||
|
|
||||||
|
let row_numbers = vec![&row_number, &and_rows]
|
||||||
|
.into_iter()
|
||||||
|
.flatten()
|
||||||
|
.collect::<Vec<&Tagged<u64>>>();
|
||||||
|
|
||||||
|
let mut result = VecDeque::new();
|
||||||
|
|
||||||
|
if row_numbers
|
||||||
|
.iter()
|
||||||
|
.any(|requested| requested.item == idx as u64)
|
||||||
|
{
|
||||||
|
result.push_back(ReturnSuccess::value(item.clone()));
|
||||||
|
}
|
||||||
|
|
||||||
|
result
|
||||||
|
})
|
||||||
|
.flatten();
|
||||||
|
|
||||||
|
Ok(stream.to_output_stream())
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,45 @@ mod helpers;
|
|||||||
use helpers as h;
|
use helpers as h;
|
||||||
use helpers::{Playground, Stub::*};
|
use helpers::{Playground, Stub::*};
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nth_selects_a_row() {
|
||||||
|
Playground::setup("nth_test_1", |dirs, sandbox| {
|
||||||
|
sandbox.with_files(vec![EmptyFile("notes.txt"), EmptyFile("arepas.txt")]);
|
||||||
|
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: dirs.test(), h::pipeline(
|
||||||
|
r#"
|
||||||
|
ls
|
||||||
|
| sort-by name
|
||||||
|
| nth 0
|
||||||
|
| get name
|
||||||
|
| echo $it
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert_eq!(actual, "arepas.txt");
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn nth_selects_many_rows() {
|
||||||
|
Playground::setup("nth_test_2", |dirs, sandbox| {
|
||||||
|
sandbox.with_files(vec![EmptyFile("notes.txt"), EmptyFile("arepas.txt")]);
|
||||||
|
|
||||||
|
let actual = nu!(
|
||||||
|
cwd: dirs.test(), h::pipeline(
|
||||||
|
r#"
|
||||||
|
ls
|
||||||
|
| get name
|
||||||
|
| nth 1 0
|
||||||
|
| count
|
||||||
|
| echo $it
|
||||||
|
"#
|
||||||
|
));
|
||||||
|
|
||||||
|
assert_eq!(actual, "2");
|
||||||
|
});
|
||||||
|
}
|
||||||
#[test]
|
#[test]
|
||||||
fn default_row_data_if_column_missing() {
|
fn default_row_data_if_column_missing() {
|
||||||
Playground::setup("default_test_1", |dirs, sandbox| {
|
Playground::setup("default_test_1", |dirs, sandbox| {
|
||||||
|
Loading…
Reference in New Issue
Block a user