diff --git a/src/cli.rs b/src/cli.rs index 2c813c9dc4..0677634321 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -309,6 +309,7 @@ pub async fn cli() -> Result<(), Box> { whole_stream_command(What), whole_stream_command(Which), whole_stream_command(Debug), + whole_stream_command(Range), ]); cfg_if::cfg_if! { diff --git a/src/commands.rs b/src/commands.rs index f2e6ef477b..899ac967c0 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -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; diff --git a/src/commands/range.rs b/src/commands/range.rs new file mode 100644 index 0000000000..c110e21fac --- /dev/null +++ b/src/commands/range.rs @@ -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, +} + +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 { + args.process(registry, range)?.run() + } +} + +fn range( + RangeArgs { area: rows }: RangeArgs, + RunnableContext { input, name, .. }: RunnableContext, +) -> Result { + match rows.item.find(".") { + Some(value) => { + let (first, last) = rows.item.split_at(value); + let first = match first.parse::() { + 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::() { + 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: ..", + name, + )); + } + } +} diff --git a/tests/commands_test.rs b/tests/commands_test.rs index 89ab430b36..776998e633 100644 --- a/tests/commands_test.rs +++ b/tests/commands_test.rs @@ -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| {