Implement imag-bookmark "find" command

Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
This commit is contained in:
Matthias Beyer 2019-12-08 14:00:31 +01:00
parent 4f2ea236af
commit 401e900936
2 changed files with 55 additions and 0 deletions

View file

@ -81,6 +81,7 @@ impl ImagApplication for ImagBookmark {
"add" => add(&rt), "add" => add(&rt),
"list" => list(&rt), "list" => list(&rt),
"remove" => remove(&rt), "remove" => remove(&rt),
"find" => find(&rt),
other => { other => {
debug!("Unknown command"); debug!("Unknown command");
if rt.handle_unknown_subcommand("imag-bookmark", other, rt.cli())?.success() { if rt.handle_unknown_subcommand("imag-bookmark", other, rt.cli())?.success() {
@ -160,3 +161,35 @@ fn remove(rt: &Runtime) -> Result<()> {
.collect() .collect()
} }
fn find(rt: &Runtime) -> Result<()> {
let substr = rt.cli().subcommand_matches("find").unwrap().value_of("substr").unwrap();
if let Some(ids) = rt.ids::<crate::ui::PathProvider>()? {
ids.into_iter()
.map(Ok)
.into_get_iter(rt.store())
} else {
rt.store()
.all_bookmarks()?
.into_get_iter()
}
.map_inner_ok_or_else(|| err_msg("Did not find one entry"))
.and_then_ok(|fle| {
if fle.is_bookmark()? {
let url = fle
.get_url()?
.ok_or_else(|| format_err!("Failed to retrieve URL for {}", fle.get_location()))?;
if url.as_str().contains(substr) {
if !rt.output_is_pipe() {
writeln!(rt.stdout(), "{}", url)?;
}
rt.report_touched(fle.get_location()).map_err(Error::from)
} else {
Ok(())
}
} else {
Ok(())
}
})
.collect()
}

View file

@ -79,6 +79,27 @@ pub fn build_ui<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
.value_name("ID") .value_name("ID")
.help("IDs of bookmarks to list")) .help("IDs of bookmarks to list"))
) )
.subcommand(SubCommand::with_name("find")
.about("Find a bookmark by substring of URL")
.version("0.1")
.arg(Arg::with_name("substr")
.index(1)
.takes_value(true)
.required(true)
.multiple(false)
.value_name("str")
.help("Substring to search in the URL."))
.arg(Arg::with_name("ids")
.index(2)
.takes_value(true)
.required(false)
.multiple(true)
.value_name("IDs")
.help("IDs to search in (if not passed, searches all bookmarks. Can also be provided via STDIN"))
)
} }
pub struct PathProvider; pub struct PathProvider;
@ -102,6 +123,7 @@ impl IdPathProvider for PathProvider {
("add", _) => no_ids_error(), ("add", _) => no_ids_error(),
("remove", Some(subm)) => get_id_paths("ids", subm), ("remove", Some(subm)) => get_id_paths("ids", subm),
("list", Some(subm)) => get_id_paths("ids", subm), ("list", Some(subm)) => get_id_paths("ids", subm),
("find", Some(subm)) => get_id_paths("ids", subm),
(other, _) => Err(format_err!("Not a known command: {}", other)), (other, _) => Err(format_err!("Not a known command: {}", other)),
} }
} }