From c0f53cabee2c885c5df2fa7d9137620a8a23b699 Mon Sep 17 00:00:00 2001 From: Kirottu Date: Tue, 15 Aug 2023 19:11:49 +0300 Subject: [PATCH] Websearch: Improved search url formatting, fixed custom engines and example config --- plugins/websearch/README.md | 10 +++--- plugins/websearch/src/lib.rs | 61 ++++++++++++++++-------------------- 2 files changed, 32 insertions(+), 39 deletions(-) diff --git a/plugins/websearch/README.md b/plugins/websearch/README.md index b025761..2b849c2 100644 --- a/plugins/websearch/README.md +++ b/plugins/websearch/README.md @@ -16,12 +16,12 @@ Config( // Options: Google, Ecosia, Bing, DuckDuckGo, Custom // // Custom engines can be defined as such: - // Custom { - // name: "Searx" - // url: "searx.be/?q=" - // } + // Custom( + // name: "Searx", + // url: "searx.be/?q={}", + // ) // - // NOTE: The search query is appended after the URL, and `https://` is automatically added in front. + // NOTE: `{}` is replaced by the search query and `https://` is automatically added in front. engines: [Google] ) ``` diff --git a/plugins/websearch/src/lib.rs b/plugins/websearch/src/lib.rs index 4eb60ac..a7812c6 100644 --- a/plugins/websearch/src/lib.rs +++ b/plugins/websearch/src/lib.rs @@ -1,12 +1,10 @@ use abi_stable::std_types::{ROption, RString, RVec}; use anyrun_plugin::*; -use serde::Deserialize; +use serde::{Deserialize, Serialize}; use std::{fmt, fs, process::Command}; -use strum::IntoEnumIterator; -use strum_macros::EnumIter; use urlencoding::encode; -#[derive(Debug, Clone, Deserialize, EnumIter)] +#[derive(Debug, Clone, Serialize, Deserialize)] enum Engine { Google, Ecosia, @@ -18,10 +16,10 @@ enum Engine { impl Engine { fn value(&self) -> &str { match self { - Self::Google => "google.com/search?q=", - Self::Ecosia => "www.ecosia.org/search?q=", - Self::Bing => "www.bing.com/search?q=", - Self::DuckDuckGo => "duckduckgo.com/?q=", + Self::Google => "google.com/search?q={}", + Self::Ecosia => "www.ecosia.org/search?q={}", + Self::Bing => "www.bing.com/search?q={}", + Self::DuckDuckGo => "duckduckgo.com/?q={}", Self::Custom { url, .. } => url, } } @@ -39,7 +37,7 @@ impl fmt::Display for Engine { } } -#[derive(Deserialize)] +#[derive(Deserialize, Debug)] struct Config { prefix: String, engines: Vec, @@ -77,41 +75,36 @@ fn get_matches(input: RString, config: &Config) -> RVec { } else { config .engines - .clone() - .into_iter() - .map(|engine| Match { + .iter() + .enumerate() + .map(|(i, engine)| Match { title: input.trim_start_matches(&config.prefix).into(), description: ROption::RSome(format!("Search with {}", engine).into()), use_pango: false, icon: ROption::RNone, - id: ROption::RNone, + id: ROption::RSome(i as u64), }) .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); - } - } +fn handler(selection: Match, config: &Config) -> HandleResult { + let engine = &config.engines[selection.id.unwrap() as usize]; + + println!("{}", engine.value()); + + if let Err(why) = Command::new("sh") + .arg("-c") + .arg(format!( + "xdg-open https://{}", + engine + .value() + .replace("{}", &encode(&selection.title.to_string())) + )) + .spawn() + { + println!("Failed to perform websearch: {}", why); } HandleResult::Close