Skip to content

Commit 106ba28

Browse files
fix: special name exclusion in directory name consistency logic
1 parent f4041c4 commit 106ba28

File tree

1 file changed

+46
-30
lines changed

1 file changed

+46
-30
lines changed

src/icon.rs

+46-30
Original file line numberDiff line numberDiff line change
@@ -37,33 +37,20 @@ impl Icons {
3737
Some(t) => {
3838
// Check file types
3939
let file_type: FileType = name.file_type();
40-
let icon = match file_type {
41-
FileType::SymLink { is_dir: true } => &t.filetype.symlink_dir,
42-
FileType::SymLink { is_dir: false } => &t.filetype.symlink_file,
43-
FileType::Socket => &t.filetype.socket,
44-
FileType::Pipe => &t.filetype.pipe,
45-
FileType::CharDevice => &t.filetype.device_char,
46-
FileType::BlockDevice => &t.filetype.device_block,
47-
FileType::Special => &t.filetype.special,
48-
FileType::Directory { .. } => &t.filetype.dir,
49-
_ => {
50-
if let Some(icon) = t.name.get(name.file_name().to_lowercase().as_str()) {
51-
icon
52-
} else if let Some(icon) = name
53-
.extension()
54-
.and_then(|ext| t.extension.get(ext.to_lowercase().as_str()))
55-
{
56-
icon
57-
} else {
58-
match file_type {
59-
// If a file has no extension and is executable, show an icon.
60-
// Except for Windows, it marks everything as an executable.
61-
#[cfg(not(windows))]
62-
FileType::File { exec: true, .. } => &t.filetype.executable,
63-
_ => &t.filetype.file,
64-
}
65-
}
66-
}
40+
let icon = match icon_scheme(t, name, file_type) {
41+
#[cfg(not(windows))]
42+
(_, _, FileType::File { exec: true, .. }) => &t.filetype.executable,
43+
(_, _, FileType::BlockDevice) => &t.filetype.device_block,
44+
(_, _, FileType::CharDevice) => &t.filetype.device_char,
45+
(_, _, FileType::SymLink { is_dir: true }) => &t.filetype.symlink_dir,
46+
(_, _, FileType::SymLink { is_dir: false }) => &t.filetype.symlink_file,
47+
(_, _, FileType::Pipe) => &t.filetype.pipe,
48+
(_, _, FileType::Socket) => &t.filetype.socket,
49+
(_, _, FileType::Special) => &t.filetype.special,
50+
(None, _, FileType::Directory { .. }) => &t.filetype.dir,
51+
(Some(special_name_icon), _, _) => special_name_icon,
52+
(None, Some(ext_icon), FileType::File { .. }) => ext_icon,
53+
(None, None, FileType::File { .. }) => &t.filetype.file,
6754
};
6855

6956
format!("{}{}", icon, self.icon_separator)
@@ -72,6 +59,19 @@ impl Icons {
7259
}
7360
}
7461

62+
fn icon_scheme<'icon>(
63+
t: &'icon IconTheme,
64+
name: &'icon Name,
65+
file_type: FileType,
66+
) -> (Option<&'icon String>, Option<&'icon String>, FileType) {
67+
(
68+
t.name.get(name.file_name().to_lowercase().as_str()),
69+
name.extension()
70+
.and_then(|ext| t.extension.get(ext.to_lowercase().as_str())),
71+
file_type,
72+
)
73+
}
74+
7575
#[cfg(test)]
7676
mod test {
7777
use super::{IconTheme, Icons};
@@ -205,7 +205,7 @@ mod test {
205205
}
206206

207207
#[test]
208-
fn get_icon_by_name() {
208+
fn get_icon_by_name_files() {
209209
let tmp_dir = tempdir().expect("failed to create temp dir");
210210

211211
for (file_name, file_icon) in &IconTheme::get_default_icons_by_name() {
@@ -221,7 +221,7 @@ mod test {
221221
}
222222

223223
#[test]
224-
fn get_icon_by_extension() {
224+
fn get_icon_by_extension_files() {
225225
let tmp_dir = tempdir().expect("failed to create temp dir");
226226

227227
for (ext, file_icon) in &IconTheme::get_default_icons_by_extension() {
@@ -237,7 +237,7 @@ mod test {
237237
}
238238

239239
#[test]
240-
fn directory_icon_consistency() {
240+
fn get_icon_by_extension_dir() {
241241
let tmp_dir = tempdir().expect("failed to create temp dir");
242242

243243
for (ext, _) in &IconTheme::get_default_icons_by_extension() {
@@ -253,4 +253,20 @@ mod test {
253253
assert_eq!(icon_str, format!("{}{}", by_type.dir, icon.icon_separator));
254254
}
255255
}
256+
257+
#[test]
258+
fn get_icon_by_name_dir() {
259+
let tmp_dir = tempdir().expect("failed to create temp dir");
260+
261+
for (dir_name, dir_icon) in &IconTheme::get_default_icons_by_name() {
262+
let dir_path = tmp_dir.path().join(dir_name);
263+
create_dir_all(&dir_path).expect("failed to create file");
264+
let meta = Meta::from_path(&dir_path, false, false).unwrap();
265+
266+
let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
267+
let icon_str = icon.get(&meta.name);
268+
269+
assert_eq!(icon_str, format!("{}{}", dir_icon, icon.icon_separator));
270+
}
271+
}
256272
}

0 commit comments

Comments
 (0)