nu_command/filters/
prepend.rs1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct Prepend;
5
6impl Command for Prepend {
7 fn name(&self) -> &str {
8 "prepend"
9 }
10
11 fn signature(&self) -> nu_protocol::Signature {
12 Signature::build("prepend")
13 .input_output_types(vec![(Type::Any, Type::List(Box::new(Type::Any)))])
14 .required(
15 "row",
16 SyntaxShape::Any,
17 "The row, list, or table to prepend.",
18 )
19 .category(Category::Filters)
20 }
21
22 fn description(&self) -> &str {
23 "Prepend any number of rows to a table."
24 }
25
26 fn extra_description(&self) -> &str {
27 r#"Be aware that this command 'unwraps' lists passed to it. So, if you pass a variable to it,
28and you want the variable's contents to be prepended without being unwrapped, it's wise to
29pre-emptively wrap the variable in a list, like so: `prepend [$val]`. This way, `prepend` will
30only unwrap the outer list, and leave the variable's contents untouched."#
31 }
32
33 fn search_terms(&self) -> Vec<&str> {
34 vec!["add", "concatenate"]
35 }
36
37 fn examples(&self) -> Vec<Example<'_>> {
38 vec![
39 Example {
40 example: "0 | prepend [1 2 3]",
41 description: "prepend a list to an item",
42 result: Some(Value::test_list(vec![
43 Value::test_int(1),
44 Value::test_int(2),
45 Value::test_int(3),
46 Value::test_int(0),
47 ])),
48 },
49 Example {
50 example: r#""a" | prepend ["b"] "#,
51 description: "Prepend a list of strings to a string",
52 result: Some(Value::test_list(vec![
53 Value::test_string("b"),
54 Value::test_string("a"),
55 ])),
56 },
57 Example {
58 example: "[1 2 3 4] | prepend 0",
59 description: "Prepend one int item",
60 result: Some(Value::test_list(vec![
61 Value::test_int(0),
62 Value::test_int(1),
63 Value::test_int(2),
64 Value::test_int(3),
65 Value::test_int(4),
66 ])),
67 },
68 Example {
69 example: "[2 3 4] | prepend [0 1]",
70 description: "Prepend two int items",
71 result: Some(Value::test_list(vec![
72 Value::test_int(0),
73 Value::test_int(1),
74 Value::test_int(2),
75 Value::test_int(3),
76 Value::test_int(4),
77 ])),
78 },
79 Example {
80 example: "[2 nu 4 shell] | prepend [0 1 rocks]",
81 description: "Prepend ints and strings",
82 result: Some(Value::test_list(vec![
83 Value::test_int(0),
84 Value::test_int(1),
85 Value::test_string("rocks"),
86 Value::test_int(2),
87 Value::test_string("nu"),
88 Value::test_int(4),
89 Value::test_string("shell"),
90 ])),
91 },
92 Example {
93 example: "[3 4] | prepend 0..2",
94 description: "Prepend a range",
95 result: Some(Value::test_list(vec![
96 Value::test_int(0),
97 Value::test_int(1),
98 Value::test_int(2),
99 Value::test_int(3),
100 Value::test_int(4),
101 ])),
102 },
103 ]
104 }
105
106 fn run(
107 &self,
108 engine_state: &EngineState,
109 stack: &mut Stack,
110 call: &Call,
111 input: PipelineData,
112 ) -> Result<PipelineData, ShellError> {
113 let other: Value = call.req(engine_state, stack, 0)?;
114 let metadata = input.metadata();
115
116 Ok(other
117 .into_pipeline_data()
118 .into_iter()
119 .chain(input)
120 .into_pipeline_data_with_metadata(call.head, engine_state.signals().clone(), metadata))
121 }
122}
123
124#[cfg(test)]
125mod test {
126 use super::*;
127
128 #[test]
129 fn test_examples() {
130 use crate::test_examples;
131
132 test_examples(Prepend {})
133 }
134}