diff --git a/Cargo.lock b/Cargo.lock index d5458f0..fa5fa57 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -128,8 +128,8 @@ name = "anyrun" version = "0.1.0" dependencies = [ "abi_stable", - "anyrun-interface", - "anyrun-macros", + "anyrun-interface 0.1.0", + "anyrun-macros 0.1.0", "clap", "gtk", "gtk-layer-shell", @@ -146,6 +146,14 @@ dependencies = [ "abi_stable", ] +[[package]] +name = "anyrun-interface" +version = "0.1.0" +source = "git+https://github.com/Kirottu/anyrun#da97c9287f01f7d85ecf5e4ab8819bde3ae0e79a" +dependencies = [ + "abi_stable", +] + [[package]] name = "anyrun-macros" version = "0.1.0" @@ -154,13 +162,32 @@ dependencies = [ "syn 2.0.15", ] +[[package]] +name = "anyrun-macros" +version = "0.1.0" +source = "git+https://github.com/Kirottu/anyrun#da97c9287f01f7d85ecf5e4ab8819bde3ae0e79a" +dependencies = [ + "quote", + "syn 2.0.15", +] + [[package]] name = "anyrun-plugin" version = "0.1.0" dependencies = [ "abi_stable", - "anyrun-interface", - "anyrun-macros", + "anyrun-interface 0.1.0", + "anyrun-macros 0.1.0", +] + +[[package]] +name = "anyrun-plugin" +version = "0.1.0" +source = "git+https://github.com/Kirottu/anyrun#da97c9287f01f7d85ecf5e4ab8819bde3ae0e79a" +dependencies = [ + "abi_stable", + "anyrun-interface 0.1.0 (git+https://github.com/Kirottu/anyrun)", + "anyrun-macros 0.1.0 (git+https://github.com/Kirottu/anyrun)", ] [[package]] @@ -168,7 +195,7 @@ name = "applications" version = "0.1.0" dependencies = [ "abi_stable", - "anyrun-plugin", + "anyrun-plugin 0.1.0", "fuzzy-matcher", "ron", "serde", @@ -533,7 +560,7 @@ name = "dictionary" version = "0.1.0" dependencies = [ "abi_stable", - "anyrun-plugin", + "anyrun-plugin 0.1.0", "reqwest", "ron", "serde", @@ -988,9 +1015,9 @@ checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" [[package]] name = "heck" -version = "0.4.0" +version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2540771e65fc8cb83cd6e8a237f70c319bd5c29f78ed1084ba5d50eeac86f7f9" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" [[package]] name = "hermit-abi" @@ -1103,7 +1130,7 @@ dependencies = [ "serde", "serde_json", "serde_repr", - "strum", + "strum 0.24.1", "tokio", ] @@ -1219,7 +1246,7 @@ name = "kidex" version = "0.1.0" dependencies = [ "abi_stable", - "anyrun-plugin", + "anyrun-plugin 0.1.0", "fuzzy-matcher", "kidex-common", "open", @@ -1653,7 +1680,7 @@ name = "randr" version = "0.1.0" dependencies = [ "abi_stable", - "anyrun-plugin", + "anyrun-plugin 0.1.0", "fuzzy-matcher", "hyprland", "ron", @@ -1763,7 +1790,7 @@ name = "rink" version = "0.1.0" dependencies = [ "abi_stable", - "anyrun-plugin", + "anyrun-plugin 0.1.0", "reqwest", "rink-core", ] @@ -1964,7 +1991,7 @@ name = "shell" version = "0.1.0" dependencies = [ "abi_stable", - "anyrun-plugin", + "anyrun-plugin 0.1.0", "ron", "serde", ] @@ -2020,7 +2047,7 @@ name = "stdin" version = "0.1.0" dependencies = [ "abi_stable", - "anyrun-plugin", + "anyrun-plugin 0.1.0", "fuzzy-matcher", "ron", "serde", @@ -2038,9 +2065,15 @@ version = "0.24.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "063e6045c0e62079840579a7e47a355ae92f60eb74daaf156fb1e84ba164e63f" dependencies = [ - "strum_macros", + "strum_macros 0.24.3", ] +[[package]] +name = "strum" +version = "0.25.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "290d54ea6f91c969195bdbcd7442c8c2a2ba87da8bf60a7ee86a235d4bc1e125" + [[package]] name = "strum_macros" version = "0.24.3" @@ -2054,6 +2087,19 @@ dependencies = [ "syn 1.0.107", ] +[[package]] +name = "strum_macros" +version = "0.25.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6069ca09d878a33f883cc06aaa9718ede171841d3832450354410b718b097232" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "rustversion", + "syn 2.0.15", +] + [[package]] name = "sublime_fuzzy" version = "0.7.0" @@ -2065,7 +2111,7 @@ name = "symbols" version = "0.1.0" dependencies = [ "abi_stable", - "anyrun-plugin", + "anyrun-plugin 0.1.0", "fuzzy-matcher", "ron", "serde", @@ -2280,7 +2326,7 @@ name = "translate" version = "0.1.0" dependencies = [ "abi_stable", - "anyrun-plugin", + "anyrun-plugin 0.1.0", "futures", "fuzzy-matcher", "reqwest", @@ -2381,6 +2427,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "urlencoding" +version = "2.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "daf8dba3b7eb870caf1ddeed7bc9d2a049f3cfdfae7cb521b087cc33ae4c49da" + [[package]] name = "utf8parse" version = "0.2.1" @@ -2575,6 +2627,19 @@ dependencies = [ "webpki", ] +[[package]] +name = "websearch" +version = "0.1.0" +dependencies = [ + "abi_stable", + "anyrun-plugin 0.1.0 (git+https://github.com/Kirottu/anyrun)", + "ron", + "serde", + "strum 0.25.0", + "strum_macros 0.25.1", + "urlencoding", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/Cargo.toml b/Cargo.toml index 2ac6b5d..a197e03 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,4 +13,5 @@ members = [ "plugins/randr", "plugins/stdin", "plugins/dictionary", -] \ No newline at end of file + "plugins/websearch", +] diff --git a/plugins/websearch/Cargo.toml b/plugins/websearch/Cargo.toml new file mode 100644 index 0000000..34bdc8a --- /dev/null +++ b/plugins/websearch/Cargo.toml @@ -0,0 +1,16 @@ +[package] +name = "websearch" +version = "0.1.0" +edition = "2021" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +anyrun-plugin = { git = "https://github.com/Kirottu/anyrun" } +abi_stable = "0.11.1" +urlencoding = "2.1.3" +ron = "0.8.0" +serde = { version = "1.0.152", features = ["derive"] } +strum = "0.25.0" +strum_macros = "0.25.1" diff --git a/plugins/websearch/src/lib.rs b/plugins/websearch/src/lib.rs new file mode 100644 index 0000000..1f5aacd --- /dev/null +++ b/plugins/websearch/src/lib.rs @@ -0,0 +1,115 @@ +use abi_stable::std_types::{ROption, RString, RVec}; +use anyrun_plugin::*; +use serde::Deserialize; +use std::{fmt, fs, process::Command}; +use strum::IntoEnumIterator; +use strum_macros::EnumIter; +use urlencoding::encode; + +#[derive(Debug, Clone, Copy, Deserialize, EnumIter)] +enum Engine { + Google, + Ecosia, + Bing, + DuckDuckGo, +} + +impl Engine { + fn value(&self) -> &str { + match *self { + Engine::Google => "google.com/search?q=", + Engine::Ecosia => "www.ecosia.org/search?q=", + Engine::Bing => "www.bing.com/search?q=", + Engine::DuckDuckGo => "duckduckgo.com/?q=", + } + } +} + +impl fmt::Display for Engine { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self { + Engine::Google => write!(f, "Google"), + Engine::Ecosia => write!(f, "Ecosia"), + Engine::Bing => write!(f, "Bing"), + Engine::DuckDuckGo => write!(f, "DuckDuckGo"), + } + } +} + +#[derive(Deserialize)] +struct Config { + prefix: String, + engines: Vec, +} + +impl Default for Config { + fn default() -> Self { + Config { + prefix: "?".to_string(), + engines: vec![Engine::Google], + } + } +} + +#[init] +fn init(config_dir: RString) -> Config { + match fs::read_to_string(format!("{}/websearch.ron", config_dir)) { + Ok(content) => ron::from_str(&content).unwrap_or_default(), + Err(_) => Config::default(), + } +} + +#[info] +fn info() -> PluginInfo { + PluginInfo { + name: "Websearch".into(), + icon: "help-about".into(), + } +} + +#[get_matches] +fn get_matches(input: RString, config: &Config) -> RVec { + if !input.starts_with(&config.prefix) { + RVec::new() + } else { + config + .engines + .clone() + .into_iter() + .map(|engine| Match { + title: input.trim_start_matches(&config.prefix).into(), + description: ROption::RSome(format!("Search with {}", engine.to_string()).into()), + use_pango: false, + icon: ROption::RNone, + id: ROption::RNone, + }) + .collect() + } +} + +#[handler] +fn handler(selection: Match) -> HandleResult { + for engine in Engine::iter() { + if selection + .description + .clone() + .unwrap() + .to_string() + .contains(&engine.to_string()) + { + if let Err(why) = Command::new("sh") + .arg("-c") + .arg(format!( + "xdg-open https://{}{}", + engine.value(), + encode(&selection.title.to_string()) + )) + .spawn() + { + println!("Failed to perform websearch: {}", why); + } + } + } + + HandleResult::Close +}