From 77eea9b5e14971ddec94b2afa629deec54116ef8 Mon Sep 17 00:00:00 2001 From: atamakahere Date: Wed, 27 Dec 2023 13:09:16 +0530 Subject: [PATCH 1/4] fix: directory name ending with extention are not prepended with ext icons --- src/icon.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/icon.rs b/src/icon.rs index 6fec005f2..d5752ae0e 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -45,6 +45,7 @@ impl Icons { FileType::CharDevice => &t.filetype.device_char, FileType::BlockDevice => &t.filetype.device_block, FileType::Special => &t.filetype.special, + FileType::Directory { .. } => &t.filetype.dir, _ => { if let Some(icon) = t.name.get(name.file_name().to_lowercase().as_str()) { icon @@ -55,7 +56,6 @@ impl Icons { icon } else { match file_type { - FileType::Directory { .. } => &t.filetype.dir, // If a file has no extension and is executable, show an icon. // Except for Windows, it marks everything as an executable. #[cfg(not(windows))] From 427242c1dc069a1c5893a6ecf4dcffadab2aed9c Mon Sep 17 00:00:00 2001 From: atamakahere Date: Wed, 27 Dec 2023 13:21:51 +0530 Subject: [PATCH 2/4] test: add test for directory icon consistency --- src/icon.rs | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/icon.rs b/src/icon.rs index d5752ae0e..daf0b32a2 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -77,7 +77,7 @@ mod test { use super::{IconTheme, Icons}; use crate::flags::{IconOption, IconTheme as FlagTheme, PermissionFlag}; use crate::meta::Meta; - use std::fs::File; + use std::fs::{create_dir_all, File}; use tempfile::tempdir; #[test] @@ -234,4 +234,23 @@ mod test { assert_eq!(icon_str, format!("{}{}", file_icon, icon.icon_separator)); } } + + #[test] + fn directory_icon_consistency() { + let tmp_dir = tempdir().expect("failed to create temp dir"); + + for (ext, _) in &IconTheme::get_default_icons_by_extension() { + let dir_path = tmp_dir.path().join(format!("folder.{ext}")); + create_dir_all(&dir_path).expect("failed to create file"); + let meta = Meta::from_path(&dir_path, false, false).unwrap(); + + let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); + let icon_str = icon.get(&meta.name); + + assert_eq!( + icon_str, + format!("{}{}", "\u{f115}" /*  */, icon.icon_separator) + ); + } + } } From ede2f125349ac2971b347895981796110ae74482 Mon Sep 17 00:00:00 2001 From: atamakahere Date: Wed, 27 Dec 2023 13:33:59 +0530 Subject: [PATCH 3/4] test: make test depend on ByType struct --- src/icon.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/icon.rs b/src/icon.rs index daf0b32a2..6377c28c5 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -77,6 +77,7 @@ mod test { use super::{IconTheme, Icons}; use crate::flags::{IconOption, IconTheme as FlagTheme, PermissionFlag}; use crate::meta::Meta; + use crate::theme::icon::ByType; use std::fs::{create_dir_all, File}; use tempfile::tempdir; @@ -247,10 +248,9 @@ mod test { let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); let icon_str = icon.get(&meta.name); - assert_eq!( - icon_str, - format!("{}{}", "\u{f115}" /*  */, icon.icon_separator) - ); + let by_type = ByType::default(); + + assert_eq!(icon_str, format!("{}{}", by_type.dir, icon.icon_separator)); } } } From 0bad9f3f7d2e68058094f15d49b325452283b3ce Mon Sep 17 00:00:00 2001 From: atamakahere Date: Tue, 2 Jan 2024 19:50:25 +0530 Subject: [PATCH 4/4] fix: special name exclusion in directory name consistency logic --- src/icon.rs | 76 ++++++++++++++++++++++++++++++++--------------------- 1 file changed, 46 insertions(+), 30 deletions(-) diff --git a/src/icon.rs b/src/icon.rs index 6377c28c5..42aaa9bf1 100644 --- a/src/icon.rs +++ b/src/icon.rs @@ -37,33 +37,20 @@ impl Icons { Some(t) => { // Check file types let file_type: FileType = name.file_type(); - let icon = match file_type { - FileType::SymLink { is_dir: true } => &t.filetype.symlink_dir, - FileType::SymLink { is_dir: false } => &t.filetype.symlink_file, - FileType::Socket => &t.filetype.socket, - FileType::Pipe => &t.filetype.pipe, - FileType::CharDevice => &t.filetype.device_char, - FileType::BlockDevice => &t.filetype.device_block, - FileType::Special => &t.filetype.special, - FileType::Directory { .. } => &t.filetype.dir, - _ => { - if let Some(icon) = t.name.get(name.file_name().to_lowercase().as_str()) { - icon - } else if let Some(icon) = name - .extension() - .and_then(|ext| t.extension.get(ext.to_lowercase().as_str())) - { - icon - } else { - match file_type { - // If a file has no extension and is executable, show an icon. - // Except for Windows, it marks everything as an executable. - #[cfg(not(windows))] - FileType::File { exec: true, .. } => &t.filetype.executable, - _ => &t.filetype.file, - } - } - } + let icon = match icon_scheme(t, name, file_type) { + #[cfg(not(windows))] + (_, _, FileType::File { exec: true, .. }) => &t.filetype.executable, + (_, _, FileType::BlockDevice) => &t.filetype.device_block, + (_, _, FileType::CharDevice) => &t.filetype.device_char, + (_, _, FileType::SymLink { is_dir: true }) => &t.filetype.symlink_dir, + (_, _, FileType::SymLink { is_dir: false }) => &t.filetype.symlink_file, + (_, _, FileType::Pipe) => &t.filetype.pipe, + (_, _, FileType::Socket) => &t.filetype.socket, + (_, _, FileType::Special) => &t.filetype.special, + (None, _, FileType::Directory { .. }) => &t.filetype.dir, + (Some(special_name_icon), _, _) => special_name_icon, + (None, Some(ext_icon), FileType::File { .. }) => ext_icon, + (None, None, FileType::File { .. }) => &t.filetype.file, }; format!("{}{}", icon, self.icon_separator) @@ -72,6 +59,19 @@ impl Icons { } } +fn icon_scheme<'icon>( + t: &'icon IconTheme, + name: &'icon Name, + file_type: FileType, +) -> (Option<&'icon String>, Option<&'icon String>, FileType) { + ( + t.name.get(name.file_name().to_lowercase().as_str()), + name.extension() + .and_then(|ext| t.extension.get(ext.to_lowercase().as_str())), + file_type, + ) +} + #[cfg(test)] mod test { use super::{IconTheme, Icons}; @@ -205,7 +205,7 @@ mod test { } #[test] - fn get_icon_by_name() { + fn get_icon_by_name_files() { let tmp_dir = tempdir().expect("failed to create temp dir"); for (file_name, file_icon) in &IconTheme::get_default_icons_by_name() { @@ -221,7 +221,7 @@ mod test { } #[test] - fn get_icon_by_extension() { + fn get_icon_by_extension_files() { let tmp_dir = tempdir().expect("failed to create temp dir"); for (ext, file_icon) in &IconTheme::get_default_icons_by_extension() { @@ -237,7 +237,7 @@ mod test { } #[test] - fn directory_icon_consistency() { + fn get_icon_by_extension_dir() { let tmp_dir = tempdir().expect("failed to create temp dir"); for (ext, _) in &IconTheme::get_default_icons_by_extension() { @@ -253,4 +253,20 @@ mod test { assert_eq!(icon_str, format!("{}{}", by_type.dir, icon.icon_separator)); } } + + #[test] + fn get_icon_by_name_dir() { + let tmp_dir = tempdir().expect("failed to create temp dir"); + + for (dir_name, dir_icon) in &IconTheme::get_default_icons_by_name() { + let dir_path = tmp_dir.path().join(dir_name); + create_dir_all(&dir_path).expect("failed to create file"); + let meta = Meta::from_path(&dir_path, false, false).unwrap(); + + let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string()); + let icon_str = icon.get(&meta.name); + + assert_eq!(icon_str, format!("{}{}", dir_icon, icon.icon_separator)); + } + } }