Skip to main content

nu_command/filters/
find.rs

1use fancy_regex::{Regex, escape};
2use nu_ansi_term::Style;
3use nu_color_config::StyleComputer;
4use nu_engine::command_prelude::*;
5use nu_protocol::Config;
6
7#[derive(Clone)]
8pub struct Find;
9
10impl Command for Find {
11    fn name(&self) -> &str {
12        "find"
13    }
14
15    fn signature(&self) -> Signature {
16        Signature::build(self.name())
17            .input_output_types(vec![
18                (
19                    // TODO: This is too permissive; if we could express this
20                    // using a type parameter it would be List<T> -> List<T>.
21                    Type::List(Box::new(Type::Any)),
22                    Type::List(Box::new(Type::Any)),
23                ),
24                (Type::String, Type::Any),
25            ])
26            .named(
27                "regex",
28                SyntaxShape::String,
29                "Regex to match with.",
30                Some('r'),
31            )
32            .switch(
33                "ignore-case",
34                "Case-insensitive; when in regex mode, this is equivalent to (?i).",
35                Some('i'),
36            )
37            .switch(
38                "multiline",
39                "Don't split multi-line strings into lists of lines. you should use this option when using the (?m) or (?s) flags in regex mode.",
40                Some('m'),
41            )
42            .switch(
43                "dotall",
44                "Dotall regex mode: allow a dot . to match newlines \\n; equivalent to (?s).",
45                Some('s'),
46            )
47            .named(
48                "columns",
49                SyntaxShape::List(Box::new(SyntaxShape::String)),
50                "Column names to be searched.",
51                Some('c'),
52            )
53            .switch(
54                "no-highlight",
55                "No-highlight mode: find without marking with ansi code.",
56                Some('n'),
57            )
58            .switch("invert", "Invert the match.", Some('v'))
59            .switch(
60                "rfind",
61                "Search from the end of the string and only return the first match.",
62                Some('R'),
63            )
64            .rest("rest", SyntaxShape::Any, "Terms to search.")
65            .category(Category::Filters)
66    }
67
68    fn description(&self) -> &str {
69        "Search for terms in the input data."
70    }
71
72    fn examples(&self) -> Vec<Example<'_>> {
73        vec![
74            Example {
75                description: "Search for multiple terms in a command output.",
76                example: "ls | find toml md sh",
77                result: None,
78            },
79            Example {
80                description: "Search and highlight text for a term in a string.",
81                example: "'Cargo.toml' | find Cargo",
82                result: Some(Value::test_string(
83                    "\u{1b}[39m\u{1b}[0m\u{1b}[41;39mCargo\u{1b}[0m\u{1b}[39m.toml\u{1b}[0m"
84                        .to_owned(),
85                )),
86            },
87            Example {
88                description: "Search a number or a file size in a list of numbers.",
89                example: "[1 5 3kb 4 35 3Mb] | find 5 3kb",
90                result: Some(Value::list(
91                    vec![Value::test_int(5), Value::test_filesize(3000)],
92                    Span::test_data(),
93                )),
94            },
95            Example {
96                description: "Search a char in a list of string.",
97                example: "[moe larry curly] | find l",
98                result: Some(Value::list(
99                    vec![
100                        Value::test_string(
101                            "\u{1b}[39m\u{1b}[0m\u{1b}[41;39ml\u{1b}[0m\u{1b}[39marry\u{1b}[0m",
102                        ),
103                        Value::test_string(
104                            "\u{1b}[39mcur\u{1b}[0m\u{1b}[41;39ml\u{1b}[0m\u{1b}[39my\u{1b}[0m",
105                        ),
106                    ],
107                    Span::test_data(),
108                )),
109            },
110            Example {
111                description: "Search using regex.",
112                example: r#"[abc odb arc abf] | find --regex "b.""#,
113                result: Some(Value::list(
114                    vec![
115                        Value::test_string(
116                            "\u{1b}[39ma\u{1b}[0m\u{1b}[41;39mbc\u{1b}[0m\u{1b}[39m\u{1b}[0m"
117                                .to_string(),
118                        ),
119                        Value::test_string(
120                            "\u{1b}[39ma\u{1b}[0m\u{1b}[41;39mbf\u{1b}[0m\u{1b}[39m\u{1b}[0m"
121                                .to_string(),
122                        ),
123                    ],
124                    Span::test_data(),
125                )),
126            },
127            Example {
128                description: "Case insensitive search.",
129                example: r#"[aBc bde Arc abf] | find "ab" -i"#,
130                result: Some(Value::list(
131                    vec![
132                        Value::test_string(
133                            "\u{1b}[39m\u{1b}[0m\u{1b}[41;39maB\u{1b}[0m\u{1b}[39mc\u{1b}[0m"
134                                .to_string(),
135                        ),
136                        Value::test_string(
137                            "\u{1b}[39m\u{1b}[0m\u{1b}[41;39mab\u{1b}[0m\u{1b}[39mf\u{1b}[0m"
138                                .to_string(),
139                        ),
140                    ],
141                    Span::test_data(),
142                )),
143            },
144            Example {
145                description: "Find value in records using regex.",
146                example: r#"[[version name]; ['0.1.0' nushell] ['0.1.1' fish] ['0.2.0' zsh]] | find --regex "nu""#,
147                result: Some(Value::test_list(vec![Value::test_record(record! {
148                        "version" => Value::test_string("0.1.0"),
149                        "name" => Value::test_string("\u{1b}[39m\u{1b}[0m\u{1b}[41;39mnu\u{1b}[0m\u{1b}[39mshell\u{1b}[0m".to_string()),
150                })])),
151            },
152            Example {
153                description: "Find inverted values in records using regex.",
154                example: r#"[[version name]; ['0.1.0' nushell] ['0.1.1' fish] ['0.2.0' zsh]] | find --regex "nu" --invert"#,
155                result: Some(Value::test_list(vec![
156                    Value::test_record(record! {
157                            "version" => Value::test_string("0.1.1"),
158                            "name" => Value::test_string("fish".to_string()),
159                    }),
160                    Value::test_record(record! {
161                            "version" => Value::test_string("0.2.0"),
162                            "name" =>Value::test_string("zsh".to_string()),
163                    }),
164                ])),
165            },
166            Example {
167                description: "Find value in list using regex.",
168                example: r#"[["Larry", "Moe"], ["Victor", "Marina"]] | find --regex "rr""#,
169                result: Some(Value::list(
170                    vec![Value::list(
171                        vec![
172                            Value::test_string(
173                                "\u{1b}[39mLa\u{1b}[0m\u{1b}[41;39mrr\u{1b}[0m\u{1b}[39my\u{1b}[0m",
174                            ),
175                            Value::test_string("Moe"),
176                        ],
177                        Span::test_data(),
178                    )],
179                    Span::test_data(),
180                )),
181            },
182            Example {
183                description: "Find inverted values in records using regex.",
184                example: r#"[["Larry", "Moe"], ["Victor", "Marina"]] | find --regex "rr" --invert"#,
185                result: Some(Value::list(
186                    vec![Value::list(
187                        vec![Value::test_string("Victor"), Value::test_string("Marina")],
188                        Span::test_data(),
189                    )],
190                    Span::test_data(),
191                )),
192            },
193            Example {
194                description: "Remove ANSI sequences from result.",
195                example: "[[foo bar]; [abc 123] [def 456]] | find --no-highlight 123",
196                result: Some(Value::list(
197                    vec![Value::test_record(record! {
198                        "foo" => Value::test_string("abc"),
199                        "bar" => Value::test_int(123)
200                    })],
201                    Span::test_data(),
202                )),
203            },
204            Example {
205                description: "Find and highlight text in specific columns.",
206                example: "[[col1 col2 col3]; [moe larry curly] [larry curly moe]] | find moe --columns [col1]",
207                result: Some(Value::list(
208                    vec![Value::test_record(record! {
209                            "col1" => Value::test_string(
210                                "\u{1b}[39m\u{1b}[0m\u{1b}[41;39mmoe\u{1b}[0m\u{1b}[39m\u{1b}[0m"
211                                    .to_string(),
212                            ),
213                            "col2" => Value::test_string("larry".to_string()),
214                            "col3" => Value::test_string("curly".to_string()),
215                    })],
216                    Span::test_data(),
217                )),
218            },
219            Example {
220                description: "Find in a multi-line string.",
221                example: "'Violets are red\nAnd roses are blue\nWhen metamaterials\nAlter their hue' | find ue",
222                result: Some(Value::list(
223                    vec![
224                        Value::test_string(
225                            "\u{1b}[39mAnd roses are bl\u{1b}[0m\u{1b}[41;39mue\u{1b}[0m\u{1b}[39m\u{1b}[0m",
226                        ),
227                        Value::test_string(
228                            "\u{1b}[39mAlter their h\u{1b}[0m\u{1b}[41;39mue\u{1b}[0m\u{1b}[39m\u{1b}[0m",
229                        ),
230                    ],
231                    Span::test_data(),
232                )),
233            },
234            Example {
235                description: "Find in a multi-line string without splitting the input into a list of lines.",
236                example: "'Violets are red\nAnd roses are blue\nWhen metamaterials\nAlter their hue' | find --multiline ue",
237                result: Some(Value::test_string(
238                    "\u{1b}[39mViolets are red\nAnd roses are bl\u{1b}[0m\u{1b}[41;39mue\u{1b}[0m\u{1b}[39m\nWhen metamaterials\nAlter their h\u{1b}[0m\u{1b}[41;39mue\u{1b}[0m\u{1b}[39m\u{1b}[0m",
239                )),
240            },
241            Example {
242                description: "Find and highlight the last occurrence in a string.",
243                example: "'hello world hello' | find --rfind hello",
244                result: Some(Value::test_string(
245                    "\u{1b}[39mhello world \u{1b}[0m\u{1b}[41;39mhello\u{1b}[0m\u{1b}[39m\u{1b}[0m",
246                )),
247            },
248        ]
249    }
250
251    fn search_terms(&self) -> Vec<&str> {
252        vec!["filter", "regex", "search", "condition", "grep"]
253    }
254
255    fn run(
256        &self,
257        engine_state: &EngineState,
258        stack: &mut Stack,
259        call: &Call,
260        input: PipelineData,
261    ) -> Result<PipelineData, ShellError> {
262        let pattern = get_match_pattern_from_arguments(engine_state, stack, call)?;
263
264        let multiline = call.has_flag(engine_state, stack, "multiline")?;
265
266        let columns_to_search: Vec<_> = call
267            .get_flag(engine_state, stack, "columns")?
268            .unwrap_or_default();
269
270        let input = if multiline {
271            if let PipelineData::ByteStream(..) = input {
272                // ByteStream inputs are processed by iterating over the lines, which necessarily
273                // breaks the multi-line text being streamed into a list of lines.
274                return Err(ShellError::IncompatibleParametersSingle {
275                    msg: "Flag `--multiline` currently doesn't work for byte stream inputs. Consider using `collect`".into(),
276                    span: call.get_flag_span(stack, "multiline").expect("has flag"),
277                });
278            };
279            input
280        } else {
281            split_string_if_multiline(input, call.head)
282        };
283
284        find_in_pipelinedata(pattern, columns_to_search, engine_state, stack, input)
285    }
286}
287
288#[derive(Clone)]
289struct MatchPattern {
290    /// the regex to be used for matching in text
291    regex: Regex,
292
293    /// the list of match terms (converted to lowercase if needed), or empty if a regex was provided
294    search_terms: Vec<String>,
295
296    /// case-insensitive match
297    ignore_case: bool,
298
299    /// return a modified version of the value where matching parts are highlighted
300    highlight: bool,
301
302    /// return the values that aren't a match instead
303    invert: bool,
304
305    /// search from the end (find last occurrence)
306    rfind: bool,
307
308    /// style of the non-highlighted string sections
309    string_style: Style,
310
311    /// style of the highlighted string sections
312    highlight_style: Style,
313}
314
315fn get_match_pattern_from_arguments(
316    engine_state: &EngineState,
317    stack: &mut Stack,
318    call: &Call,
319) -> Result<MatchPattern, ShellError> {
320    let config = stack.get_config(engine_state);
321
322    let span = call.head;
323    let regex = call.get_flag::<String>(engine_state, stack, "regex")?;
324    let terms = call.rest::<Value>(engine_state, stack, 0)?;
325
326    let invert = call.has_flag(engine_state, stack, "invert")?;
327    let highlight = !call.has_flag(engine_state, stack, "no-highlight")?;
328    let rfind = call.has_flag(engine_state, stack, "rfind")?;
329
330    let ignore_case = call.has_flag(engine_state, stack, "ignore-case")?;
331
332    let dotall = call.has_flag(engine_state, stack, "dotall")?;
333
334    let style_computer = StyleComputer::from_config(engine_state, stack);
335    // Currently, search results all use the same style.
336    // Also note that this sample string is passed into user-written code (the closure that may or may not be
337    // defined for "string").
338    let string_style = style_computer.compute("string", &Value::string("search result", span));
339    let highlight_style =
340        style_computer.compute("search_result", &Value::string("search result", span));
341
342    let (regex_str, search_terms) = match (regex, terms.as_slice()) {
343        (Some(_), [_, ..]) => {
344            return Err(ShellError::IncompatibleParametersSingle {
345                msg: "Cannot use a `--regex` parameter with additional search terms".into(),
346                span: call.get_flag_span(stack, "regex").expect("has flag"),
347            });
348        }
349        (Some(regex), []) => {
350            let flags = match (ignore_case, dotall) {
351                (false, false) => "",
352                (true, false) => "(?i)", // case insensitive
353                (false, true) => "(?s)", // allow . to match \n
354                (true, true) => "(?is)", // case insensitive and allow . to match \n
355            };
356
357            (flags.to_string() + regex.as_str(), Vec::new())
358        }
359        (None, _) if dotall => {
360            return Err(ShellError::IncompatibleParametersSingle {
361                msg: "Flag --dotall only works for regex search".into(),
362                span: call.get_flag_span(stack, "dotall").expect("has flag"),
363            });
364        }
365        // NOTE: Should this be an error? It doesn't make sense to call `find` with no arguments.
366        // (None, []) => {}
367        (None, terms) => {
368            let mut regex = String::new();
369
370            if ignore_case {
371                regex += "(?i)";
372            }
373
374            let search_terms = terms
375                .iter()
376                .map(|v| {
377                    if ignore_case {
378                        v.to_expanded_string("", &config).to_lowercase()
379                    } else {
380                        v.to_expanded_string("", &config)
381                    }
382                })
383                .collect::<Vec<String>>();
384
385            if let [first, rest @ ..] = search_terms.as_slice() {
386                regex.push_str(escape(first).as_ref());
387                for term in rest {
388                    regex.push('|');
389                    regex.push_str(escape(term).as_ref());
390                }
391            }
392
393            (regex, search_terms)
394        }
395    };
396
397    let regex = engine_state.compile_regex(regex_str.as_str(), span)?;
398
399    Ok(MatchPattern {
400        regex,
401        search_terms,
402        ignore_case,
403        invert,
404        highlight,
405        rfind,
406        string_style,
407        highlight_style,
408    })
409}
410
411// map functions
412
413fn highlight_matches_in_string(pattern: &MatchPattern, val: String) -> String {
414    if !pattern.regex.is_match(&val).unwrap_or(false) {
415        return val;
416    }
417
418    let stripped_val = nu_utils::strip_ansi_string_unlikely(val);
419
420    if pattern.rfind {
421        highlight_last_match(pattern, &stripped_val)
422    } else {
423        highlight_all_matches(pattern, &stripped_val)
424    }
425}
426
427fn highlight_last_match(pattern: &MatchPattern, text: &str) -> String {
428    // Find the last match using fold to avoid collecting all matches
429    let last_match = pattern.regex.find_iter(text).fold(None, |_, m| m.ok());
430
431    match last_match {
432        Some(m) => {
433            let start = m.start();
434            let end = m.end();
435            format!(
436                "{}{}{}",
437                pattern.string_style.paint(&text[..start]),
438                pattern.highlight_style.paint(&text[start..end]),
439                pattern.string_style.paint(&text[end..])
440            )
441        }
442        None => pattern.string_style.paint(text).to_string(),
443    }
444}
445
446fn highlight_all_matches(pattern: &MatchPattern, text: &str) -> String {
447    let mut last_match_end = 0;
448    let mut highlighted = String::new();
449
450    for cap in pattern.regex.captures_iter(text) {
451        let Ok(capture) = cap else {
452            return pattern.string_style.paint(text).to_string();
453        };
454
455        let Some(m) = capture.get(0) else { continue };
456
457        highlighted.push_str(
458            &pattern
459                .string_style
460                .paint(&text[last_match_end..m.start()])
461                .to_string(),
462        );
463        highlighted.push_str(
464            &pattern
465                .highlight_style
466                .paint(&text[m.start()..m.end()])
467                .to_string(),
468        );
469        last_match_end = m.end();
470    }
471
472    highlighted.push_str(
473        &pattern
474            .string_style
475            .paint(&text[last_match_end..])
476            .to_string(),
477    );
478    highlighted
479}
480
481fn highlight_matches_in_value(
482    pattern: &MatchPattern,
483    value: Value,
484    columns_to_search: &[String],
485) -> Value {
486    if !pattern.highlight || pattern.invert {
487        return value;
488    }
489    let span = value.span();
490
491    match value {
492        Value::Record { val: record, .. } => {
493            let col_select = !columns_to_search.is_empty();
494
495            // TODO: change API to mutate in place
496            let mut record = record.into_owned();
497
498            for (col, val) in record.iter_mut() {
499                if col_select && !columns_to_search.contains(col) {
500                    continue;
501                }
502
503                *val = highlight_matches_in_value(pattern, std::mem::take(val), &[]);
504            }
505
506            Value::record(record, span)
507        }
508        Value::List { vals, .. } => vals
509            .into_iter()
510            .map(|item| highlight_matches_in_value(pattern, item, &[]))
511            .collect::<Vec<Value>>()
512            .into_value(span),
513        Value::String { val, .. } => highlight_matches_in_string(pattern, val).into_value(span),
514        _ => value,
515    }
516}
517
518fn find_in_pipelinedata(
519    pattern: MatchPattern,
520    columns_to_search: Vec<String>,
521    engine_state: &EngineState,
522    stack: &mut Stack,
523    input: PipelineData,
524) -> Result<PipelineData, ShellError> {
525    let config = stack.get_config(engine_state);
526
527    let map_pattern = pattern.clone();
528    let map_columns_to_search = columns_to_search.clone();
529
530    match input {
531        PipelineData::Empty => Ok(PipelineData::empty()),
532        PipelineData::Value(_, _) => input
533            .filter(
534                move |value| {
535                    value_should_be_printed(&pattern, value, &columns_to_search, &config)
536                        != pattern.invert
537                },
538                engine_state.signals(),
539            )?
540            .map(
541                move |x| highlight_matches_in_value(&map_pattern, x, &map_columns_to_search),
542                engine_state.signals(),
543            ),
544        PipelineData::ListStream(stream, metadata) => {
545            let stream = stream.modify(|iter| {
546                iter.filter(move |value| {
547                    value_should_be_printed(&pattern, value, &columns_to_search, &config)
548                        != pattern.invert
549                })
550                .map(move |x| highlight_matches_in_value(&map_pattern, x, &map_columns_to_search))
551            });
552
553            Ok(PipelineData::list_stream(stream, metadata))
554        }
555        PipelineData::ByteStream(stream, ..) => {
556            let span = stream.span();
557            if let Some(lines) = stream.lines() {
558                let mut output: Vec<Value> = vec![];
559                for line in lines {
560                    let line = line?;
561                    if string_should_be_printed(&pattern, &line) != pattern.invert {
562                        if pattern.highlight && !pattern.invert {
563                            output
564                                .push(highlight_matches_in_string(&pattern, line).into_value(span))
565                        } else {
566                            output.push(line.into_value(span))
567                        }
568                    }
569                }
570                Ok(Value::list(output, span).into_pipeline_data())
571            } else {
572                Ok(PipelineData::empty())
573            }
574        }
575    }
576}
577
578// filter functions
579
580fn string_should_be_printed(pattern: &MatchPattern, value: &str) -> bool {
581    pattern.regex.is_match(value).unwrap_or(false)
582}
583
584fn value_should_be_printed(
585    pattern: &MatchPattern,
586    value: &Value,
587    columns_to_search: &[String],
588    config: &Config,
589) -> bool {
590    let value_as_string = if pattern.ignore_case {
591        value.to_expanded_string("", config).to_lowercase()
592    } else {
593        value.to_expanded_string("", config)
594    };
595
596    match value {
597        Value::Bool { .. }
598        | Value::Int { .. }
599        | Value::Filesize { .. }
600        | Value::Duration { .. }
601        | Value::Date { .. }
602        | Value::Range { .. }
603        | Value::Float { .. }
604        | Value::Closure { .. }
605        | Value::Nothing { .. } => {
606            if !pattern.search_terms.is_empty() {
607                // look for exact match when searching with terms
608                pattern
609                    .search_terms
610                    .iter()
611                    .any(|term: &String| term == &value_as_string)
612            } else {
613                string_should_be_printed(pattern, &value_as_string)
614            }
615        }
616        Value::Glob { .. } | Value::CellPath { .. } | Value::Custom { .. } => {
617            string_should_be_printed(pattern, &value_as_string)
618        }
619        Value::String { val, .. } => string_should_be_printed(pattern, val),
620        Value::List { vals, .. } => vals
621            .iter()
622            .any(|item| value_should_be_printed(pattern, item, &[], config)),
623        Value::Record { val: record, .. } => {
624            let col_select = !columns_to_search.is_empty();
625            record.iter().any(|(col, val)| {
626                if col_select && !columns_to_search.contains(col) {
627                    return false;
628                }
629                value_should_be_printed(pattern, val, &[], config)
630            })
631        }
632        Value::Binary { .. } => false,
633        Value::Error { .. } => true,
634    }
635}
636
637// utility
638
639fn split_string_if_multiline(input: PipelineData, head_span: Span) -> PipelineData {
640    let span = input.span().unwrap_or(head_span);
641    match input {
642        PipelineData::Value(Value::String { ref val, .. }, metadata) if val.contains('\n') => {
643            Value::list(
644                val.lines()
645                    .map(|s| Value::string(s.to_string(), span))
646                    .collect(),
647                span,
648            )
649            .into_pipeline_data_with_metadata(metadata)
650        }
651        _ => input,
652    }
653}
654
655/// function for using find from other commands
656pub fn find_internal(
657    input: PipelineData,
658    engine_state: &EngineState,
659    stack: &mut Stack,
660    search_term: &str,
661    columns_to_search: &[&str],
662    highlight: bool,
663    head: Span,
664) -> Result<PipelineData, ShellError> {
665    let span = input.span().unwrap_or(head);
666
667    let style_computer = StyleComputer::from_config(engine_state, stack);
668    let string_style = style_computer.compute("string", &Value::string("search result", span));
669    let highlight_style =
670        style_computer.compute("search_result", &Value::string("search result", span));
671
672    let regex_str = format!("(?i){}", escape(search_term));
673
674    let regex = engine_state.compile_regex(regex_str.as_str(), head)?;
675
676    let pattern = MatchPattern {
677        regex,
678        search_terms: vec![search_term.to_lowercase()],
679        ignore_case: true,
680        highlight,
681        invert: false,
682        rfind: false,
683        string_style,
684        highlight_style,
685    };
686
687    let columns_to_search = columns_to_search
688        .iter()
689        .map(|str| String::from(*str))
690        .collect();
691
692    find_in_pipelinedata(pattern, columns_to_search, engine_state, stack, input)
693}
694
695#[cfg(test)]
696mod tests {
697    use super::*;
698
699    #[test]
700    fn test_examples() -> nu_test_support::Result {
701        nu_test_support::test().examples(Find)
702    }
703}