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
|
// 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]
|
||||||
)
|
)
|
||||||
```
|
```
|
||||||
|
@@ -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,42 +75,37 @@ 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()
|
|
||||||
.to_string()
|
|
||||||
.contains(&engine.to_string())
|
|
||||||
{
|
|
||||||
if let Err(why) = Command::new("sh")
|
if let Err(why) = Command::new("sh")
|
||||||
.arg("-c")
|
.arg("-c")
|
||||||
.arg(format!(
|
.arg(format!(
|
||||||
"xdg-open https://{}{}",
|
"xdg-open https://{}",
|
||||||
engine.value(),
|
engine
|
||||||
encode(&selection.title.to_string())
|
.value()
|
||||||
|
.replace("{}", &encode(&selection.title.to_string()))
|
||||||
))
|
))
|
||||||
.spawn()
|
.spawn()
|
||||||
{
|
{
|
||||||
println!("Failed to perform websearch: {}", why);
|
println!("Failed to perform websearch: {}", why);
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
HandleResult::Close
|
HandleResult::Close
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user