dots/.config/awesome/quarrel/native/src/lenses/application.rs
delta f42a3a2cc9 major update of awesome config
add new icons, switch over to using stylesheets instead of gears.color.recolor_image, add a music widget to the panel, optimize services in common.lua, fix the application lense filtering and increase the update rate of services in common.lua

Signed-off-by: delta <darkussdelta@gmail.com>
2023-05-21 10:12:46 +02:00

88 lines
2.5 KiB
Rust

use std::{
fs::read_dir,
path::PathBuf,
};
use freedesktop_entry_parser as fd;
use mlua::prelude::*;
use rayon::prelude::*;
use url::Url;
use crate::lenses::entry::{
entries_to_lua_table,
Entry,
};
fn parse_entry(entry: &fd::Entry, path: &PathBuf) -> Result<Entry, ()> {
let section = entry.section("Desktop Entry");
let name = section.attr("Name").ok_or(())?.to_string();
if section.attr("Type").ok_or(())? != "Application" {
return Err(());
}
if section.attr("OnlyShowIn").is_some()
|| section.attr("Hidden").is_some()
|| section.attr("NoDisplay").is_some()
{
return Err(());
}
let exec = section.attr("Exec").ok_or(())?.to_string();
let mut new_exec = exec.clone();
for (index, _) in exec.match_indices('%') {
match exec.chars().nth(index + 1).unwrap().to_ascii_lowercase() {
'i' => {
if let Some(icon) = section.attr("Icon") {
new_exec.replace_range(index..index + 2, &format!("--icon {icon}"));
}
}
'c' => new_exec.replace_range(index..index + 2, &name),
'k' => new_exec.replace_range(index..index + 2, Url::from_file_path(path)?.as_str()),
'f' | 'u' | 'v' | 'm' | 'd' | 'n' => new_exec.replace_range(index..index + 2, ""),
_ => continue,
}
}
Ok(Entry {
message: name,
exec: Some((
new_exec,
section
.attr("Terminal")
.unwrap_or("false")
.parse()
.map_err(drop)?,
)),
provider: "Application".to_string(),
})
}
pub fn query(lua: &Lua, input: String) -> LuaResult<LuaTable> {
let applications_dir = "/usr/share/applications";
let entries = read_dir(applications_dir)?
.map(|result| result.map(|e| e.path()))
.collect::<Result<Vec<_>, std::io::Error>>()?;
let parsed_entries: Vec<Entry> = entries
.into_par_iter()
.filter(|path| matches!(path.extension(), Some(ext) if ext == "desktop"))
.filter_map(|path| {
let Ok(entry) = fd::parse_entry(&path) else {
return None
};
return parse_entry(&entry, &path).ok();
})
.collect();
Ok(entries_to_lua_table(
parsed_entries
.into_iter()
.filter(|entry| entry.message.to_lowercase().contains(&input.to_lowercase()))
.collect(),
lua,
))
}