@@ -37,33 +37,20 @@ impl Icons {
37
37
Some ( t) => {
38
38
// Check file types
39
39
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 ,
67
54
} ;
68
55
69
56
format ! ( "{}{}" , icon, self . icon_separator)
@@ -72,6 +59,19 @@ impl Icons {
72
59
}
73
60
}
74
61
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
+
75
75
#[ cfg( test) ]
76
76
mod test {
77
77
use super :: { IconTheme , Icons } ;
@@ -205,7 +205,7 @@ mod test {
205
205
}
206
206
207
207
#[ test]
208
- fn get_icon_by_name ( ) {
208
+ fn get_icon_by_name_files ( ) {
209
209
let tmp_dir = tempdir ( ) . expect ( "failed to create temp dir" ) ;
210
210
211
211
for ( file_name, file_icon) in & IconTheme :: get_default_icons_by_name ( ) {
@@ -221,7 +221,7 @@ mod test {
221
221
}
222
222
223
223
#[ test]
224
- fn get_icon_by_extension ( ) {
224
+ fn get_icon_by_extension_files ( ) {
225
225
let tmp_dir = tempdir ( ) . expect ( "failed to create temp dir" ) ;
226
226
227
227
for ( ext, file_icon) in & IconTheme :: get_default_icons_by_extension ( ) {
@@ -237,7 +237,7 @@ mod test {
237
237
}
238
238
239
239
#[ test]
240
- fn directory_icon_consistency ( ) {
240
+ fn get_icon_by_extension_dir ( ) {
241
241
let tmp_dir = tempdir ( ) . expect ( "failed to create temp dir" ) ;
242
242
243
243
for ( ext, _) in & IconTheme :: get_default_icons_by_extension ( ) {
@@ -253,4 +253,20 @@ mod test {
253
253
assert_eq ! ( icon_str, format!( "{}{}" , by_type. dir, icon. icon_separator) ) ;
254
254
}
255
255
}
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
+ }
256
272
}
0 commit comments