1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
use nu_engine::command_prelude::*;

#[derive(Clone)]
pub struct Lines;

impl Command for Lines {
    fn name(&self) -> &str {
        "lines"
    }

    fn usage(&self) -> &str {
        "Converts input to lines."
    }

    fn signature(&self) -> nu_protocol::Signature {
        Signature::build("lines")
            .input_output_types(vec![(Type::Any, Type::List(Box::new(Type::String)))])
            .switch("skip-empty", "skip empty lines", Some('s'))
            .category(Category::Filters)
    }
    fn run(
        &self,
        engine_state: &EngineState,
        stack: &mut Stack,
        call: &Call,
        input: PipelineData,
    ) -> Result<PipelineData, ShellError> {
        let head = call.head;
        let ctrlc = engine_state.ctrlc.clone();
        let skip_empty = call.has_flag(engine_state, stack, "skip-empty")?;

        let span = input.span().unwrap_or(call.head);
        match input {
            PipelineData::Value(value, ..) => match value {
                Value::String { val, .. } => {
                    let lines = if skip_empty {
                        val.lines()
                            .filter_map(|s| {
                                if s.trim().is_empty() {
                                    None
                                } else {
                                    Some(Value::string(s, span))
                                }
                            })
                            .collect()
                    } else {
                        val.lines().map(|s| Value::string(s, span)).collect()
                    };

                    Ok(Value::list(lines, span).into_pipeline_data())
                }
                // Propagate existing errors
                Value::Error { error, .. } => Err(*error),
                value => Err(ShellError::OnlySupportsThisInputType {
                    exp_input_type: "string or byte stream".into(),
                    wrong_type: value.get_type().to_string(),
                    dst_span: head,
                    src_span: value.span(),
                }),
            },
            PipelineData::Empty => Ok(PipelineData::Empty),
            PipelineData::ListStream(stream, metadata) => {
                let stream = stream.modify(|iter| {
                    iter.filter_map(move |value| {
                        let span = value.span();
                        if let Value::String { val, .. } = value {
                            Some(
                                val.lines()
                                    .filter_map(|s| {
                                        if skip_empty && s.trim().is_empty() {
                                            None
                                        } else {
                                            Some(Value::string(s, span))
                                        }
                                    })
                                    .collect::<Vec<_>>(),
                            )
                        } else {
                            None
                        }
                    })
                    .flatten()
                });

                Ok(PipelineData::ListStream(stream, metadata))
            }
            PipelineData::ByteStream(stream, ..) => {
                if let Some(lines) = stream.lines() {
                    Ok(lines
                        .map(move |line| match line {
                            Ok(line) => Value::string(line, head),
                            Err(err) => Value::error(err, head),
                        })
                        .into_pipeline_data(head, ctrlc))
                } else {
                    Ok(PipelineData::empty())
                }
            }
        }
    }

    fn examples(&self) -> Vec<Example> {
        vec![Example {
            description: "Split multi-line string into lines",
            example: r#"$"two\nlines" | lines"#,
            result: Some(Value::list(
                vec![Value::test_string("two"), Value::test_string("lines")],
                Span::test_data(),
            )),
        }]
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_examples() {
        use crate::test_examples;

        test_examples(Lines {})
    }
}