Merge pull request #1045 from sebastian-xyz/range

add range command
This commit is contained in:
Jonathan Turner 2019-12-04 08:37:03 -08:00 committed by GitHub
commit 1d196394f6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 159 additions and 0 deletions

View File

@ -309,6 +309,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
whole_stream_command(What),
whole_stream_command(Which),
whole_stream_command(Debug),
whole_stream_command(Range),
]);
cfg_if::cfg_if! {

View File

@ -60,6 +60,7 @@ pub(crate) mod post;
pub(crate) mod prepend;
pub(crate) mod prev;
pub(crate) mod pwd;
pub(crate) mod range;
#[allow(unused)]
pub(crate) mod reduce_by;
pub(crate) mod reject;
@ -149,6 +150,7 @@ pub(crate) use post::Post;
pub(crate) use prepend::Prepend;
pub(crate) use prev::Previous;
pub(crate) use pwd::PWD;
pub(crate) use range::Range;
#[allow(unused)]
pub(crate) use reduce_by::ReduceBy;
pub(crate) use reject::Reject;

88
src/commands/range.rs Normal file
View File

@ -0,0 +1,88 @@
use crate::commands::WholeStreamCommand;
use crate::context::CommandRegistry;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{Signature, SyntaxShape};
use nu_source::Tagged;
#[derive(Deserialize)]
struct RangeArgs {
area: Tagged<String>,
}
pub struct Range;
impl WholeStreamCommand for Range {
fn name(&self) -> &str {
"range"
}
fn signature(&self) -> Signature {
Signature::build("range").required(
"rows ",
SyntaxShape::Any,
"range of rows to return: Eg) 4..7 (=> from 4 to 7)",
)
}
fn usage(&self) -> &str {
"Return only the selected rows"
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, range)?.run()
}
}
fn range(
RangeArgs { area: rows }: RangeArgs,
RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> {
match rows.item.find(".") {
Some(value) => {
let (first, last) = rows.item.split_at(value);
let first = match first.parse::<u64>() {
Ok(postion) => postion,
Err(_) => {
if first == "" {
0
} else {
return Err(ShellError::labeled_error(
"no correct start of range",
"'from' needs to be an Integer or empty",
name,
));
}
}
};
let last = match last.trim_start_matches(".").parse::<u64>() {
Ok(postion) => postion,
Err(_) => {
if last == ".." {
std::u64::MAX - 1
} else {
return Err(ShellError::labeled_error(
"no correct end of range",
"'to' needs to be an Integer or empty",
name,
));
}
}
};
return Ok(OutputStream::from_input(
input.values.skip(first).take(last - first + 1),
));
}
None => {
return Err(ShellError::labeled_error(
"No correct formatted range found",
"format: <from>..<to>",
name,
));
}
}
}

View File

@ -204,6 +204,74 @@ fn group_by_errors_if_unknown_column_name() {
})
}
#[test]
fn range_selects_a_row() {
Playground::setup("range_test_1", |dirs, sandbox| {
sandbox.with_files(vec![EmptyFile("notes.txt"), EmptyFile("tests.txt")]);
let actual = nu!(
cwd: dirs.test(), h::pipeline(
r#"
ls
| sort-by name
| range 0..0
| get name
| echo $it
"#
));
assert_eq!(actual, "notes.txt");
});
}
#[test]
fn range_selects_some_rows() {
Playground::setup("range_test_2", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("notes.txt"),
EmptyFile("tests.txt"),
EmptyFile("persons.txt"),
]);
let actual = nu!(
cwd: dirs.test(), h::pipeline(
r#"
ls
| get name
| range 1..2
| count
| echo $it
"#
));
assert_eq!(actual, "2");
});
}
#[test]
fn range_selects_all_rows() {
Playground::setup("range_test_3", |dirs, sandbox| {
sandbox.with_files(vec![
EmptyFile("notes.txt"),
EmptyFile("tests.txt"),
EmptyFile("persons.txt"),
]);
let actual = nu!(
cwd: dirs.test(), h::pipeline(
r#"
ls
| get name
| range ..
| count
| echo $it
"#
));
assert_eq!(actual, "3");
});
}
#[test]
fn split_by() {
Playground::setup("split_by_test_1", |dirs, sandbox| {