Added default icons to Icons struct

Also added tests for unicode icon set
This commit is contained in:
Sebastian Zivota 2019-01-16 17:54:23 +01:00 committed by Pierre Peltier
parent 390f5e4280
commit 5151446fcb

View file

@ -5,6 +5,8 @@ pub struct Icons {
display_icons: bool,
icons_by_name: HashMap<&'static str, &'static str>,
icons_by_extension: HashMap<&'static str, &'static str>,
default_folder_icon: &'static str,
default_file_icon: &'static str,
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
@ -23,19 +25,24 @@ const ICON_SPACE: &str = " ";
impl Icons {
pub fn new(theme: Theme) -> Self {
let display_icons = theme == Theme::Fancy || theme == Theme::Unicode;
let (icons_by_name, icons_by_extension) = if theme == Theme::Fancy {
(
Self::get_default_icons_by_name(),
Self::get_default_icons_by_extension(),
)
} else {
(HashMap::new(), HashMap::new())
};
let (icons_by_name, icons_by_extension, default_file_icon, default_folder_icon) =
if theme == Theme::Fancy {
(
Self::get_default_icons_by_name(),
Self::get_default_icons_by_extension(),
"\u{f016}",
"\u{f115}",
)
} else {
(HashMap::new(), HashMap::new(), "\u{1F5CB}", "\u{1F5C1}")
};
Self {
display_icons,
icons_by_name,
icons_by_extension,
default_file_icon,
default_folder_icon,
}
}
@ -48,7 +55,7 @@ impl Icons {
// Check directory.
if name.file_type() == FileType::Directory {
res += "\u{f115}"; // 
res += self.default_folder_icon;
res += ICON_SPACE;
return res;
}
@ -70,7 +77,7 @@ impl Icons {
}
// Use the default icons.
res += "\u{f016}"; // 
res += self.default_file_icon;
res += ICON_SPACE;
res
}
@ -330,6 +337,21 @@ mod test {
assert_eq!(icon, format!("{}{}", "\u{f016}", ICON_SPACE)); // 
}
#[test]
fn get_default_file_icon_unicode() {
let tmp_dir = TempDir::new("test_file_type").expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file");
File::create(&file_path).expect("failed to create file");
let meta = file_path.metadata().expect("failed to get metas");
let file_type = FileType::new(&meta, &Permissions::from(&meta));
let name = Name::new(&file_path, file_type);
let icon = Icons::new(Theme::Unicode);
let icon = icon.get(&name);
assert_eq!(icon, format!("{}{}", "\u{1F5CB}", ICON_SPACE));
}
#[test]
fn get_directory_icon() {
let tmp_dir = TempDir::new("test_file_type").expect("failed to create temp dir");
@ -344,6 +366,20 @@ mod test {
assert_eq!(icon, format!("{}{}", "\u{f115}", ICON_SPACE)); // 
}
#[test]
fn get_directory_icon_unicode() {
let tmp_dir = TempDir::new("test_file_type").expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = file_path.metadata().expect("failed to get metas");
let file_type = FileType::new(&meta, &Permissions::from(&meta));
let name = Name::new(&file_path, file_type);
let icon = Icons::new(Theme::Unicode);
let icon = icon.get(&name);
assert_eq!(icon, format!("{}{}", "\u{1F5C1}", ICON_SPACE));
}
#[test]
fn get_directory_icon_with_ext() {
let tmp_dir = TempDir::new("test_file_type.rs").expect("failed to create temp dir");