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
use crate::prelude::*;
use nu_engine::WholeStreamCommand;
use nu_errors::ShellError;
use nu_protocol::{Dictionary, Signature, SyntaxShape, UntaggedValue, Value};

pub struct Find;

impl WholeStreamCommand for Find {
    fn name(&self) -> &str {
        "find"
    }

    fn signature(&self) -> Signature {
        Signature::build("find").rest("rest", SyntaxShape::String, "search term")
    }

    fn usage(&self) -> &str {
        "Find text in the output of a previous command"
    }

    fn run(&self, args: CommandArgs) -> Result<OutputStream, ShellError> {
        find(args)
    }

    fn examples(&self) -> Vec<Example> {
        vec![
            Example {
                description: "Search pipeline output for multiple terms",
                example: r#"ls | find toml md sh"#,
                result: None,
            },
            Example {
                description: "Search strings for term(s)",
                example: r#"echo Cargo.toml | find toml"#,
                result: Some(vec![Value::from("Cargo.toml")]),
            },
            Example {
                description: "Search a number list for term(s)",
                example: r#"[1 2 3 4 5] | find 5"#,
                result: Some(vec![UntaggedValue::int(5).into()]),
            },
            Example {
                description: "Search string list for term(s)",
                example: r#"[moe larry curly] | find l"#,
                result: Some(vec![Value::from("larry"), Value::from("curly")]),
            },
        ]
    }
}

fn row_contains(row: &Dictionary, search_terms: Vec<String>) -> bool {
    for term in search_terms {
        for (k, v) in &row.entries {
            let key = k.to_string().trim().to_lowercase();
            let value = v.convert_to_string().trim().to_lowercase();
            if key.contains(&term) || value.contains(&term) {
                return true;
            }
        }
    }

    false
}

fn find(args: CommandArgs) -> Result<OutputStream, ShellError> {
    let rest: Vec<Value> = args.rest(0)?;

    Ok(args
        .input
        .filter(move |row| match &row.value {
            UntaggedValue::Row(row) => {
                let sterms: Vec<String> = rest
                    .iter()
                    .map(|t| t.convert_to_string().trim().to_lowercase())
                    .collect();
                row_contains(row, sterms)
            }
            UntaggedValue::Primitive(_p) => {
                // eprint!("prim {}", p.type_name());
                let sterms: Vec<String> = rest
                    .iter()
                    .map(|t| t.convert_to_string().trim().to_lowercase())
                    .collect();

                let prim_string = &row.convert_to_string().trim().to_lowercase();
                for term in sterms {
                    if prim_string.contains(&term) {
                        return true;
                    }
                }

                false
            }
            _ => false,
        })
        .into_output_stream())
}

#[cfg(test)]
mod tests {
    use super::Find;
    use super::ShellError;

    #[test]
    fn examples_work_as_expected() -> Result<(), ShellError> {
        use crate::examples::test as test_examples;

        test_examples(Find {})
    }
}