libimaggrepcmd/
lib.rs

1//
2// imag - the personal information management suite for the commandline
3// Copyright (C) 2015-2020 Matthias Beyer <mail@beyermatthias.de> and contributors
4//
5// This library is free software; you can redistribute it and/or
6// modify it under the terms of the GNU Lesser General Public
7// License as published by the Free Software Foundation; version
8// 2.1 of the License.
9//
10// This library is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13// Lesser General Public License for more details.
14//
15// You should have received a copy of the GNU Lesser General Public
16// License along with this library; if not, write to the Free Software
17// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18//
19
20#![forbid(unsafe_code)]
21
22#![deny(
23    non_camel_case_types,
24    non_snake_case,
25    path_statements,
26    trivial_numeric_casts,
27    unstable_features,
28    unused_allocation,
29    unused_import_braces,
30    unused_imports,
31    unused_must_use,
32    unused_mut,
33    unused_qualifications,
34    while_true,
35)]
36
37#[macro_use] extern crate log;
38#[macro_use] extern crate failure;
39extern crate clap;
40extern crate regex;
41extern crate resiter;
42
43extern crate libimagstore;
44extern crate libimagrt;
45extern crate libimagerror;
46
47use std::io::Write;
48
49use regex::Regex;
50use clap::App;
51use failure::Fallible as Result;
52use failure::err_msg;
53use resiter::AndThen;
54use resiter::IterInnerOkOrElse;
55
56use libimagrt::runtime::Runtime;
57use libimagrt::application::ImagApplication;
58use libimagstore::store::Entry;
59
60
61mod ui;
62
63struct Options {
64    files_with_matches: bool,
65    count: bool,
66}
67
68/// Marker enum for implementing ImagApplication on
69///
70/// This is used by binaries crates to execute business logic
71/// or to build a CLI completion.
72pub enum ImagGrep {}
73impl ImagApplication for ImagGrep {
74    fn run(rt: Runtime) -> Result<()> {
75        let opts = Options {
76            files_with_matches    : rt.cli().is_present("files-with-matches"),
77            count                 : rt.cli().is_present("count"),
78        };
79
80        let mut count : usize = 0;
81
82        let pattern = rt
83            .cli()
84            .value_of("pattern")
85            .map(Regex::new)
86            .unwrap() // ensured by clap
87            .map_err(|e| format_err!("Regex building error: {:?}", e))?;
88
89        let overall_count = rt
90            .store()
91            .entries()?
92            .into_get_iter()
93            .map_inner_ok_or_else(|| err_msg("Entry from entries missing"))
94            .and_then_ok(|entry| {
95                if pattern.is_match(entry.get_content()) {
96                    debug!("Matched: {}", entry.get_location());
97                    rt.report_touched(entry.get_location())?;
98                    show(&rt, &entry, &pattern, &opts, &mut count)
99                } else {
100                    debug!("Not matched: {}", entry.get_location());
101                    Ok(())
102                }
103            })
104            .collect::<Result<Vec<_>>>()?
105            .len();
106
107        if opts.count {
108            writeln!(rt.stdout(), "{}", count)?;
109        } else if !opts.files_with_matches {
110            writeln!(rt.stdout(), "Processed {} files, {} matches, {} nonmatches",
111                     overall_count,
112                     count,
113                     overall_count - count)?;
114        }
115
116        Ok(())
117    }
118
119    fn build_cli<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
120        ui::build_ui(app)
121    }
122
123    fn name() -> &'static str {
124        env!("CARGO_PKG_NAME")
125    }
126
127    fn description() -> &'static str {
128        "grep through entries text"
129    }
130
131    fn version() -> &'static str {
132        env!("CARGO_PKG_VERSION")
133    }
134
135}
136
137fn show(rt: &Runtime, e: &Entry, re: &Regex, opts: &Options, count: &mut usize) -> Result<()> {
138    if opts.files_with_matches {
139        writeln!(rt.stdout(), "{}", e.get_location())?;
140    } else if opts.count {
141        *count += 1;
142    } else {
143        writeln!(rt.stdout(), "{}:", e.get_location())?;
144        for capture in re.captures_iter(e.get_content()) {
145            for mtch in capture.iter() {
146                if let Some(m) = mtch {
147                    writeln!(rt.stdout(), " '{}'", m.as_str())?;
148                }
149            }
150        }
151
152        writeln!(rt.stdout())?;
153        *count += 1;
154    }
155
156    rt.report_touched(e.get_location())
157}
158