Add max_entries config to plugins (#19)

This commit is contained in:
Kangwook Lee (이강욱)
2023-05-01 21:16:11 +09:00
committed by GitHub
parent 90ecba8261
commit 0aabb332c9
10 changed files with 144 additions and 70 deletions

View File

@@ -5,16 +5,32 @@ use scrubber::DesktopEntry;
use serde::Deserialize;
use std::{fs, process::Command};
#[derive(Deserialize, Default)]
#[derive(Deserialize)]
pub struct Config {
desktop_actions: bool,
max_entries: usize,
}
impl Default for Config {
fn default() -> Self {
Self {
desktop_actions: false,
max_entries: 5,
}
}
}
pub struct State {
config: Config,
entries: Vec<(DesktopEntry, u64)>,
}
mod scrubber;
#[handler]
pub fn handler(selection: Match, entries: &Vec<(DesktopEntry, u64)>) -> HandleResult {
let entry = entries
pub fn handler(selection: Match, state: &State) -> HandleResult {
let entry = state
.entries
.iter()
.find_map(|(entry, id)| {
if *id == selection.id.unwrap() {
@@ -33,7 +49,7 @@ pub fn handler(selection: Match, entries: &Vec<(DesktopEntry, u64)>) -> HandleRe
}
#[init]
pub fn init(config_dir: RString) -> Vec<(DesktopEntry, u64)> {
pub fn init(config_dir: RString) -> State {
let config: Config = match fs::read_to_string(format!("{}/applications.ron", config_dir)) {
Ok(content) => ron::from_str(&content).unwrap_or_else(|why| {
eprintln!("Error parsing applications plugin config: {}", why);
@@ -45,24 +61,26 @@ pub fn init(config_dir: RString) -> Vec<(DesktopEntry, u64)> {
}
};
scrubber::scrubber(config).unwrap_or_else(|why| {
let entries = scrubber::scrubber(&config).unwrap_or_else(|why| {
eprintln!("Failed to load desktop entries: {}", why);
Vec::new()
})
});
State { config, entries }
}
#[get_matches]
pub fn get_matches(input: RString, entries: &Vec<(DesktopEntry, u64)>) -> RVec<Match> {
pub fn get_matches(input: RString, state: &State) -> RVec<Match> {
let matcher = fuzzy_matcher::skim::SkimMatcherV2::default().smart_case();
let mut entries = entries
.clone()
.into_iter()
let mut entries = state
.entries
.iter()
.filter_map(|(entry, id)| {
let score = matcher.fuzzy_match(&entry.name, &input).unwrap_or(0)
+ matcher.fuzzy_match(&entry.exec, &input).unwrap_or(0);
if score > 0 {
Some((entry, id, score))
Some((entry, *id, score))
} else {
None
}
@@ -71,14 +89,14 @@ pub fn get_matches(input: RString, entries: &Vec<(DesktopEntry, u64)>) -> RVec<M
entries.sort_by(|a, b| b.2.cmp(&a.2));
entries.truncate(5);
entries.truncate(state.config.max_entries);
entries
.into_iter()
.map(|(entry, id, _)| Match {
title: entry.name.into(),
description: entry.desc.map(|desc| desc.into()).into(),
title: entry.name.clone().into(),
description: entry.desc.clone().map(|desc| desc.into()).into(),
use_pango: false,
icon: ROption::RSome(entry.icon.into()),
icon: ROption::RSome(entry.icon.clone().into()),
id: ROption::RSome(id),
})
.collect()