Websearch: Improved search url formatting, fixed custom engines and example config

This commit is contained in:
Kirottu
2023-08-15 19:11:49 +03:00
parent 7679b2fafd
commit c0f53cabee
2 changed files with 32 additions and 39 deletions

View File

@@ -16,12 +16,12 @@ Config(
// Options: Google, Ecosia, Bing, DuckDuckGo, Custom // Options: Google, Ecosia, Bing, DuckDuckGo, Custom
// //
// Custom engines can be defined as such: // Custom engines can be defined as such:
// Custom { // Custom(
// name: "Searx" // name: "Searx",
// url: "searx.be/?q=" // 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] engines: [Google]
) )
``` ```

View File

@@ -1,12 +1,10 @@
use abi_stable::std_types::{ROption, RString, RVec}; use abi_stable::std_types::{ROption, RString, RVec};
use anyrun_plugin::*; use anyrun_plugin::*;
use serde::Deserialize; use serde::{Deserialize, Serialize};
use std::{fmt, fs, process::Command}; use std::{fmt, fs, process::Command};
use strum::IntoEnumIterator;
use strum_macros::EnumIter;
use urlencoding::encode; use urlencoding::encode;
#[derive(Debug, Clone, Deserialize, EnumIter)] #[derive(Debug, Clone, Serialize, Deserialize)]
enum Engine { enum Engine {
Google, Google,
Ecosia, Ecosia,
@@ -18,10 +16,10 @@ enum Engine {
impl Engine { impl Engine {
fn value(&self) -> &str { fn value(&self) -> &str {
match self { match self {
Self::Google => "google.com/search?q=", Self::Google => "google.com/search?q={}",
Self::Ecosia => "www.ecosia.org/search?q=", Self::Ecosia => "www.ecosia.org/search?q={}",
Self::Bing => "www.bing.com/search?q=", Self::Bing => "www.bing.com/search?q={}",
Self::DuckDuckGo => "duckduckgo.com/?q=", Self::DuckDuckGo => "duckduckgo.com/?q={}",
Self::Custom { url, .. } => url, Self::Custom { url, .. } => url,
} }
} }
@@ -39,7 +37,7 @@ impl fmt::Display for Engine {
} }
} }
#[derive(Deserialize)] #[derive(Deserialize, Debug)]
struct Config { struct Config {
prefix: String, prefix: String,
engines: Vec<Engine>, engines: Vec<Engine>,
@@ -77,41 +75,36 @@ fn get_matches(input: RString, config: &Config) -> RVec<Match> {
} else { } else {
config config
.engines .engines
.clone() .iter()
.into_iter() .enumerate()
.map(|engine| Match { .map(|(i, 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).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::RSome(i as u64),
}) })
.collect() .collect()
} }
} }
#[handler] #[handler]
fn handler(selection: Match) -> HandleResult { fn handler(selection: Match, config: &Config) -> HandleResult {
for engine in Engine::iter() { let engine = &config.engines[selection.id.unwrap() as usize];
if selection
.description println!("{}", engine.value());
.clone()
.unwrap() if let Err(why) = Command::new("sh")
.to_string() .arg("-c")
.contains(&engine.to_string()) .arg(format!(
{ "xdg-open https://{}",
if let Err(why) = Command::new("sh") engine
.arg("-c") .value()
.arg(format!( .replace("{}", &encode(&selection.title.to_string()))
"xdg-open https://{}{}", ))
engine.value(), .spawn()
encode(&selection.title.to_string()) {
)) println!("Failed to perform websearch: {}", why);
.spawn()
{
println!("Failed to perform websearch: {}", why);
}
}
} }
HandleResult::Close HandleResult::Close