mirror of
https://github.com/nushell/nushell.git
synced 2024-12-24 16:09:11 +01:00
Negative indexing for range (#3427)
* adds negative indexing to range * fixes tests to reflect new parsing changes * removes duplicate definitons * fmt
This commit is contained in:
parent
dc9cd7d8b9
commit
86e6fcd309
@ -1,7 +1,7 @@
|
||||
use crate::prelude::*;
|
||||
use nu_engine::WholeStreamCommand;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Signature, SyntaxShape, Value};
|
||||
use nu_protocol::{Signature, SyntaxShape, UntaggedValue, Value};
|
||||
|
||||
struct RangeArgs {
|
||||
range: nu_protocol::Range,
|
||||
@ -26,6 +26,29 @@ impl WholeStreamCommand for Range {
|
||||
"Return only the selected rows."
|
||||
}
|
||||
|
||||
fn examples(&self) -> Vec<Example> {
|
||||
vec![
|
||||
Example {
|
||||
description: "Return rows 1 through 3",
|
||||
example: "echo [1 2 3 4 5] | range 1..3",
|
||||
result: Some(vec![
|
||||
UntaggedValue::int(2).into(),
|
||||
UntaggedValue::int(3).into(),
|
||||
UntaggedValue::int(4).into(),
|
||||
]),
|
||||
},
|
||||
Example {
|
||||
description: "Return the third row from the end, through to the end",
|
||||
example: "echo [1 2 3 4 5] | range (-3..)",
|
||||
result: Some(vec![
|
||||
UntaggedValue::int(3).into(),
|
||||
UntaggedValue::int(4).into(),
|
||||
UntaggedValue::int(5).into(),
|
||||
]),
|
||||
},
|
||||
]
|
||||
}
|
||||
|
||||
fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
range(args)
|
||||
}
|
||||
@ -37,13 +60,40 @@ fn range(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
||||
range: args.req(0)?,
|
||||
};
|
||||
|
||||
let from = cmd_args.range.min_usize()?;
|
||||
let to = cmd_args.range.max_usize()?;
|
||||
let from_raw = cmd_args.range.min_i64()?;
|
||||
let to_raw = cmd_args.range.max_i64()?;
|
||||
// only collect the input if we have any negative indices
|
||||
if from_raw < 0 || to_raw < 0 {
|
||||
let input = args.input.into_vec();
|
||||
let input_size = input.len() as i64;
|
||||
|
||||
if from > to {
|
||||
Ok(OutputStream::one(Value::nothing()))
|
||||
let from = if from_raw < 0 {
|
||||
(input_size + from_raw) as usize
|
||||
} else {
|
||||
from_raw as usize
|
||||
};
|
||||
|
||||
let to = if to_raw < 0 {
|
||||
(input_size + to_raw) as usize
|
||||
} else if to_raw > input.len() as i64 {
|
||||
input.len()
|
||||
} else {
|
||||
to_raw as usize
|
||||
};
|
||||
|
||||
if from > to {
|
||||
Ok(OutputStream::one(Value::nothing()))
|
||||
} else {
|
||||
Ok(OutputStream::from(input[from..to].to_vec()))
|
||||
}
|
||||
} else {
|
||||
Ok(args.input.skip(from).take(to - from + 1).to_output_stream())
|
||||
let from = from_raw as usize;
|
||||
let to = to_raw as usize;
|
||||
if from > to {
|
||||
Ok(OutputStream::one(Value::nothing()))
|
||||
} else {
|
||||
Ok(args.input.skip(from).take(to - from + 1).to_output_stream())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -43,3 +43,26 @@ fn selects_some_rows() {
|
||||
assert_eq!(actual.out, "2");
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn negative_indices() {
|
||||
Playground::setup("range_test_negative_indices", |dirs, sandbox| {
|
||||
sandbox.with_files(vec![
|
||||
EmptyFile("notes.txt"),
|
||||
EmptyFile("tests.txt"),
|
||||
EmptyFile("persons.txt"),
|
||||
]);
|
||||
|
||||
let actual = nu!(
|
||||
cwd: dirs.test(), pipeline(
|
||||
r#"
|
||||
ls
|
||||
| get name
|
||||
| range (-1..)
|
||||
| length
|
||||
"#
|
||||
));
|
||||
|
||||
assert_eq!(actual.out, "1");
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user