forked from extern/nushell
Split split command to sub commands.
This commit is contained in:
117
crates/nu-cli/src/commands/split/column.rs
Normal file
117
crates/nu-cli/src/commands/split/column.rs
Normal file
@ -0,0 +1,117 @@
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::prelude::*;
|
||||
use log::trace;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{
|
||||
Primitive, ReturnSuccess, Signature, SyntaxShape, TaggedDictBuilder, UntaggedValue,
|
||||
};
|
||||
use nu_source::Tagged;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SplitColumnArgs {
|
||||
separator: Tagged<String>,
|
||||
rest: Vec<Tagged<String>>,
|
||||
#[serde(rename(deserialize = "collapse-empty"))]
|
||||
collapse_empty: bool,
|
||||
}
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
impl WholeStreamCommand for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"split column"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("split column")
|
||||
.required(
|
||||
"separator",
|
||||
SyntaxShape::Any,
|
||||
"the character that denotes what separates columns",
|
||||
)
|
||||
.switch("collapse-empty", "remove empty columns", Some('c'))
|
||||
.rest(SyntaxShape::String, "column names to give the new columns")
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"splits contents across multiple columns via the separator."
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
split_column(args, registry)
|
||||
}
|
||||
}
|
||||
|
||||
fn split_column(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||
let name_span = args.call_info.name_tag.span;
|
||||
let registry = registry.clone();
|
||||
let stream = async_stream! {
|
||||
let (SplitColumnArgs { separator, rest, collapse_empty }, mut input) = args.process(®istry).await?;
|
||||
while let Some(v) = input.next().await {
|
||||
if let Ok(s) = v.as_string() {
|
||||
let splitter = separator.replace("\\n", "\n");
|
||||
trace!("splitting with {:?}", splitter);
|
||||
|
||||
let split_result: Vec<_> = if collapse_empty {
|
||||
s.split(&splitter).filter(|s| !s.is_empty()).collect()
|
||||
} else {
|
||||
s.split(&splitter).collect()
|
||||
};
|
||||
|
||||
trace!("split result = {:?}", split_result);
|
||||
|
||||
let positional: Vec<_> = rest.iter().map(|f| f.item.clone()).collect();
|
||||
|
||||
// If they didn't provide column names, make up our own
|
||||
if positional.is_empty() {
|
||||
let mut gen_columns = vec![];
|
||||
for i in 0..split_result.len() {
|
||||
gen_columns.push(format!("Column{}", i + 1));
|
||||
}
|
||||
|
||||
let mut dict = TaggedDictBuilder::new(&v.tag);
|
||||
for (&k, v) in split_result.iter().zip(gen_columns.iter()) {
|
||||
dict.insert_untagged(v.clone(), Primitive::String(k.into()));
|
||||
}
|
||||
|
||||
yield ReturnSuccess::value(dict.into_value());
|
||||
} else {
|
||||
let mut dict = TaggedDictBuilder::new(&v.tag);
|
||||
for (&k, v) in split_result.iter().zip(positional.iter()) {
|
||||
dict.insert_untagged(
|
||||
v,
|
||||
UntaggedValue::Primitive(Primitive::String(k.into())),
|
||||
);
|
||||
}
|
||||
yield ReturnSuccess::value(dict.into_value());
|
||||
}
|
||||
} else {
|
||||
yield Err(ShellError::labeled_error_with_secondary(
|
||||
"Expected a string from pipeline",
|
||||
"requires string input",
|
||||
name_span,
|
||||
"value originates from here",
|
||||
v.tag.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(stream.to_output_stream())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::SubCommand;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() {
|
||||
use crate::examples::test as test_examples;
|
||||
|
||||
test_examples(SubCommand {})
|
||||
}
|
||||
}
|
49
crates/nu-cli/src/commands/split/command.rs
Normal file
49
crates/nu-cli/src/commands/split/command.rs
Normal 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, ®istry))
|
||||
.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 {})
|
||||
}
|
||||
}
|
7
crates/nu-cli/src/commands/split/mod.rs
Normal file
7
crates/nu-cli/src/commands/split/mod.rs
Normal 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;
|
84
crates/nu-cli/src/commands/split/row.rs
Normal file
84
crates/nu-cli/src/commands/split/row.rs
Normal file
@ -0,0 +1,84 @@
|
||||
use crate::commands::WholeStreamCommand;
|
||||
use crate::prelude::*;
|
||||
use log::trace;
|
||||
use nu_errors::ShellError;
|
||||
use nu_protocol::{Primitive, ReturnSuccess, Signature, SyntaxShape, UntaggedValue};
|
||||
use nu_source::Tagged;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct SplitRowArgs {
|
||||
separator: Tagged<String>,
|
||||
}
|
||||
|
||||
pub struct SubCommand;
|
||||
|
||||
impl WholeStreamCommand for SubCommand {
|
||||
fn name(&self) -> &str {
|
||||
"split row"
|
||||
}
|
||||
|
||||
fn signature(&self) -> Signature {
|
||||
Signature::build("split row").required(
|
||||
"separator",
|
||||
SyntaxShape::Any,
|
||||
"the character that denotes what separates rows",
|
||||
)
|
||||
}
|
||||
|
||||
fn usage(&self) -> &str {
|
||||
"splits contents over multiple rows via the separator."
|
||||
}
|
||||
|
||||
fn run(
|
||||
&self,
|
||||
args: CommandArgs,
|
||||
registry: &CommandRegistry,
|
||||
) -> Result<OutputStream, ShellError> {
|
||||
split_row(args, registry)
|
||||
}
|
||||
}
|
||||
|
||||
fn split_row(args: CommandArgs, registry: &CommandRegistry) -> Result<OutputStream, ShellError> {
|
||||
let registry = registry.clone();
|
||||
let stream = async_stream! {
|
||||
let name = args.call_info.name_tag.clone();
|
||||
let (SplitRowArgs { separator }, mut input) = args.process(®istry).await?;
|
||||
while let Some(v) = input.next().await {
|
||||
if let Ok(s) = v.as_string() {
|
||||
let splitter = separator.item.replace("\\n", "\n");
|
||||
trace!("splitting with {:?}", splitter);
|
||||
let split_result: Vec<_> = s.split(&splitter).filter(|s| s.trim() != "").collect();
|
||||
|
||||
trace!("split result = {:?}", split_result);
|
||||
|
||||
for s in split_result {
|
||||
yield ReturnSuccess::value(
|
||||
UntaggedValue::Primitive(Primitive::String(s.into())).into_value(&v.tag),
|
||||
);
|
||||
}
|
||||
} else {
|
||||
yield Err(ShellError::labeled_error_with_secondary(
|
||||
"Expected a string from pipeline",
|
||||
"requires string input",
|
||||
name.span,
|
||||
"value originates from here",
|
||||
v.tag.span,
|
||||
));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
Ok(stream.to_output_stream())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::SubCommand;
|
||||
|
||||
#[test]
|
||||
fn examples_work_as_expected() {
|
||||
use crate::examples::test as test_examples;
|
||||
|
||||
test_examples(SubCommand {})
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user