Websearch: Improved search url formatting, fixed custom engines and example config
This commit is contained in:
@@ -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]
|
||||
)
|
||||
```
|
||||
|
@@ -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<Engine>,
|
||||
@@ -77,42 +75,37 @@ fn get_matches(input: RString, config: &Config) -> RVec<Match> {
|
||||
} 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())
|
||||
{
|
||||
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(),
|
||||
encode(&selection.title.to_string())
|
||||
"xdg-open https://{}",
|
||||
engine
|
||||
.value()
|
||||
.replace("{}", &encode(&selection.title.to_string()))
|
||||
))
|
||||
.spawn()
|
||||
{
|
||||
println!("Failed to perform websearch: {}", why);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
HandleResult::Close
|
||||
}
|
||||
|
Reference in New Issue
Block a user