Merge pull request #1003 from andrasio/compact

Compact.
This commit is contained in:
Andrés N. Robalino
2019-11-23 22:03:20 -05:00
committed by GitHub
6 changed files with 117 additions and 1 deletions

View File

@ -309,6 +309,7 @@ pub async fn cli() -> Result<(), Box<dyn Error>> {
per_item_command(Where),
per_item_command(Echo),
whole_stream_command(Config),
whole_stream_command(Compact),
whole_stream_command(SkipWhile),
per_item_command(Enter),
per_item_command(Help),

View File

@ -11,6 +11,7 @@ pub(crate) mod cd;
pub(crate) mod classified;
pub(crate) mod clip;
pub(crate) mod command;
pub(crate) mod compact;
pub(crate) mod config;
pub(crate) mod count;
pub(crate) mod cp;
@ -98,6 +99,7 @@ pub(crate) use command::{
pub(crate) use append::Append;
pub(crate) use classified::ClassifiedCommand;
pub(crate) use compact::Compact;
pub(crate) use config::Config;
pub(crate) use count::Count;
pub(crate) use cp::Cpy;

53
src/commands/compact.rs Normal file
View File

@ -0,0 +1,53 @@
use crate::commands::WholeStreamCommand;
use crate::errors::ShellError;
use crate::parser::registry::{CommandRegistry, Signature};
use crate::prelude::*;
use futures::stream::StreamExt;
pub struct Compact;
#[derive(Deserialize)]
pub struct CompactArgs {
rest: Vec<Tagged<String>>,
}
impl WholeStreamCommand for Compact {
fn name(&self) -> &str {
"compact"
}
fn signature(&self) -> Signature {
Signature::build("compact").rest(SyntaxShape::Any, "the columns to compact from the table")
}
fn usage(&self) -> &str {
"Creates a table with non-empty rows"
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, compact)?.run()
}
}
pub fn compact(
CompactArgs { rest: columns }: CompactArgs,
RunnableContext { input, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> {
let objects = input.values.take_while(move |item| {
let keep = if columns.is_empty() {
item.is_some()
} else {
columns
.iter()
.all(|field| item.get_data(field).borrow().is_some())
};
futures::future::ready(keep)
});
Ok(objects.from_input_stream())
}

View File

@ -498,6 +498,17 @@ impl Value {
}
}
pub(crate) fn is_some(&self) -> bool {
!self.is_none()
}
pub(crate) fn is_none(&self) -> bool {
match self {
Value::Primitive(Primitive::Nothing) => true,
_ => false,
}
}
pub(crate) fn is_error(&self) -> bool {
match self {
Value::Error(_err) => true,