nushell/src/commands/split_column.rs

104 lines
3.5 KiB
Rust
Raw Normal View History

use crate::commands::WholeStreamCommand;
use crate::data::{Primitive, TaggedDictBuilder, Value};
2019-09-11 16:36:50 +02:00
use crate::errors::ShellError;
2019-05-25 03:20:03 +02:00
use crate::prelude::*;
use log::trace;
2019-05-25 03:20:03 +02:00
#[derive(Deserialize)]
struct SplitColumnArgs {
2019-08-20 08:11:11 +02:00
separator: Tagged<String>,
rest: Vec<Tagged<String>>,
2019-08-27 13:30:09 +02:00
#[serde(rename(deserialize = "collapse-empty"))]
collapse_empty: bool,
}
pub struct SplitColumn;
impl WholeStreamCommand for SplitColumn {
fn name(&self) -> &str {
"split-column"
}
fn signature(&self) -> Signature {
2019-08-20 08:11:11 +02:00
Signature::build("split-column")
.required("separator", SyntaxType::Any)
2019-08-27 13:30:09 +02:00
.switch("collapse-empty")
Add support for ~ expansion This ended up being a bit of a yak shave. The basic idea in this commit is to expand `~` in paths, but only in paths. The way this is accomplished is by doing the expansion inside of the code that parses literal syntax for `SyntaxType::Path`. As a quick refresher: every command is entitled to expand its arguments in a custom way. While this could in theory be used for general-purpose macros, today the expansion facility is limited to syntactic hints. For example, the syntax `where cpu > 0` expands under the hood to `where { $it.cpu > 0 }`. This happens because the first argument to `where` is defined as a `SyntaxType::Block`, and the parser coerces binary expressions whose left-hand-side looks like a member into a block when the command is expecting one. This is mildly more magical than what most programming languages would do, but we believe that it makes sense to allow commands to fine-tune the syntax because of the domain nushell is in (command-line shells). The syntactic expansions supported by this facility are relatively limited. For example, we don't allow `$it` to become a bare word, simply because the command asks for a string in the relevant position. That would quickly become more confusing than it's worth. This PR adds a new `SyntaxType` rule: `SyntaxType::Path`. When a command declares a parameter as a `SyntaxType::Path`, string literals and bare words passed as an argument to that parameter are processed using the path expansion rules. Right now, that only means that `~` is expanded into the home directory, but additional rules are possible in the future. By restricting this expansion to a syntactic expansion when passed as an argument to a command expecting a path, we avoid making `~` a generally reserved character. This will also allow us to give good tab completion for paths with `~` characters in them when a command is expecting a path. In order to accomplish the above, this commit changes the parsing functions to take a `Context` instead of just a `CommandRegistry`. From the perspective of macro expansion, you can think of the `CommandRegistry` as a dictionary of in-scope macros, and the `Context` as the compile-time state used in expansion. This could gain additional functionality over time as we find more uses for the expansion system.
2019-08-26 21:21:03 +02:00
.rest(SyntaxType::Member)
}
fn usage(&self) -> &str {
"Split row contents across multiple columns via the separator."
}
fn run(
&self,
args: CommandArgs,
registry: &CommandRegistry,
) -> Result<OutputStream, ShellError> {
args.process(registry, split_column)?.run()
}
}
fn split_column(
2019-09-11 16:36:50 +02:00
SplitColumnArgs {
separator,
rest,
collapse_empty,
}: SplitColumnArgs,
RunnableContext { input, name, .. }: RunnableContext,
) -> Result<OutputStream, ShellError> {
2019-05-25 03:20:03 +02:00
Ok(input
.values
2019-07-08 18:44:53 +02:00
.map(move |v| match v.item {
2019-08-01 03:58:42 +02:00
Value::Primitive(Primitive::String(ref s)) => {
2019-08-20 08:11:11 +02:00
let splitter = separator.replace("\\n", "\n");
trace!("splitting with {:?}", splitter);
2019-05-25 03:20:03 +02:00
2019-08-27 13:30:09 +02:00
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);
2019-05-26 08:54:41 +02:00
2019-08-20 08:11:11 +02:00
let positional: Vec<_> = rest.iter().map(|f| f.item.clone()).collect();
2019-05-28 04:01:37 +02:00
// If they didn't provide column names, make up our own
2019-08-20 08:11:11 +02:00
if positional.len() == 0 {
2019-05-28 04:01:37 +02:00
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());
2019-06-22 22:46:16 +02:00
for (&k, v) in split_result.iter().zip(gen_columns.iter()) {
2019-07-09 06:31:26 +02:00
dict.insert(v.clone(), Primitive::String(k.into()));
2019-05-28 04:01:37 +02:00
}
2019-07-09 06:31:26 +02:00
2019-08-01 03:58:42 +02:00
ReturnSuccess::value(dict.into_tagged_value())
2019-08-20 08:11:11 +02:00
} else if split_result.len() == positional.len() {
let mut dict = TaggedDictBuilder::new(v.tag());
2019-08-20 08:11:11 +02:00
for (&k, v) in split_result.iter().zip(positional.iter()) {
dict.insert(v, Value::Primitive(Primitive::String(k.into())));
2019-05-25 03:20:03 +02:00
}
2019-08-01 03:58:42 +02:00
ReturnSuccess::value(dict.into_tagged_value())
2019-05-25 03:20:03 +02:00
} else {
let mut dict = TaggedDictBuilder::new(v.tag());
2019-08-20 08:11:11 +02:00
for (&k, v) in split_result.iter().zip(positional.iter()) {
dict.insert(v, Value::Primitive(Primitive::String(k.into())));
2019-05-25 03:20:03 +02:00
}
2019-08-01 03:58:42 +02:00
ReturnSuccess::value(dict.into_tagged_value())
2019-05-25 03:20:03 +02:00
}
}
_ => Err(ShellError::labeled_error_with_secondary(
"Expected a string from pipeline",
"requires string input",
name,
"value originates from here",
v.span(),
)),
2019-05-25 03:20:03 +02:00
})
.to_output_stream())
2019-05-25 03:20:03 +02:00
}