nushell/src/commands/skip_while.rs

63 lines
1.5 KiB
Rust
Raw Normal View History

2019-06-18 02:39:57 +02:00
use crate::errors::ShellError;
2019-06-22 05:43:37 +02:00
use crate::parser::registry::CommandConfig;
2019-06-18 02:39:57 +02:00
use crate::parser::registry::PositionalType;
use crate::prelude::*;
pub struct SkipWhile;
impl Command for SkipWhile {
2019-07-24 00:22:11 +02:00
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
skip_while(args, registry)
2019-06-18 02:39:57 +02:00
}
fn name(&self) -> &str {
"skip-while"
}
fn config(&self) -> CommandConfig {
CommandConfig {
name: self.name().to_string(),
2019-07-13 04:07:06 +02:00
positional: vec![PositionalType::mandatory_block("condition")],
2019-06-18 02:39:57 +02:00
rest_positional: false,
named: indexmap::IndexMap::new(),
2019-07-02 09:56:20 +02:00
is_filter: true,
is_sink: false,
2019-06-18 02:39:57 +02:00
}
}
}
2019-07-24 00:22:11 +02:00
pub fn skip_while(
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let args = args.evaluate_once(registry)?;
let block = args.expect_nth(0)?.as_block()?;
let span = args.name_span();
let len = args.len();
let input = args.input;
if len == 0 {
2019-06-18 02:39:57 +02:00
return Err(ShellError::maybe_labeled_error(
"Where requires a condition",
"needs condition",
2019-07-24 00:22:11 +02:00
span,
2019-06-18 02:39:57 +02:00
));
}
let objects = input.values.skip_while(move |item| {
2019-06-18 02:39:57 +02:00
let result = block.invoke(&item);
let return_value = match result {
Ok(v) if v.is_true() => true,
_ => false,
};
futures::future::ready(return_value)
});
Ok(objects.from_input_stream())
2019-06-22 05:43:37 +02:00
}