@@ -37,33 +37,49 @@ 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 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+ // _ => {
49+ // match (
50+ // t.name.get(name.file_name().to_lowercase().as_str()),
51+ // name.extension()
52+ // .and_then(|ext| t.extension.get(ext.to_lowercase().as_str())),
53+ // file_type,
54+ // ) {
55+ // // Not a special-named dir
56+ // (None, _, FileType::Directory { .. }) => &t.filetype.dir,
57+ // // Special named file or dir
58+ // (Some(icon), _, _) => icon,
59+ // // Executable on non windows platform
60+ // #[cfg(not(windows))]
61+ // (None, _, FileType::File { exec: true, .. }) => &t.filetype.executable,
62+ // // File with extention or not
63+ // (None, Some(icon), _) => icon,
64+ // (None, None, _) => &t.filetype.file,
65+ // }
66+ // }
67+ // };
68+
69+ let icon = match icon_scheme ( t, name, file_type) {
70+ #[ cfg( not( windows) ) ]
71+ ( _, _, FileType :: File { exec : true , .. } ) => & t. filetype . executable ,
72+ ( _, _, FileType :: BlockDevice ) => & t. filetype . device_block ,
73+ ( _, _, FileType :: CharDevice ) => & t. filetype . device_char ,
74+ ( _, _, FileType :: SymLink { is_dir : true } ) => & t. filetype . symlink_dir ,
75+ ( _, _, FileType :: SymLink { is_dir : false } ) => & t. filetype . symlink_file ,
76+ ( _, _, FileType :: Pipe ) => & t. filetype . pipe ,
77+ ( _, _, FileType :: Socket ) => & t. filetype . socket ,
78+ ( _, _, FileType :: Special ) => & t. filetype . special ,
79+ ( None , _, FileType :: Directory { .. } ) => & t. filetype . dir ,
80+ ( Some ( special_name_icon) , _, _) => special_name_icon,
81+ ( None , Some ( ext_icon) , FileType :: File { .. } ) => ext_icon,
82+ ( None , None , FileType :: File { .. } ) => & t. filetype . file ,
6783 } ;
6884
6985 format ! ( "{}{}" , icon, self . icon_separator)
@@ -72,6 +88,19 @@ impl Icons {
7288 }
7389}
7490
91+ fn icon_scheme < ' icon > (
92+ t : & ' icon IconTheme ,
93+ name : & ' icon Name ,
94+ file_type : FileType ,
95+ ) -> ( Option < & ' icon String > , Option < & ' icon String > , FileType ) {
96+ (
97+ t. name . get ( name. file_name ( ) . to_lowercase ( ) . as_str ( ) ) ,
98+ name. extension ( )
99+ . and_then ( |ext| t. extension . get ( ext. to_lowercase ( ) . as_str ( ) ) ) ,
100+ file_type,
101+ )
102+ }
103+
75104#[ cfg( test) ]
76105mod test {
77106 use super :: { IconTheme , Icons } ;
@@ -205,7 +234,7 @@ mod test {
205234 }
206235
207236 #[ test]
208- fn get_icon_by_name ( ) {
237+ fn get_icon_by_name_files ( ) {
209238 let tmp_dir = tempdir ( ) . expect ( "failed to create temp dir" ) ;
210239
211240 for ( file_name, file_icon) in & IconTheme :: get_default_icons_by_name ( ) {
@@ -221,7 +250,7 @@ mod test {
221250 }
222251
223252 #[ test]
224- fn get_icon_by_extension ( ) {
253+ fn get_icon_by_extension_files ( ) {
225254 let tmp_dir = tempdir ( ) . expect ( "failed to create temp dir" ) ;
226255
227256 for ( ext, file_icon) in & IconTheme :: get_default_icons_by_extension ( ) {
@@ -237,7 +266,7 @@ mod test {
237266 }
238267
239268 #[ test]
240- fn directory_icon_consistency ( ) {
269+ fn get_icon_by_extension_dir ( ) {
241270 let tmp_dir = tempdir ( ) . expect ( "failed to create temp dir" ) ;
242271
243272 for ( ext, _) in & IconTheme :: get_default_icons_by_extension ( ) {
@@ -253,4 +282,20 @@ mod test {
253282 assert_eq ! ( icon_str, format!( "{}{}" , by_type. dir, icon. icon_separator) ) ;
254283 }
255284 }
285+
286+ #[ test]
287+ fn get_icon_by_name_dir ( ) {
288+ let tmp_dir = tempdir ( ) . expect ( "failed to create temp dir" ) ;
289+
290+ for ( dir_name, dir_icon) in & IconTheme :: get_default_icons_by_name ( ) {
291+ let dir_path = tmp_dir. path ( ) . join ( dir_name) ;
292+ create_dir_all ( & dir_path) . expect ( "failed to create file" ) ;
293+ let meta = Meta :: from_path ( & dir_path, false , false ) . unwrap ( ) ;
294+
295+ let icon = Icons :: new ( false , IconOption :: Always , FlagTheme :: Fancy , " " . to_string ( ) ) ;
296+ let icon_str = icon. get ( & meta. name ) ;
297+
298+ assert_eq ! ( icon_str, format!( "{}{}" , dir_icon, icon. icon_separator) ) ;
299+ }
300+ }
256301}
0 commit comments