2021-12-07 09:46:21 +01:00
|
|
|
use nu_engine::CallExt;
|
|
|
|
use nu_protocol::ast::Call;
|
|
|
|
use nu_protocol::engine::{Command, EngineState, Stack};
|
|
|
|
use nu_protocol::{
|
|
|
|
Category, Example, IntoInterruptiblePipelineData, PipelineData, ShellError, Signature, Span,
|
|
|
|
SyntaxShape, Value,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
pub struct Prepend;
|
|
|
|
|
|
|
|
impl Command for Prepend {
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
"prepend"
|
|
|
|
}
|
|
|
|
|
|
|
|
fn signature(&self) -> nu_protocol::Signature {
|
|
|
|
Signature::build("prepend")
|
2022-04-04 00:50:22 +02:00
|
|
|
.required(
|
|
|
|
"row",
|
|
|
|
SyntaxShape::Any,
|
|
|
|
"the row, list, or table to prepend",
|
|
|
|
)
|
2021-12-07 09:46:21 +01:00
|
|
|
.category(Category::Filters)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn usage(&self) -> &str {
|
2022-04-04 00:50:22 +02:00
|
|
|
"Prepend any number of rows to a table."
|
|
|
|
}
|
|
|
|
|
|
|
|
fn search_terms(&self) -> Vec<&str> {
|
2022-04-05 14:01:21 +02:00
|
|
|
vec!["add", "concatenate"]
|
2021-12-07 09:46:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
fn examples(&self) -> Vec<Example> {
|
|
|
|
vec![
|
|
|
|
Example {
|
|
|
|
example: "[1,2,3,4] | prepend 0",
|
|
|
|
description: "Prepend one Int item",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::test_int(0),
|
|
|
|
Value::test_int(1),
|
|
|
|
Value::test_int(2),
|
|
|
|
Value::test_int(3),
|
|
|
|
Value::test_int(4),
|
|
|
|
],
|
2021-12-19 08:46:13 +01:00
|
|
|
span: Span::test_data(),
|
2021-12-07 09:46:21 +01:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
example: "[2,3,4] | prepend [0,1]",
|
|
|
|
description: "Prepend two Int items",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::test_int(0),
|
|
|
|
Value::test_int(1),
|
|
|
|
Value::test_int(2),
|
|
|
|
Value::test_int(3),
|
|
|
|
Value::test_int(4),
|
|
|
|
],
|
2021-12-19 08:46:13 +01:00
|
|
|
span: Span::test_data(),
|
2021-12-07 09:46:21 +01:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
Example {
|
|
|
|
example: "[2,nu,4,shell] | prepend [0,1,rocks]",
|
|
|
|
description: "Prepend Ints and Strings",
|
|
|
|
result: Some(Value::List {
|
|
|
|
vals: vec![
|
|
|
|
Value::test_int(0),
|
|
|
|
Value::test_int(1),
|
|
|
|
Value::test_string("rocks"),
|
|
|
|
Value::test_int(2),
|
|
|
|
Value::test_string("nu"),
|
|
|
|
Value::test_int(4),
|
|
|
|
Value::test_string("shell"),
|
|
|
|
],
|
2021-12-19 08:46:13 +01:00
|
|
|
span: Span::test_data(),
|
2021-12-07 09:46:21 +01:00
|
|
|
}),
|
|
|
|
},
|
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
fn run(
|
|
|
|
&self,
|
|
|
|
engine_state: &EngineState,
|
|
|
|
stack: &mut Stack,
|
|
|
|
call: &Call,
|
|
|
|
input: PipelineData,
|
|
|
|
) -> Result<PipelineData, ShellError> {
|
|
|
|
let val: Value = call.req(engine_state, stack, 0)?;
|
|
|
|
let vec: Vec<Value> = process_value(val);
|
2022-03-28 13:43:09 +02:00
|
|
|
let metadata = input.metadata();
|
2021-12-07 09:46:21 +01:00
|
|
|
|
|
|
|
Ok(vec
|
|
|
|
.into_iter()
|
|
|
|
.chain(input)
|
|
|
|
.into_iter()
|
2022-03-28 13:43:09 +02:00
|
|
|
.into_pipeline_data(engine_state.ctrlc.clone())
|
|
|
|
.set_metadata(metadata))
|
2021-12-07 09:46:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn process_value(val: Value) -> Vec<Value> {
|
|
|
|
match val {
|
|
|
|
Value::List {
|
|
|
|
vals: input_vals,
|
|
|
|
span: _,
|
|
|
|
} => {
|
|
|
|
let mut output = vec![];
|
|
|
|
for input_val in input_vals {
|
|
|
|
output.push(input_val);
|
|
|
|
}
|
|
|
|
output
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
vec![val]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_examples() {
|
|
|
|
use crate::test_examples;
|
|
|
|
|
|
|
|
test_examples(Prepend {})
|
|
|
|
}
|
|
|
|
}
|