summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <mail@beyermatthias.de>2020-03-29 15:27:57 +0200
committerMatthias Beyer <mail@beyermatthias.de>2020-03-29 17:32:44 +0200
commitfb3f373960e00c407cccf45dedf9e363a48478a1 (patch)
tree02b21f49188cba4c481521767a751c24d2b6d3f8
parent8a9ee7a63f2b34f0d9d19c19598c75213114cf80 (diff)
Implement piping for imag-grep
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
-rw-r--r--bin/core/imag-grep/src/lib.rs27
-rw-r--r--bin/core/imag-grep/src/ui.rs32
2 files changed, 52 insertions, 7 deletions
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<dyn Iterator<Item = _ >> = if rt.ids_from_stdin() {
+ let iter = rt.ids::<crate::ui::PathProvider>()?
+ .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<Option<Vec<StoreId>>> {
+ matches.values_of("entry")
+ .map(|v| v
+ .map(PathBuf::from)
+ .map(|pb| pb.into_storeid())
+ .collect::<Result<Vec<_>>>()
+ )
+ .transpose()
+ }
}
+