forked from extern/nushell
* Begin allowing comments and multiline scripts. * clippy * Finish moving to groups. Test pass * Keep going * WIP * WIP * BROKEN WIP * WIP * WIP * Fix more tests * WIP: alias starts working * Broken WIP * Broken WIP * Variables begin to work * captures start working * A little better but needs fixed scope * Shorthand env setting * Update main merge * Broken WIP * WIP * custom command parsing * Custom commands start working * Fix coloring and parsing of block * Almost there * Add some tests * Add more param types * Bump version * Fix benchmark * Fix stuff
78 lines
1.8 KiB
Rust
78 lines
1.8 KiB
Rust
use crate::commands::WholeStreamCommand;
|
|
use crate::deserializer::NumericRange;
|
|
use crate::prelude::*;
|
|
use nu_errors::ShellError;
|
|
use nu_protocol::{RangeInclusion, ReturnSuccess, Signature, SyntaxShape};
|
|
use nu_source::Tagged;
|
|
|
|
#[derive(Deserialize)]
|
|
struct RangeArgs {
|
|
area: Tagged<NumericRange>,
|
|
}
|
|
|
|
pub struct Range;
|
|
|
|
#[async_trait]
|
|
impl WholeStreamCommand for Range {
|
|
fn name(&self) -> &str {
|
|
"range"
|
|
}
|
|
|
|
fn signature(&self) -> Signature {
|
|
Signature::build("range").required(
|
|
"rows ",
|
|
SyntaxShape::Range,
|
|
"range of rows to return: Eg) 4..7 (=> from 4 to 7)",
|
|
)
|
|
}
|
|
|
|
fn usage(&self) -> &str {
|
|
"Return only the selected rows"
|
|
}
|
|
|
|
async fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
range(args).await
|
|
}
|
|
}
|
|
|
|
async fn range(args: CommandArgs) -> Result<OutputStream, ShellError> {
|
|
let (RangeArgs { area }, input) = args.process().await?;
|
|
let range = area.item;
|
|
let (from, left_inclusive) = range.from;
|
|
let (to, right_inclusive) = range.to;
|
|
let from = from.map(|from| *from as usize).unwrap_or(0).saturating_add(
|
|
if left_inclusive == RangeInclusion::Inclusive {
|
|
0
|
|
} else {
|
|
1
|
|
},
|
|
);
|
|
let to = to
|
|
.map(|to| *to as usize)
|
|
.unwrap_or(usize::MAX)
|
|
.saturating_sub(if right_inclusive == RangeInclusion::Inclusive {
|
|
0
|
|
} else {
|
|
1
|
|
});
|
|
|
|
Ok(input
|
|
.skip(from)
|
|
.take(to - from + 1)
|
|
.map(ReturnSuccess::value)
|
|
.to_output_stream())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::Range;
|
|
use super::ShellError;
|
|
|
|
#[test]
|
|
fn examples_work_as_expected() -> Result<(), ShellError> {
|
|
use crate::examples::test as test_examples;
|
|
|
|
Ok(test_examples(Range {})?)
|
|
}
|
|
}
|