From fb3f373960e00c407cccf45dedf9e363a48478a1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sun, 29 Mar 2020 15:27:57 +0200 Subject: Implement piping for imag-grep Signed-off-by: Matthias Beyer --- bin/core/imag-grep/src/lib.rs | 27 +++++++++++++++++++++------ bin/core/imag-grep/src/ui.rs | 32 +++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 7 deletions(-) (limited to 'bin') diff --git a/bin/core/imag-grep/src/lib.rs b/bin/core/imag-grep/src/lib.rs index fa7bbc3a..76ea5bca 100644 --- a/bin/core/imag-grep/src/lib.rs +++ b/bin/core/imag-grep/src/lib.rs @@ -56,7 +56,7 @@ use resiter::IterInnerOkOrElse; use libimagrt::runtime::Runtime; use libimagrt::application::ImagApplication; use libimagstore::store::Entry; - +use libimagstore::iter::get::StoreIdGetIteratorExtension; mod ui; @@ -86,11 +86,26 @@ impl ImagApplication for ImagGrep { .unwrap() // ensured by clap .map_err(|e| anyhow!("Regex building error: {:?}", e))?; - let overall_count = rt - .store() - .entries()? - .into_get_iter() - .map_inner_ok_or_else(|| anyhow!("Entry from entries missing")) + let entries : Box> = if rt.ids_from_stdin() { + let iter = rt.ids::()? + .ok_or_else(|| anyhow!("No ids supplied"))? + .into_iter() + .map(Ok) + .into_get_iter(rt.store()) + .map_inner_ok_or_else(|| anyhow!("Did not find one entry")) + .inspect(|e| debug!("Editing = {:?}", e)); + + Box::new(iter) + } else { + let iter = rt.store() + .entries()? + .into_get_iter() + .map_inner_ok_or_else(|| anyhow!("Entry from entries missing")); + + Box::new(iter) + }; + + let overall_count = entries .and_then_ok(|entry| { if pattern.is_match(entry.get_content()) { debug!("Matched: {}", entry.get_location()); diff --git a/bin/core/imag-grep/src/ui.rs b/bin/core/imag-grep/src/ui.rs index 7b05172c..7c4fd2f5 100644 --- a/bin/core/imag-grep/src/ui.rs +++ b/bin/core/imag-grep/src/ui.rs @@ -17,10 +17,18 @@ // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // -use clap::{Arg, App}; +use std::path::PathBuf; + +use clap::{Arg, ArgMatches, App}; +use anyhow::Result; + +use libimagstore::storeid::IntoStoreId; +use libimagstore::storeid::StoreId; +use libimagrt::runtime::IdPathProvider; pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { app + .arg(Arg::with_name("files-with-matches") .long("files-with-matches") .short("l") @@ -44,4 +52,26 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> { .multiple(false) .value_name("PATTERN") .help("Pattern to search for. Regex is supported, multiple patterns are not.")) + + .arg(Arg::with_name("entry") + .index(2) + .takes_value(true) + .required(false) + .multiple(true) + .help("The entry/entries to grep in") + .value_name("ENTRY")) +} + +pub struct PathProvider; +impl IdPathProvider for PathProvider { + fn get_ids(matches: &ArgMatches) -> Result>> { + matches.values_of("entry") + .map(|v| v + .map(PathBuf::from) + .map(|pb| pb.into_storeid()) + .collect::>>() + ) + .transpose() + } } + -- cgit v1.2.3