Added custom search engine support, some code style changes

This commit is contained in:
Kirottu
2023-08-08 15:17:09 +03:00
parent 1da9daa605
commit 7679b2fafd
2 changed files with 25 additions and 13 deletions

View File

@@ -10,9 +10,18 @@ Enter your search-term and select the resulting search action you want to perfor
Default config Default config
``` ```ron
Config( Config(
prefix: "?", 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]
) )
``` ```

View File

@@ -6,21 +6,23 @@ use strum::IntoEnumIterator;
use strum_macros::EnumIter; use strum_macros::EnumIter;
use urlencoding::encode; use urlencoding::encode;
#[derive(Debug, Clone, Copy, Deserialize, EnumIter)] #[derive(Debug, Clone, Deserialize, EnumIter)]
enum Engine { enum Engine {
Google, Google,
Ecosia, Ecosia,
Bing, Bing,
DuckDuckGo, DuckDuckGo,
Custom { name: String, url: String },
} }
impl Engine { impl Engine {
fn value(&self) -> &str { fn value(&self) -> &str {
match *self { match self {
Engine::Google => "google.com/search?q=", Self::Google => "google.com/search?q=",
Engine::Ecosia => "www.ecosia.org/search?q=", Self::Ecosia => "www.ecosia.org/search?q=",
Engine::Bing => "www.bing.com/search?q=", Self::Bing => "www.bing.com/search?q=",
Engine::DuckDuckGo => "duckduckgo.com/?q=", Self::DuckDuckGo => "duckduckgo.com/?q=",
Self::Custom { url, .. } => url,
} }
} }
} }
@@ -28,10 +30,11 @@ impl Engine {
impl fmt::Display for Engine { impl fmt::Display for Engine {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
Engine::Google => write!(f, "Google"), Self::Google => write!(f, "Google"),
Engine::Ecosia => write!(f, "Ecosia"), Self::Ecosia => write!(f, "Ecosia"),
Engine::Bing => write!(f, "Bing"), Self::Bing => write!(f, "Bing"),
Engine::DuckDuckGo => write!(f, "DuckDuckGo"), Self::DuckDuckGo => write!(f, "DuckDuckGo"),
Self::Custom { name, .. } => write!(f, "{}", name),
} }
} }
} }
@@ -78,7 +81,7 @@ fn get_matches(input: RString, config: &Config) -> RVec<Match> {
.into_iter() .into_iter()
.map(|engine| Match { .map(|engine| Match {
title: input.trim_start_matches(&config.prefix).into(), 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, use_pango: false,
icon: ROption::RNone, icon: ROption::RNone,
id: ROption::RNone, id: ROption::RNone,