From 121bd0a8c5adf53f54c757fa399c43c581c4bab1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 2 Jan 2016 15:45:01 +0100 Subject: [PATCH 1/2] Add dep: open --- Cargo.lock | 6 ++++++ Cargo.toml | 1 + 2 files changed, 7 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index be327963..ebfbeffd 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ dependencies = [ "config 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", "glob 0.2.10 (registry+https://github.com/rust-lang/crates.io-index)", "log 0.3.2 (registry+https://github.com/rust-lang/crates.io-index)", + "open 1.1.0 (registry+https://github.com/rust-lang/crates.io-index)", "prettytable-rs 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", "regex 0.1.41 (registry+https://github.com/rust-lang/crates.io-index)", "rustty 0.1.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -141,6 +142,11 @@ dependencies = [ "rustc-serialize 0.3.16 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "open" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "prettytable-rs" version = "0.4.0" diff --git a/Cargo.toml b/Cargo.toml index 1caf46e3..ceb9bda9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,4 +26,5 @@ rustty = "0.1.9" term = "0.2.12" term_grid = "0.1.2" prettytable-rs = "0.4.0" +open = "1.1.0" From 89101719ad15356c1633dd0a8c34e76bc2a57c76 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Sat, 2 Jan 2016 16:03:01 +0100 Subject: [PATCH 2/2] Add bm-open command --- etc/cli.yml | 25 ++++++++++++++++++++++++ src/main.rs | 1 + src/module/bm/mod.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 72 insertions(+) diff --git a/etc/cli.yml b/etc/cli.yml index 33bd260b..21b38e0f 100644 --- a/etc/cli.yml +++ b/etc/cli.yml @@ -105,6 +105,31 @@ subcommands: required: false takes_value: true + - open: + about: Open bookmarks + version: 0.1 + author: Matthias Beyer + 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 diff --git a/src/main.rs b/src/main.rs index 98e02ce2..504be825 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; diff --git a/src/module/bm/mod.rs b/src/module/bm/mod.rs index 8ce411ff..fc6de35f 100644 --- a/src/module/bm/mod.rs +++ b/src/module/bm/mod.rs @@ -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 = 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()) },