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")
|
2019-09-11 05:23:22 +02:00
|
|
|
.required("condition", SyntaxShape::Block)
|
2019-08-02 21:15:07 +02:00
|
|
|
.filter()
|
|
|
|
}
|
|
|
|
|
2019-08-30 00:52:32 +02:00
|
|
|
fn usage(&self) -> &str {
|
|
|
|
"Skips rows while the condition matches."
|
|
|
|
}
|
|
|
|
|
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> {
|
2019-07-03 22:31:15 +02:00
|
|
|
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)
|
|
|
|
});
|
|
|
|
|
2019-07-03 22:31:15 +02:00
|
|
|
Ok(objects.from_input_stream())
|
2019-06-22 05:43:37 +02:00
|
|
|
}
|