improv(plugins): Convert web plugin to Rust plugin, with config support
This commit is contained in:
parent
251dcd5b2c
commit
48f09be4c9
15 changed files with 324 additions and 164 deletions
111
plugins/src/web/config.ron
Normal file
111
plugins/src/web/config.ron
Normal file
|
|
@ -0,0 +1,111 @@
|
|||
(
|
||||
rules: [
|
||||
(
|
||||
matches: ["ali", "alie"],
|
||||
queries: [(name: "AliExpress", query: "aliexpress.com/wholesale?SearchText=")]
|
||||
),
|
||||
(
|
||||
matches: ["a", "amazon"],
|
||||
queries: [(name: "Amazon", query: "smile.amazon.com/s?k=" )]
|
||||
),
|
||||
(
|
||||
matches: ["arch"],
|
||||
queries: [(name: "Arch Wiki", query: "wiki.archlinux.org/index.php/" )]
|
||||
),
|
||||
(
|
||||
matches: ["bc", "bandcamp"],
|
||||
queries: [(name: "Bandcamp", query: "bandcamp.com/search?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["b", "bing"],
|
||||
queries: [(name: "Bing", query: "bing.com/search?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["crates"],
|
||||
queries: [
|
||||
(name: "Crates.io", query: "crates.io/search?q="),
|
||||
(name: "Lib.rs", query: "lib.rs/search?q="),
|
||||
]
|
||||
),
|
||||
(
|
||||
matches: ["ddg"],
|
||||
queries: [(name: "DuckDuckGo", query: "duckduckgo.com/?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["dev"],
|
||||
queries: [(name: "DEV Community", query: "dev.to/search?q=")],
|
||||
),
|
||||
(
|
||||
matches: ["fh"],
|
||||
queries: [(name: "Flathub", query: "flathub.org/apps/search/")]
|
||||
),
|
||||
(
|
||||
matches: ["gh"],
|
||||
queries: [(name: "GitHub", query: "github.com/search?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["gist"],
|
||||
queries: [(name: "GitHub Gist", query: "gist.github.com/search?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["g", "google"],
|
||||
queries: [(name: "Search", query: "google.com/search?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["gi"],
|
||||
queries: [(name: "Images", query: "google.com/images?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["gm"],
|
||||
queries: [(name: "Maps", query: "google.com/maps?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["gn"],
|
||||
queries: [(name: "News", query: "google.com/news?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["lib"],
|
||||
queries: [(name: "Libraries.io", query: "libraries.io/search?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["npm"],
|
||||
queries: [(name: "npm", query: "npmjs.com/search?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["pp"],
|
||||
queries: [(name: "Pop!_Planet", query: "pop-planet.info/forums/search/1/?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["ppw"],
|
||||
queries: [(name: "Pop!_Planet Wiki", query: "pop-planet.info/wiki/?search=")]
|
||||
),
|
||||
(
|
||||
matches: ["rdt", "reddit"],
|
||||
queries: [(name: "reddit", query: "reddit.com/search/?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["sc", "sdcl"],
|
||||
queries: [(name: "SoundCloud", query: "soundcloud.com/search?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["stack"],
|
||||
queries: [(name: "Stack Overflow", query: "stackoverflow.com/search?q=")]
|
||||
),
|
||||
(
|
||||
matches: ["twitch"],
|
||||
queries: [(name: "Twitch", query: "twitch.tv/search?term=")]
|
||||
),
|
||||
(
|
||||
matches: ["wiki"],
|
||||
queries: [(name: "Wikipedia", query: "wikipedia.org/w/index.php?search=")]
|
||||
),
|
||||
(
|
||||
matches: ["yh"],
|
||||
queries: [(name: "Yahoo!", query: "search.yahoo.com/search?p=")]
|
||||
),
|
||||
(
|
||||
matches: ["yt"],
|
||||
queries: [(name: "YouTube", query: "youtube.com/results?search_query=")]
|
||||
),
|
||||
]
|
||||
)
|
||||
71
plugins/src/web/config.rs
Normal file
71
plugins/src/web/config.rs
Normal file
|
|
@ -0,0 +1,71 @@
|
|||
use serde::Deserialize;
|
||||
use slab::Slab;
|
||||
use std::collections::HashMap;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Config {
|
||||
matches: HashMap<String, u32>,
|
||||
queries: Slab<Vec<Definition>>,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn new(rules: RawConfig) -> Self {
|
||||
let mut config = Self::default();
|
||||
|
||||
for rule in rules.rules {
|
||||
let idx = config.queries.insert(rule.queries);
|
||||
for keyword in rule.matches {
|
||||
config.matches.insert(keyword, idx as u32);
|
||||
}
|
||||
}
|
||||
|
||||
config
|
||||
}
|
||||
|
||||
pub fn get(&self, word: &str) -> Option<&[Definition]> {
|
||||
self.matches
|
||||
.get(word)
|
||||
.and_then(|idx| self.queries.get(*idx as usize))
|
||||
.map(|vec| &vec[..])
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct RawConfig {
|
||||
pub rules: Vec<Rule>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Rule {
|
||||
pub matches: Vec<String>,
|
||||
pub queries: Vec<Definition>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct Definition {
|
||||
pub name: String,
|
||||
pub query: String,
|
||||
}
|
||||
|
||||
pub fn load() -> Config {
|
||||
pop_launcher::config::find("web")
|
||||
.next()
|
||||
.and_then(|path| {
|
||||
let string = match std::fs::read_to_string(&path) {
|
||||
Ok(string) => string,
|
||||
Err(why) => {
|
||||
tracing::error!("failed to read config: {}", why);
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
match ron::from_str::<RawConfig>(&string) {
|
||||
Ok(config) => Some(Config::new(config)),
|
||||
Err(why) => {
|
||||
tracing::error!("failed to deserialize config: {}", why);
|
||||
None
|
||||
}
|
||||
}
|
||||
})
|
||||
.unwrap_or_default()
|
||||
}
|
||||
91
plugins/src/web/mod.rs
Normal file
91
plugins/src/web/mod.rs
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
mod config;
|
||||
|
||||
use self::config::{Config, Definition};
|
||||
use futures_lite::StreamExt;
|
||||
use pop_launcher::*;
|
||||
|
||||
use smol::Unblock;
|
||||
use std::io;
|
||||
|
||||
pub async fn main() {
|
||||
let mut app = App::new();
|
||||
|
||||
let mut requests = json_input_stream(async_stdin());
|
||||
|
||||
while let Some(result) = requests.next().await {
|
||||
match result {
|
||||
Ok(request) => match request {
|
||||
Request::Activate(id) => app.activate(id).await,
|
||||
Request::Search(query) => app.search(query).await,
|
||||
Request::Exit => break,
|
||||
_ => (),
|
||||
},
|
||||
Err(why) => {
|
||||
tracing::error!("malformed JSON input: {}", why)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct App {
|
||||
config: Config,
|
||||
queries: Vec<String>,
|
||||
out: Unblock<io::Stdout>,
|
||||
}
|
||||
|
||||
impl App {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
config: config::load(),
|
||||
queries: Vec::new(),
|
||||
out: async_stdout(),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn activate(&mut self, id: u32) {
|
||||
if let Some(query) = self.queries.get(id as usize) {
|
||||
eprintln!("got query: {}", query);
|
||||
crate::xdg_open(query);
|
||||
}
|
||||
|
||||
crate::send(&mut self.out, PluginResponse::Close).await;
|
||||
}
|
||||
|
||||
pub async fn search(&mut self, query: String) {
|
||||
self.queries.clear();
|
||||
if let Some(word) = query.split_ascii_whitespace().next() {
|
||||
if let Some(defs) = self.config.get(word) {
|
||||
for (id, def) in defs.iter().enumerate() {
|
||||
let (_, mut query) = query.split_at(word.len());
|
||||
query = query.trim();
|
||||
let encoded = build_query(def, query);
|
||||
|
||||
crate::send(
|
||||
&mut self.out,
|
||||
PluginResponse::Append(PluginSearchResult {
|
||||
id: id as u32,
|
||||
name: [&def.name, ": ", query].concat(),
|
||||
description: encoded.clone(),
|
||||
..Default::default()
|
||||
}),
|
||||
)
|
||||
.await;
|
||||
|
||||
self.queries.push(encoded);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
crate::send(&mut self.out, PluginResponse::Finished).await;
|
||||
}
|
||||
}
|
||||
|
||||
fn build_query(definition: &Definition, query: &str) -> String {
|
||||
let prefix = if definition.query.starts_with("https://") {
|
||||
""
|
||||
} else {
|
||||
"https://"
|
||||
};
|
||||
|
||||
[prefix, &*definition.query, &*urlencoding::encode(query)].concat()
|
||||
}
|
||||
|
|
@ -1,10 +1,7 @@
|
|||
(
|
||||
name: "Web Search",
|
||||
description: "Site-specific web search",
|
||||
query: (
|
||||
regex: "^(amazon|wiki|bing|ddg|google|yt|stack|crates|arch|pp|ppw|rdt|bc|lib|npm|gist|fh|gh|dev|sdcl|twitch|yh|alie)\\s.*",
|
||||
help: "ddg ",
|
||||
),
|
||||
bin: (path: "web.js"),
|
||||
query: (help: "ddg "),
|
||||
bin: (path: "web"),
|
||||
icon: Name("system-search"),
|
||||
)
|
||||
|
|
@ -1,128 +0,0 @@
|
|||
#!/usr/bin/env gjs
|
||||
|
||||
const { GLib, Gio } = imports.gi
|
||||
|
||||
const STDIN = new Gio.DataInputStream({ base_stream: new Gio.UnixInputStream({ fd: 0 }) })
|
||||
const STDOUT = new Gio.DataOutputStream({ base_stream: new Gio.UnixOutputStream({ fd: 1 }) })
|
||||
|
||||
const ENTRIES = new Map([
|
||||
['wiki', { query: 'https://wikipedia.org/w/index.php?search=', name: 'Wikipedia' }],
|
||||
['bing', { query: 'https://www.bing.com/search?q=', name: 'Bing' }],
|
||||
['ddg', { query: 'https://www.duckduckgo.com/?q=', name: 'DuckDuckGo' }],
|
||||
['google', { query: 'https://www.google.com/search?q=', name: 'Google' }],
|
||||
['yt', { query: 'https://www.youtube.com/results?search_query=', name: 'YouTube' }],
|
||||
['amazon', { query: 'https://smile.amazon.com/s?k=', name: 'Amazon' }],
|
||||
['stack', { query: 'https://stackoverflow.com/search?q=', name: 'Stack Overflow' }],
|
||||
['crates', { query: 'https://crates.io/search?q=', name: 'Crates.io' }],
|
||||
['rdt', { query: 'https://www.reddit.com/search/?q=', name: 'reddit' }],
|
||||
['arch', { query: 'https://wiki.archlinux.org/index.php/', name: 'Arch Wiki' }],
|
||||
['pp', { query: 'https://pop-planet.info/forums/search/1/?q=', name: 'Pop!_Planet' }],
|
||||
['ppw', { query: 'https://pop-planet.info/wiki/?search=', name: 'Pop!_Planet Wiki' }],
|
||||
['bc', { query: 'https://bandcamp.com/search?q=', name: 'Bandcamp' }],
|
||||
['npm', { query: 'https://www.npmjs.com/search?q=', name: 'npm' }],
|
||||
['lib', { query: 'https://libraries.io/search?q=', name: 'Libraries.io' }],
|
||||
['gist', { query: 'https://gist.github.com/search?q=', name: 'GitHub Gist' }],
|
||||
['fh', { query: 'https://flathub.org/apps/search/', name: 'Flathub' }],
|
||||
['gh', { query: 'https://github.com/search?q=', name: 'GitHub' }],
|
||||
['sdcl', { query: 'https://soundcloud.com/search?q=', name: 'SoundCloud' }],
|
||||
['twitch', { query: 'https://www.twitch.tv/search?term=', name: 'Twitch' }],
|
||||
['yh', { query: 'https://search.yahoo.com/search?p=', name: 'Yahoo!' }],
|
||||
['alie', { query: 'https://www.aliexpress.com/wholesale?SearchText=', name: 'AliExpress' }],
|
||||
['dev', { query: 'https://dev.to/search?q=', name: 'DEV Community' }]
|
||||
])
|
||||
|
||||
class App {
|
||||
constructor() {
|
||||
this.last_query = ''
|
||||
this.last_value = ''
|
||||
this.query_base = ''
|
||||
this.name_base = ''
|
||||
this.app_info = Gio.AppInfo.get_default_for_uri_scheme('https')
|
||||
}
|
||||
|
||||
build_query() {
|
||||
return `${this.query_base}${encodeURIComponent(this.last_query)}`
|
||||
}
|
||||
|
||||
search(input) {
|
||||
const delim_position = input.indexOf(' ')
|
||||
const key = input.substring(0, delim_position)
|
||||
this.last_query = input.substr(delim_position + 1).trim()
|
||||
|
||||
const entry = ENTRIES.get(key) || { query: 'https://www.duckduckgo.com/?q=', name: 'DuckDuckGo' }
|
||||
this.query_base = entry.query;
|
||||
this.name_base = entry.name;
|
||||
|
||||
this.send({ "Append": {
|
||||
id: 0,
|
||||
description: this.build_query(),
|
||||
name: `${this.name_base}: ${this.last_query}`,
|
||||
icon: { Name: this.app_info.get_icon().to_string() },
|
||||
}})
|
||||
|
||||
this.send("Finished")
|
||||
}
|
||||
|
||||
submit(_id) {
|
||||
try {
|
||||
GLib.spawn_command_line_async(`xdg-open ${this.build_query()}`)
|
||||
} catch (e) {
|
||||
log(`xdg-open failed: ${e} `)
|
||||
}
|
||||
|
||||
this.send("Close")
|
||||
}
|
||||
|
||||
send(object) {
|
||||
STDOUT.write_bytes(new GLib.Bytes(JSON.stringify(object) + "\n"), null)
|
||||
}
|
||||
}
|
||||
|
||||
function main() {
|
||||
/** @type {null | ByteArray} */
|
||||
let input_array
|
||||
|
||||
/** @type {string} */
|
||||
let input_str
|
||||
|
||||
/** @type {null | LauncherRequest} */
|
||||
let event
|
||||
|
||||
let app = new App()
|
||||
|
||||
mainloop:
|
||||
while (true) {
|
||||
try {
|
||||
[input_array,] = STDIN.read_line(null)
|
||||
} catch (e) {
|
||||
break
|
||||
}
|
||||
|
||||
input_str = imports.byteArray.toString(input_array)
|
||||
if ((event = parse_event(input_str)) !== null) {
|
||||
if ("Search" in event) {
|
||||
app.search(event.Search)
|
||||
} else if ("Activate" in event) {
|
||||
app.submit(event.Activate)
|
||||
} else if ("Exit" === event) {
|
||||
break mainloop
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parses an IPC event received from STDIN
|
||||
* @param {string} input
|
||||
* @returns {null | LauncherRequest}
|
||||
*/
|
||||
function parse_event(input) {
|
||||
try {
|
||||
return JSON.parse(input)
|
||||
} catch (e) {
|
||||
log(`Input not valid JSON`)
|
||||
return null
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
Loading…
Add table
Add a link
Reference in a new issue