From 7679b2fafdb2eab202a76aab9b563e16c50b7b06 Mon Sep 17 00:00:00 2001 From: Kirottu Date: Tue, 8 Aug 2023 15:17:09 +0300 Subject: [PATCH] Added custom search engine support, some code style changes --- plugins/websearch/README.md | 13 +++++++++++-- plugins/websearch/src/lib.rs | 25 ++++++++++++++----------- 2 files changed, 25 insertions(+), 13 deletions(-) diff --git a/plugins/websearch/README.md b/plugins/websearch/README.md index ca5d558..b025761 100644 --- a/plugins/websearch/README.md +++ b/plugins/websearch/README.md @@ -10,9 +10,18 @@ Enter your search-term and select the resulting search action you want to perfor Default config -``` +```ron Config( prefix: "?", - engines: [Google] // options: Google, Ecosia, Bing, DuckDuckGo + // Options: Google, Ecosia, Bing, DuckDuckGo, Custom + // + // Custom engines can be defined as such: + // Custom { + // name: "Searx" + // url: "searx.be/?q=" + // } + // + // NOTE: The search query is appended after the URL, and `https://` is automatically added in front. + engines: [Google] ) ``` diff --git a/plugins/websearch/src/lib.rs b/plugins/websearch/src/lib.rs index 1f5aacd..4eb60ac 100644 --- a/plugins/websearch/src/lib.rs +++ b/plugins/websearch/src/lib.rs @@ -6,21 +6,23 @@ use strum::IntoEnumIterator; use strum_macros::EnumIter; use urlencoding::encode; -#[derive(Debug, Clone, Copy, Deserialize, EnumIter)] +#[derive(Debug, Clone, Deserialize, EnumIter)] enum Engine { Google, Ecosia, Bing, DuckDuckGo, + Custom { name: String, url: String }, } 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=", + 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::Custom { url, .. } => url, } } } @@ -28,10 +30,11 @@ impl Engine { 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"), + Self::Google => write!(f, "Google"), + Self::Ecosia => write!(f, "Ecosia"), + Self::Bing => write!(f, "Bing"), + Self::DuckDuckGo => write!(f, "DuckDuckGo"), + Self::Custom { name, .. } => write!(f, "{}", name), } } } @@ -78,7 +81,7 @@ fn get_matches(input: RString, config: &Config) -> RVec { .into_iter() .map(|engine| Match { title: input.trim_start_matches(&config.prefix).into(), - description: ROption::RSome(format!("Search with {}", engine.to_string()).into()), + description: ROption::RSome(format!("Search with {}", engine).into()), use_pango: false, icon: ROption::RNone, id: ROption::RNone,