mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
commit
1d196394f6
@ -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! {
|
||||
|
@ -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
88
src/commands/range.rs
Normal 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,
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
@ -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| {
|
||||
|
Loading…
Reference in New Issue
Block a user