2019-12-02 20:15:14 +01:00
|
|
|
use crate::commands::WholeStreamCommand;
|
|
|
|
use crate::context::CommandRegistry;
|
2019-12-04 22:14:52 +01:00
|
|
|
use crate::deserializer::NumericRange;
|
2019-12-02 20:15:14 +01:00
|
|
|
use crate::prelude::*;
|
2019-12-03 20:56:39 +01:00
|
|
|
use nu_errors::ShellError;
|
|
|
|
use nu_protocol::{Signature, SyntaxShape};
|
2019-12-02 20:15:14 +01:00
|
|
|
use nu_source::Tagged;
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
struct RangeArgs {
|
2019-12-04 22:14:52 +01:00
|
|
|
area: Tagged<NumericRange>,
|
2019-12-02 20:15:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct Range;
|
|
|
|
|
|
|
|
impl WholeStreamCommand for Range {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"range"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
|
|
|
Signature::build("range").required(
|
|
|
|
"rows ",
|
2019-12-04 22:14:52 +01:00
|
|
|
SyntaxShape::Range,
|
2019-12-02 20:15:14 +01:00
|
|
|
"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(
|
2019-12-04 22:14:52 +01:00
|
|
|
RangeArgs { area }: RangeArgs,
|
|
|
|
RunnableContext { input, name: _, .. }: RunnableContext,
|
2019-12-03 08:24:49 +01:00
|
|
|
) -> Result<OutputStream, ShellError> {
|
2019-12-04 22:14:52 +01:00
|
|
|
let range = area.item;
|
|
|
|
let (from, _) = range.from;
|
|
|
|
let (to, _) = range.to;
|
|
|
|
|
|
|
|
return Ok(OutputStream::from_input(
|
|
|
|
input.values.skip(*from).take(*to - *from + 1),
|
|
|
|
));
|
2019-12-03 08:24:49 +01:00
|
|
|
}
|