Add bm-open command

This commit is contained in:
Matthias Beyer 2016-01-02 16:03:01 +01:00
parent 121bd0a8c5
commit 89101719ad
3 changed files with 72 additions and 0 deletions

View File

@ -105,6 +105,31 @@ subcommands:
required: false
takes_value: true
- open:
about: Open bookmarks
version: 0.1
author: Matthias Beyer <mail@beyermatthias.de>
args:
- id:
long: id
help: Open Bookmark with ID
required: false
takes_value: true
- match:
short: m
long: match
help: Open links matching regex
required: false
takes_value: true
- tags:
short: t
long: tags
help: Open links with these tags
required: false
takes_value: true
- remove:
about: Remove bookmark(s)
version: 0.1

View File

@ -8,6 +8,7 @@
#[macro_use] extern crate prettytable;
extern crate url;
extern crate config;
extern crate open;
pub use cli::CliConfig;
pub use configuration::Configuration;

View File

@ -127,6 +127,48 @@ impl<'a> BM<'a> {
true
}
/**
* Subcommand: open
*/
fn command_open(&self, matches: &ArgMatches) -> bool {
use open;
let parser = Parser::new(JsonHeaderParser::new(None));
let filter : Box<CliFileFilter> = get_file_filter_by_cli(&parser, matches, "id", "match", "tags", None);
let result = self.rt
.store()
.load_for_module(self, &parser)
.iter()
.filter(|file| filter.filter_file(file))
.map(|file| {
debug!("File loaded, can open now: {:?}", file);
let f = file.deref().borrow();
get_url_from_header(f.header()).map(|url| {
if open::that(&url[..]).is_ok() {
info!("open({})", url);
true
} else {
info!("could not open({})", url);
false
}
})
.unwrap_or(false)
})
.fold((0, 0), |acc, succeeded| {
let (worked, failed) = acc;
if succeeded {
(worked + 1, failed)
} else {
(worked, failed + 1)
}
});
let (succ, fail) = result;
info!("open() succeeded for {} files", succ);
info!("open() failed for {} files", fail);
return fail == 0;
}
/**
* Subcommand: remove
*/
@ -263,6 +305,10 @@ impl<'a> Module<'a> for BM<'a> {
self.command_list(matches.subcommand_matches("list").unwrap())
},
Some("open") => {
self.command_open(matches.subcommand_matches("open").unwrap())
},
Some("remove") => {
self.command_remove(matches.subcommand_matches("remove").unwrap())
},