Split split command to sub commands.

This commit is contained in:
Andrés N. Robalino
2020-05-24 01:41:30 -05:00
parent 74c2daf665
commit edbecda14d
22 changed files with 154 additions and 57 deletions

View File

@ -279,6 +279,7 @@ pub fn create_default_context(
whole_stream_command(Autoview),
whole_stream_command(Table),
// Text manipulation
whole_stream_command(Split),
whole_stream_command(SplitColumn),
whole_stream_command(SplitRow),
whole_stream_command(Lines),

View File

@ -96,9 +96,8 @@ pub(crate) mod skip;
pub(crate) mod skip_until;
pub(crate) mod skip_while;
pub(crate) mod sort_by;
pub(crate) mod split;
pub(crate) mod split_by;
pub(crate) mod split_column;
pub(crate) mod split_row;
pub(crate) mod sum;
#[allow(unused)]
pub(crate) mod t_sort_by;
@ -222,9 +221,10 @@ pub(crate) use skip::Skip;
pub(crate) use skip_until::SkipUntil;
pub(crate) use skip_while::SkipWhile;
pub(crate) use sort_by::SortBy;
pub(crate) use split::Split;
pub(crate) use split::SplitColumn;
pub(crate) use split::SplitRow;
pub(crate) use split_by::SplitBy;
pub(crate) use split_column::SplitColumn;
pub(crate) use split_row::SplitRow;
pub(crate) use sum::Sum;
#[allow(unused_imports)]
pub(crate) use t_sort_by::TSortBy;

View File

@ -33,7 +33,7 @@ impl WholeStreamCommand for Headers {
fn examples(&self) -> Vec<Example> {
vec![Example {
description: "Create headers for a raw string",
example: r#"echo "a b c|1 2 3" | split-row "|" | split-column " " | headers"#,
example: r#"echo "a b c|1 2 3" | split row "|" | split column " " | headers"#,
result: None,
}]
}

View File

@ -15,15 +15,15 @@ struct SplitColumnArgs {
collapse_empty: bool,
}
pub struct SplitColumn;
pub struct SubCommand;
impl WholeStreamCommand for SplitColumn {
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"split-column"
"split column"
}
fn signature(&self) -> Signature {
Signature::build("split-column")
Signature::build("split column")
.required(
"separator",
SyntaxShape::Any,
@ -34,7 +34,7 @@ impl WholeStreamCommand for SplitColumn {
}
fn usage(&self) -> &str {
"Split row contents across multiple columns via the separator."
"splits contents across multiple columns via the separator."
}
fn run(
@ -106,12 +106,12 @@ fn split_column(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputS
#[cfg(test)]
mod tests {
use super::SplitColumn;
use super::SubCommand;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(SplitColumn {})
test_examples(SubCommand {})
}
}

View File

@ -0,0 +1,49 @@
use crate::commands::WholeStreamCommand;
use crate::prelude::*;
use nu_errors::ShellError;
use nu_protocol::{ReturnSuccess, Signature, UntaggedValue};
#[derive(Clone)]
pub struct Command;
impl WholeStreamCommand for Command {
fn name(&self) -> &str {
"split"
}
fn signature(&self) -> Signature {
Signature::build("split")
}
fn usage(&self) -> &str {
"split contents across desired subcommand (like row, column) via the separator."
}
fn run(
&self,
_args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
let registry = registry.clone();
let stream = async_stream! {
yield Ok(ReturnSuccess::Value(
UntaggedValue::string(crate::commands::help::get_help(&Command, &registry))
.into_value(Tag::unknown()),
));
};
Ok(stream.to_output_stream())
}
}
#[cfg(test)]
mod tests {
use super::Command;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(Command {})
}
}

View File

@ -0,0 +1,7 @@
pub mod column;
pub mod command;
pub mod row;
pub use column::SubCommand as SplitColumn;
pub use command::Command as Split;
pub use row::SubCommand as SplitRow;

View File

@ -10,15 +10,15 @@ struct SplitRowArgs {
separator: Tagged<String>,
}
pub struct SplitRow;
pub struct SubCommand;
impl WholeStreamCommand for SplitRow {
impl WholeStreamCommand for SubCommand {
fn name(&self) -> &str {
"split-row"
"split row"
}
fn signature(&self) -> Signature {
Signature::build("split-row").required(
Signature::build("split row").required(
"separator",
SyntaxShape::Any,
"the character that denotes what separates rows",
@ -26,7 +26,7 @@ impl WholeStreamCommand for SplitRow {
}
fn usage(&self) -> &str {
"Split row contents over multiple rows via the separator."
"splits contents over multiple rows via the separator."
}
fn run(
@ -73,12 +73,12 @@ fn split_row(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStre
#[cfg(test)]
mod tests {
use super::SplitRow;
use super::SubCommand;
#[test]
fn examples_work_as_expected() {
use crate::examples::test as test_examples;
test_examples(SplitRow {})
test_examples(SubCommand {})
}
}