Refinement, plugins, etc.

This commit is contained in:
Kirottu
2022-12-31 12:51:46 +02:00
parent 990bf12b79
commit ce4b157160
17 changed files with 36037 additions and 223 deletions

View File

@@ -11,3 +11,6 @@ crate-type = ["cdylib"]
[dependencies]
anyrun-plugin = { path = "../../anyrun-plugin" }
abi_stable = "0.11.1"
ron = "0.8.0"
serde = { version = "1.0.152", features = ["derive"] }
fuzzy-matcher = "0.3.7"

28
plugins/symbols/build.rs Normal file
View File

@@ -0,0 +1,28 @@
use std::{
env,
fs::{self, File},
io::Write,
};
fn main() {
let string = fs::read_to_string("res/UnicodeData.txt").expect("Failed to load unicode data!");
let mut file = File::create(format!("{}/unicode.rs", env::var("OUT_DIR").unwrap()))
.expect("Unable to create unicode output file!");
file.write_all(b"#[allow(text_direction_codepoint_in_literal)]\nconst UNICODE_CHARS: &[(&str, &str)] = &[\n")
.unwrap();
string.lines().for_each(|line| {
let fields = line.split(';').collect::<Vec<_>>();
let chr = match char::from_u32(u32::from_str_radix(fields[0], 16).unwrap()) {
Some(char) => char,
None => return,
};
if fields[1] != "<control>" {
file.write_all(format!("(r#\"{}\"#, r#\"{}\"#),\n", fields[1], chr).as_bytes())
.unwrap();
}
});
file.write_all(b"];\n").unwrap();
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,27 +1,88 @@
use std::{collections::HashMap, fs};
use abi_stable::std_types::{ROption, RString, RVec};
use anyrun_plugin::{plugin, anyrun_interface::HandleResult, Match, PluginInfo};
use anyrun_plugin::{anyrun_interface::HandleResult, plugin, Match, PluginInfo};
use fuzzy_matcher::FuzzyMatcher;
use serde::Deserialize;
pub fn init(_config_dir: RString) {}
include!(concat!(env!("OUT_DIR"), "/unicode.rs"));
pub fn info() -> PluginInfo {
#[derive(Clone, Debug)]
struct Symbol {
chr: String,
name: String,
}
#[derive(Deserialize, Debug)]
struct Config {
symbols: HashMap<String, String>,
}
fn init(config_dir: RString) -> Vec<Symbol> {
// Try to load the config file, if it does not exist only use the static unicode characters
if let Ok(content) = fs::read_to_string(format!("{}/symbols.ron", config_dir)) {
match ron::from_str::<Config>(&content) {
Ok(config) => {
let symbols = UNICODE_CHARS
.iter()
.map(|(name, chr)| (name.to_string(), chr.to_string()))
.chain(config.symbols.into_iter())
.map(|(name, chr)| Symbol { chr, name })
.collect();
return symbols;
}
Err(why) => {
println!("Error parsing symbols config file: {}", why);
}
}
}
UNICODE_CHARS
.iter()
.map(|(name, chr)| Symbol {
chr: chr.to_string(),
name: name.to_string(),
})
.collect()
}
fn info() -> PluginInfo {
PluginInfo {
name: "Symbols".into(),
icon: "emblem-mail".into(),
icon: "accessories-character-map".into(),
}
}
pub fn get_matches(input: RString) -> RVec<Match> {
vec![Match {
title: "Test".into(),
description: ROption::RNone,
icon: "dialog-warning".into(),
id: 0,
}]
.into()
fn get_matches(input: RString, symbols: &mut Vec<Symbol>) -> RVec<Match> {
let matcher = fuzzy_matcher::skim::SkimMatcherV2::default().ignore_case();
let mut symbols = symbols
.clone()
.into_iter()
.filter_map(|symbol| {
matcher
.fuzzy_match(&symbol.name, &input)
.map(|score| (symbol, score))
})
.collect::<Vec<_>>();
// Sort the symbol list according to the score
symbols.sort_by(|a, b| b.1.cmp(&a.1));
symbols.truncate(3);
symbols
.into_iter()
.map(|(symbol, _)| Match {
title: symbol.chr.into(),
description: ROption::RSome(symbol.name.into()),
icon: ROption::RNone,
id: ROption::RNone,
})
.collect()
}
pub fn handler(selection: Match) -> HandleResult {
HandleResult::Close
fn handler(selection: Match, _symbols: &mut Vec<Symbol>) -> HandleResult {
HandleResult::Copy(selection.title.into_bytes())
}
plugin!(init, info, get_matches, handler);
plugin!(init, info, get_matches, handler, Vec<Symbol>);