Filter out duplicated dirs

This commit is contained in:
nik012003
2023-07-25 12:27:54 +02:00
parent c679557314
commit 2548cc10d2
5 changed files with 39 additions and 36 deletions

10
Cargo.lock generated
View File

@@ -1087,6 +1087,15 @@ dependencies = [
"windows-sys", "windows-sys",
] ]
[[package]]
name = "itertools"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57"
dependencies = [
"either",
]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "1.0.9" version = "1.0.9"
@@ -1628,6 +1637,7 @@ dependencies = [
"freedesktop-icons-greedy", "freedesktop-icons-greedy",
"glam", "glam",
"image", "image",
"itertools",
"lazy_static", "lazy_static",
"linicon-theme", "linicon-theme",
"manifest-dir-macros", "manifest-dir-macros",

View File

@@ -13,6 +13,7 @@ ez-pixmap = "0.2.2"
freedesktop-icons-greedy = "0.2.5" freedesktop-icons-greedy = "0.2.5"
glam = { version = "0.24.0", features = ["mint"] } glam = { version = "0.24.0", features = ["mint"] }
image = "0.24.5" image = "0.24.5"
itertools = "0.11.0"
lazy_static = "1.4.0" lazy_static = "1.4.0"
linicon-theme = "1.2.0" linicon-theme = "1.2.0"
manifest-dir-macros = "0.1.16" manifest-dir-macros = "0.1.16"
@@ -33,4 +34,4 @@ ustr = "0.9.0"
walkdir = "2.3.3" walkdir = "2.3.3"
[dev-dependencies] [dev-dependencies]
tempdir = "0.3.7" tempdir = "0.3.7"

View File

@@ -48,7 +48,6 @@ struct AppGrid {
impl AppGrid { impl AppGrid {
fn new(client: &Client) -> Self { fn new(client: &Client) -> Self {
let apps = get_desktop_files() let apps = get_desktop_files()
.into_iter()
.filter_map(|d| parse_desktop_file(d).ok()) .filter_map(|d| parse_desktop_file(d).ok())
.filter(|d| !d.no_display) .filter(|d| !d.no_display)
.enumerate() .enumerate()

View File

@@ -98,7 +98,6 @@ impl AppHexGrid {
fn new(client: &Client) -> Self { fn new(client: &Client) -> Self {
let button = Button::new(client).unwrap(); let button = Button::new(client).unwrap();
let mut desktop_files: Vec<DesktopFile> = get_desktop_files() let mut desktop_files: Vec<DesktopFile> = get_desktop_files()
.into_iter()
.filter_map(|d| parse_desktop_file(d).ok()) .filter_map(|d| parse_desktop_file(d).ok())
.filter(|d| !d.no_display) .filter(|d| !d.no_display)
.collect(); .collect();

View File

@@ -1,5 +1,6 @@
use color_eyre::eyre::Result; use color_eyre::eyre::Result;
use freedesktop_icons_greedy::lookup; use freedesktop_icons_greedy::lookup;
use itertools::Itertools;
use lazy_static::lazy_static; use lazy_static::lazy_static;
use regex::Regex; use regex::Regex;
use resvg::render; use resvg::render;
@@ -70,7 +71,9 @@ fn get_data_dirs() -> Vec<PathBuf> {
.filter_map(|dir| PathBuf::from_str(dir).ok()) .filter_map(|dir| PathBuf::from_str(dir).ok())
.chain(dirs::home_dir().into_iter().map(|d| d.join(".local/share"))) // $HOME/.local/share .chain(dirs::home_dir().into_iter().map(|d| d.join(".local/share"))) // $HOME/.local/share
.chain(PathBuf::from_str("/usr/share").into_iter()) // /usr/share .chain(PathBuf::from_str("/usr/share").into_iter()) // /usr/share
.chain(PathBuf::from_str("/usr/local/share").into_iter()) // /usr/local/share
.filter(|dir| dir.exists() && dir.is_dir()) .filter(|dir| dir.exists() && dir.is_dir())
.unique()
.collect() .collect()
} }
@@ -82,11 +85,9 @@ fn get_app_dirs() -> Vec<PathBuf> {
.collect() .collect()
} }
pub fn get_desktop_files() -> Vec<PathBuf> { pub fn get_desktop_files() -> impl Iterator<Item = PathBuf> {
let desktop_extension = OsString::from_str("desktop").unwrap();
// Get the list of directories to search // Get the list of directories to search
let app_dirs = get_app_dirs(); get_app_dirs()
app_dirs
.into_iter() .into_iter()
.flat_map(|dir| { .flat_map(|dir| {
// Follow symlinks and recursively search directories // Follow symlinks and recursively search directories
@@ -97,17 +98,14 @@ pub fn get_desktop_files() -> Vec<PathBuf> {
.filter(|entry| entry.file_type().is_file()) .filter(|entry| entry.file_type().is_file())
.map(|entry| entry.path().to_path_buf()) .map(|entry| entry.path().to_path_buf())
}) })
.filter(|path| path.extension() == Some(&desktop_extension)) .filter(|path| path.extension() == Some(&OsString::from_str("desktop").unwrap()))
.collect::<Vec<PathBuf>>()
} }
#[test] #[test]
fn test_get_desktop_files() { fn test_get_desktop_files() {
let desktop_files = get_desktop_files(); let mut desktop_files = get_desktop_files();
dbg!(&desktop_files); assert!(desktop_files.any(|file| file.ends_with("gimp.desktop")));
assert!(desktop_files dbg!(desktop_files.collect::<Vec<PathBuf>>());
.iter()
.any(|file| file.ends_with("gimp.desktop")));
} }
pub fn parse_desktop_file(path: PathBuf) -> Result<DesktopFile, String> { pub fn parse_desktop_file(path: PathBuf) -> Result<DesktopFile, String> {
@@ -306,29 +304,25 @@ impl Icon {
} }
pub fn cached_process(self, size: u16) -> Result<Icon, std::io::Error> { pub fn cached_process(self, size: u16) -> Result<Icon, std::io::Error> {
if !IMAGE_CACHE.lock().unwrap().map.contains_key(&( let image_name = self
self.path .path
.with_extension("") .with_extension("")
.file_name() .file_name()
.unwrap()
.to_str()
.unwrap()
.to_owned();
if !IMAGE_CACHE
.lock()
.unwrap()
.map
.contains_key(&(image_name.clone(), size))
{
IMAGE_CACHE
.lock()
.unwrap() .unwrap()
.to_str() .insert((image_name, size), self.path.clone());
.unwrap()
.to_owned(),
size,
)) {
IMAGE_CACHE.lock().unwrap().insert(
(
self.path
.with_extension("")
.file_name()
.unwrap()
.to_str()
.unwrap()
.to_owned(),
size,
),
self.path.clone(),
);
IMAGE_CACHE.lock().unwrap().save(); IMAGE_CACHE.lock().unwrap().save();
} }
match self.icon_type { match self.icon_type {