Skip to content

Commit

Permalink
fix: special name exclusion in directory name consistency logic
Browse files Browse the repository at this point in the history
  • Loading branch information
atamakahere-git committed Apr 7, 2024
1 parent ede2f12 commit 0bad9f3
Showing 1 changed file with 46 additions and 30 deletions.
76 changes: 46 additions & 30 deletions src/icon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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};
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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() {
Expand All @@ -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();

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / MinSRV

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Style (ubuntu-latest)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Style (macos-latest)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Style (windows-latest)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-unknown-linux-gnu, use-cross)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, i686-unknown-linux-gnu, use-cross)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, aarch64-unknown-linux-gnu, use-cross)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, i686-unknown-linux-musl, use-cross)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, arm-unknown-linux-gnueabihf, use-cross)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-unknown-linux-musl, use-cross)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, aarch64-unknown-linux-musl, use-cross)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, i686-pc-windows-gnu)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Build (macos-latest, x86_64-apple-darwin)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, i686-pc-windows-msvc)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-msvc)

mismatched types

Check failure on line 264 in src/icon.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-gnu)

mismatched types

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));
}
}
}

0 comments on commit 0bad9f3

Please sign in to comment.