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
```
```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]
)
```

View File

@@ -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<Match> {
.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,