2021-10-09 04:45:25 +02:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::{
|
|
|
|
ast::Call,
|
2021-10-25 18:58:58 +02:00
|
|
|
engine::{Command, EngineState, Stack},
|
2022-02-19 16:24:48 +01:00
|
|
|
Category, Example, PipelineData, ShellError, Signature, Span, Spanned, SyntaxShape, Value,
|
2021-10-09 04:45:25 +02:00
|
|
|
};
|
|
|
|
|
2021-10-25 06:01:02 +02:00
|
|
|
#[derive(Clone)]
|
2021-10-09 04:45:25 +02:00
|
|
|
pub struct SubCommand;
|
|
|
|
|
|
|
|
impl Command for SubCommand {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"split row"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> Signature {
|
2021-11-17 05:22:37 +01:00
|
|
|
Signature::build("split row")
|
|
|
|
.required(
|
|
|
|
"separator",
|
|
|
|
SyntaxShape::String,
|
|
|
|
"the character that denotes what separates rows",
|
|
|
|
)
|
2022-05-06 17:53:02 +02:00
|
|
|
.named(
|
|
|
|
"number",
|
|
|
|
SyntaxShape::Int,
|
|
|
|
"Split into maximum number of items",
|
|
|
|
Some('n'),
|
|
|
|
)
|
2021-11-17 05:22:37 +01:00
|
|
|
.category(Category::Strings)
|
2021-10-09 04:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2022-04-08 10:30:49 +02:00
|
|
|
"Split a string into multiple rows using a separator"
|
2021-10-09 04:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
2021-10-25 08:31:39 +02:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-09 04:45:25 +02:00
|
|
|
call: &Call,
|
2021-10-25 06:01:02 +02:00
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
2021-10-25 08:31:39 +02:00
|
|
|
split_row(engine_state, stack, call, input)
|
2021-10-09 04:45:25 +02:00
|
|
|
}
|
2022-02-19 16:24:48 +01:00
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
description: "Split a string into rows of char",
|
|
|
|
example: "echo 'abc' | split row ''",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::test_string("a"),
|
|
|
|
Value::test_string("b"),
|
|
|
|
Value::test_string("c"),
|
|
|
|
],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
description: "Split a string into rows by the specified separator",
|
|
|
|
example: "echo 'a--b--c' | split row '--'",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::test_string("a"),
|
|
|
|
Value::test_string("b"),
|
|
|
|
Value::test_string("c"),
|
|
|
|
],
|
|
|
|
span: Span::test_data(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
2021-10-09 04:45:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
fn split_row(
|
2021-10-25 08:31:39 +02:00
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
2021-10-09 04:45:25 +02:00
|
|
|
call: &Call,
|
2021-10-25 06:01:02 +02:00
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<nu_protocol::PipelineData, nu_protocol::ShellError> {
|
2021-10-09 04:45:25 +02:00
|
|
|
let name_span = call.head;
|
2021-10-25 08:31:39 +02:00
|
|
|
let separator: Spanned<String> = call.req(engine_state, stack, 0)?;
|
2022-05-06 17:53:02 +02:00
|
|
|
let max_split: Option<usize> = call.get_flag(engine_state, stack, "number")?;
|
2021-10-28 06:13:10 +02:00
|
|
|
input.flat_map(
|
2022-05-06 17:53:02 +02:00
|
|
|
move |x| split_row_helper(&x, &separator, max_split, name_span),
|
2021-10-28 06:13:10 +02:00
|
|
|
engine_state.ctrlc.clone(),
|
|
|
|
)
|
2021-10-09 04:45:25 +02:00
|
|
|
}
|
|
|
|
|
2022-05-06 17:53:02 +02:00
|
|
|
fn split_row_helper(
|
|
|
|
v: &Value,
|
|
|
|
separator: &Spanned<String>,
|
|
|
|
max_split: Option<usize>,
|
|
|
|
name: Span,
|
|
|
|
) -> Vec<Value> {
|
2021-10-11 20:45:31 +02:00
|
|
|
match v.span() {
|
|
|
|
Ok(v_span) => {
|
|
|
|
if let Ok(s) = v.as_string() {
|
2022-05-06 17:53:02 +02:00
|
|
|
match max_split {
|
|
|
|
Some(max_split) => s
|
|
|
|
.splitn(max_split, &separator.item)
|
|
|
|
.filter_map(|s| {
|
|
|
|
if s.trim() != "" {
|
|
|
|
Some(Value::string(s, v_span))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
None => s
|
|
|
|
.split(&separator.item)
|
|
|
|
.filter_map(|s| {
|
|
|
|
if s.trim() != "" {
|
|
|
|
Some(Value::string(s, v_span))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
}
|
2021-10-11 20:45:31 +02:00
|
|
|
} else {
|
|
|
|
vec![Value::Error {
|
2021-12-03 00:11:25 +01:00
|
|
|
error: ShellError::PipelineMismatch("string".into(), name, v_span),
|
2021-10-11 20:45:31 +02:00
|
|
|
}]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(error) => vec![Value::Error { error }],
|
2021-10-09 04:45:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// #[cfg(test)]
|
|
|
|
// mod tests {
|
|
|
|
// use super::ShellError;
|
|
|
|
// use super::SubCommand;
|
|
|
|
|
|
|
|
// #[test]
|
|
|
|
// fn examples_work_as_expected() -> Result<(), ShellError> {
|
|
|
|
// use crate::examples::test as test_examples;
|
|
|
|
|
|
|
|
// test_examples(SubCommand {})
|
|
|
|
// }
|
|
|
|
// }
|