nushell/src/commands/skip_while.rs

49 lines
1.1 KiB
Rust
Raw Normal View History

2019-08-15 07:02:02 +02:00
use crate::commands::WholeStreamCommand;
2019-06-18 02:39:57 +02:00
use crate::errors::ShellError;
use crate::prelude::*;
pub struct SkipWhile;
2019-08-02 21:15:07 +02:00
#[derive(Deserialize)]
pub struct SkipWhileArgs {
condition: value::Block,
}
2019-08-15 07:02:02 +02:00
impl WholeStreamCommand for SkipWhile {
2019-08-02 21:15:07 +02:00
fn name(&self) -> &str {
"skip-while"
}
fn signature(&self) -> Signature {
Signature::build("skip-while")
.required("condition", SyntaxType::Block)
.filter()
}
2019-07-24 00:22:11 +02:00
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
2019-08-02 21:15:07 +02:00
args.process(registry, skip_while)?.run()
2019-06-18 02:39:57 +02:00
}
}
2019-07-24 00:22:11 +02:00
pub fn skip_while(
2019-08-02 21:15:07 +02:00
SkipWhileArgs { condition }: SkipWhileArgs,
RunnableContext { input, .. }: RunnableContext,
2019-07-24 00:22:11 +02:00
) -> Result<OutputStream, ShellError> {
let objects = input.values.skip_while(move |item| {
2019-08-02 21:15:07 +02:00
let result = condition.invoke(&item);
2019-06-18 02:39:57 +02:00
let return_value = match result {
2019-08-29 04:44:08 +02:00
Ok(ref v) if v.is_true() => true,
2019-06-18 02:39:57 +02:00
_ => false,
};
futures::future::ready(return_value)
});
Ok(objects.from_input_stream())
2019-06-22 05:43:37 +02:00
}