Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add locale support for applications #171

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions plugins/applications/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,12 @@ pub fn handler(selection: Match, state: &State) -> HandleResult {
Command::new("sh")
.arg("-c")
.arg(&entry.exec)
.current_dir(if let Some(path) = &entry.path {
if path.exists() { path } else { current_dir }
} else {
current_dir
.current_dir(match &entry.path {
Some(path) if path.exists() => path,
_ => current_dir,
})
.spawn()
}
{
} {
eprintln!("Error running desktop entry: {}", why);
}

Expand Down Expand Up @@ -113,24 +111,36 @@ pub fn get_matches(input: RString, state: &State) -> RVec<Match> {
.entries
.iter()
.filter_map(|(entry, id)| {
let app_score = match &entry.desc {
None => matcher.fuzzy_match(&entry.name, &input).unwrap_or(0),
Some(val) => matcher
.fuzzy_match(&format!("{} {}", &val, &entry.name).to_string(), &input)
.unwrap_or(0),
};

let keyword_score = entry
.keywords
.iter()
// Can be replaced by `Iterator::intersperse` once the API becomes stable.
macro_rules! prefix_sep {
($i:expr) => {
$i.as_deref()
.map(|s| [" ", s].into_iter())
.into_iter()
.flatten()
};
}

let app_names = ([&*entry.name].into_iter())
.chain(prefix_sep!(entry.display_name))
.chain(prefix_sep!(entry.desc))
.collect::<String>();

let app_score = matcher.fuzzy_match(&app_names, &input).unwrap_or(0);

let keyword_score = (entry.keywords.iter())
.chain(entry.display_keywords.iter().flat_map(|k| k.iter()))
.map(|keyword| matcher.fuzzy_match(keyword, &input).unwrap_or(0))
.sum::<i64>();

let mut score = (app_score * 25 + keyword_score) - entry.offset;

// prioritize actions
if entry.display_name.is_some() {
score *= 2;
}
if entry.desc.is_some() {
score = score * 2;
score *= 2;
}

if score > 0 {
Expand All @@ -147,7 +157,9 @@ pub fn get_matches(input: RString, state: &State) -> RVec<Match> {
entries
.into_iter()
.map(|(entry, id, _)| Match {
title: entry.name.clone().into(),
title: (entry.display_name.clone())
.unwrap_or_else(|| entry.name.clone())
.into(),
description: entry.desc.clone().map(|desc| desc.into()).into(),
use_pango: false,
icon: ROption::RSome(entry.icon.clone().into()),
Expand Down
69 changes: 66 additions & 3 deletions plugins/applications/src/scrubber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ pub struct DesktopEntry {
pub exec: String,
pub path: Option<PathBuf>,
pub name: String,
pub display_name: Option<String>,
pub keywords: Vec<String>,
pub display_keywords: Option<Vec<String>>,
pub desc: Option<String>,
pub icon: String,
pub term: bool,
Expand All @@ -19,7 +21,20 @@ const FIELD_CODE_LIST: &[&str] = &[
];

impl DesktopEntry {
fn from_dir_entry(entry: &fs::DirEntry, config: &Config) -> Vec<Self> {
fn display_keys<'a>(
key: &'a str,
lang_choices: &'a [&str],
) -> impl Iterator<Item = String> + 'a {
lang_choices
.iter()
.map(move |lang| format!("{key}[{lang}]"))
}

fn from_dir_entry(
entry: &fs::DirEntry,
config: &Config,
lang_choices: Option<&[&str]>,
) -> Vec<Self> {
if entry.path().extension() == Some(OsStr::new("desktop")) {
let content = match fs::read_to_string(entry.path()) {
Ok(content) => content,
Expand Down Expand Up @@ -78,6 +93,11 @@ impl DesktopEntry {
},
path: map.get("Path").map(PathBuf::from),
name: map.get("Name")?.to_string(),
display_name: lang_choices.and_then(|lang_choices| {
Self::display_keys("Name", lang_choices)
.find_map(|key| map.get(&*key))
.map(ToString::to_string)
}),
keywords: map
.get("Keywords")
.map(|keywords| {
Expand All @@ -87,6 +107,16 @@ impl DesktopEntry {
.collect::<Vec<_>>()
})
.unwrap_or_default(),
display_keywords: lang_choices.and_then(|lang_choices| {
Self::display_keys("Keywords", lang_choices)
.find_map(|key| map.get(&*key))
.map(|keywords| {
keywords
.split(';')
.map(|s| s.to_owned())
.collect::<Vec<_>>()
})
}),
desc: None,
icon: map
.get("Icon")
Expand Down Expand Up @@ -137,6 +167,11 @@ impl DesktopEntry {
Some(name) => name.to_string(),
None => continue,
},
display_name: lang_choices.and_then(|lang_choices| {
Self::display_keys("Name", lang_choices)
.find_map(|key| map.get(&*key))
.map(ToString::to_string)
}),
keywords: map
.get("Keywords")
.map(|keywords| {
Expand All @@ -146,6 +181,16 @@ impl DesktopEntry {
.collect::<Vec<_>>()
})
.unwrap_or_default(),
display_keywords: lang_choices.and_then(|lang_choices| {
Self::display_keys("Keywords", lang_choices)
.find_map(|key| map.get(&*key))
.map(|keywords| {
keywords
.split(';')
.map(|s| s.to_owned())
.collect::<Vec<_>>()
})
}),
desc: Some(entry.name.clone()),
icon: entry.icon.clone(),
term: map
Expand All @@ -166,6 +211,20 @@ impl DesktopEntry {
}
}

fn lang_choices(lang: &str) -> Vec<&str> {
// example: en_US.UTF-8
let whole = lang;
// example: en_US
let Some((prefix, _)) = whole.split_once('.') else {
return vec![whole];
};
// example: en
let Some((short, _)) = prefix.split_once('_') else {
return vec![whole, prefix];
};
vec![whole, prefix, short]
}

pub fn scrubber(config: &Config) -> Result<Vec<(DesktopEntry, u64)>, Box<dyn std::error::Error>> {
// Create iterator over all the files in the XDG_DATA_DIRS
// XDG compliancy is cool
Expand All @@ -181,6 +240,9 @@ pub fn scrubber(config: &Config) -> Result<Vec<(DesktopEntry, u64)>, Box<dyn std
}
};

let lang = env::var("LANG").ok();
let lang_choices = lang.as_deref().map(lang_choices);

let mut entries: HashMap<String, DesktopEntry> = match env::var("XDG_DATA_DIRS") {
Ok(data_dirs) => {
// The vec for all the DirEntry objects
Expand Down Expand Up @@ -212,7 +274,7 @@ pub fn scrubber(config: &Config) -> Result<Vec<(DesktopEntry, u64)>, Box<dyn std
Ok(entry) => entry,
Err(_why) => return None,
};
let entries = DesktopEntry::from_dir_entry(&entry, config);
let entries = DesktopEntry::from_dir_entry(&entry, config, lang_choices.as_deref());
Some(
entries
.into_iter()
Expand All @@ -232,7 +294,8 @@ pub fn scrubber(config: &Config) -> Result<Vec<(DesktopEntry, u64)>, Box<dyn std
Ok(entry) => entry,
Err(_why) => return None,
};
let entries = DesktopEntry::from_dir_entry(&entry, config);
let entries =
DesktopEntry::from_dir_entry(&entry, config, lang_choices.as_deref());
Some(
entries
.into_iter()
Expand Down