From 2548cc10d226813b08f118201d89bc9225b2aca2 Mon Sep 17 00:00:00 2001 From: nik012003 Date: Tue, 25 Jul 2023 12:27:54 +0200 Subject: [PATCH] Filter out duplicated dirs --- Cargo.lock | 10 ++++++ Cargo.toml | 3 +- examples/app_grid.rs | 1 - examples/hexagon_launcher.rs | 1 - src/xdg.rs | 60 ++++++++++++++++-------------------- 5 files changed, 39 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2528711..45215b3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1087,6 +1087,15 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "itertools" +version = "0.11.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1c173a5686ce8bfa551b3563d0c2170bf24ca44da99c7ca4bfdab5418c3fe57" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.9" @@ -1628,6 +1637,7 @@ dependencies = [ "freedesktop-icons-greedy", "glam", "image", + "itertools", "lazy_static", "linicon-theme", "manifest-dir-macros", diff --git a/Cargo.toml b/Cargo.toml index ecefd97..2dd4b80 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,6 +13,7 @@ ez-pixmap = "0.2.2" freedesktop-icons-greedy = "0.2.5" glam = { version = "0.24.0", features = ["mint"] } image = "0.24.5" +itertools = "0.11.0" lazy_static = "1.4.0" linicon-theme = "1.2.0" manifest-dir-macros = "0.1.16" @@ -33,4 +34,4 @@ ustr = "0.9.0" walkdir = "2.3.3" [dev-dependencies] -tempdir = "0.3.7" \ No newline at end of file +tempdir = "0.3.7" diff --git a/examples/app_grid.rs b/examples/app_grid.rs index 145be49..c124942 100644 --- a/examples/app_grid.rs +++ b/examples/app_grid.rs @@ -48,7 +48,6 @@ struct AppGrid { impl AppGrid { fn new(client: &Client) -> Self { let apps = get_desktop_files() - .into_iter() .filter_map(|d| parse_desktop_file(d).ok()) .filter(|d| !d.no_display) .enumerate() diff --git a/examples/hexagon_launcher.rs b/examples/hexagon_launcher.rs index b1fc023..549d7a8 100644 --- a/examples/hexagon_launcher.rs +++ b/examples/hexagon_launcher.rs @@ -98,7 +98,6 @@ impl AppHexGrid { fn new(client: &Client) -> Self { let button = Button::new(client).unwrap(); let mut desktop_files: Vec = get_desktop_files() - .into_iter() .filter_map(|d| parse_desktop_file(d).ok()) .filter(|d| !d.no_display) .collect(); diff --git a/src/xdg.rs b/src/xdg.rs index 6b964f8..93408c6 100644 --- a/src/xdg.rs +++ b/src/xdg.rs @@ -1,5 +1,6 @@ use color_eyre::eyre::Result; use freedesktop_icons_greedy::lookup; +use itertools::Itertools; use lazy_static::lazy_static; use regex::Regex; use resvg::render; @@ -70,7 +71,9 @@ fn get_data_dirs() -> Vec { .filter_map(|dir| PathBuf::from_str(dir).ok()) .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/local/share").into_iter()) // /usr/local/share .filter(|dir| dir.exists() && dir.is_dir()) + .unique() .collect() } @@ -82,11 +85,9 @@ fn get_app_dirs() -> Vec { .collect() } -pub fn get_desktop_files() -> Vec { - let desktop_extension = OsString::from_str("desktop").unwrap(); +pub fn get_desktop_files() -> impl Iterator { // Get the list of directories to search - let app_dirs = get_app_dirs(); - app_dirs + get_app_dirs() .into_iter() .flat_map(|dir| { // Follow symlinks and recursively search directories @@ -97,17 +98,14 @@ pub fn get_desktop_files() -> Vec { .filter(|entry| entry.file_type().is_file()) .map(|entry| entry.path().to_path_buf()) }) - .filter(|path| path.extension() == Some(&desktop_extension)) - .collect::>() + .filter(|path| path.extension() == Some(&OsString::from_str("desktop").unwrap())) } #[test] fn test_get_desktop_files() { - let desktop_files = get_desktop_files(); - dbg!(&desktop_files); - assert!(desktop_files - .iter() - .any(|file| file.ends_with("gimp.desktop"))); + let mut desktop_files = get_desktop_files(); + assert!(desktop_files.any(|file| file.ends_with("gimp.desktop"))); + dbg!(desktop_files.collect::>()); } pub fn parse_desktop_file(path: PathBuf) -> Result { @@ -306,29 +304,25 @@ impl Icon { } pub fn cached_process(self, size: u16) -> Result { - if !IMAGE_CACHE.lock().unwrap().map.contains_key(&( - self.path - .with_extension("") - .file_name() + let image_name = self + .path + .with_extension("") + .file_name() + .unwrap() + .to_str() + .unwrap() + .to_owned(); + + if !IMAGE_CACHE + .lock() + .unwrap() + .map + .contains_key(&(image_name.clone(), size)) + { + IMAGE_CACHE + .lock() .unwrap() - .to_str() - .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(), - ); + .insert((image_name, size), self.path.clone()); IMAGE_CACHE.lock().unwrap().save(); } match self.icon_type {