1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use std::{
    fmt::{self, Display, Formatter},
    path::Path,
};

pub mod dark;
pub mod light;

pub struct File<'a> {
    path: &'a Path,
    pub name: &'a str,
    ext: Option<String>,
}

impl File<'_> {
    pub fn new<'a>(path: &'a Path) -> File<'a> {
        let path = path;
        let name = path.file_name().unwrap().to_str().unwrap();
        let ext = Self::ext(path);

        File { path, name, ext }
    }

    fn points_to_directory(&self) -> bool {
        self.path.display().to_string().ends_with('/')
    }

    fn ext(path: &Path) -> Option<String> {
        if let Some(ext) = path.extension() {
            return Some(ext.to_string_lossy().to_string());
        }
        let name = path.file_name().map(|f| f.to_string_lossy().to_string())?;

        name.rfind('.').map(|p| name[p + 1..].to_ascii_lowercase())
    }
}

pub enum Theme {
    Light,
    Dark,
}

#[derive(Clone, Debug)]
pub struct FileIcon {
    pub icon: char,
    pub color: &'static str,
}

//impl Display for FileIcon {
//    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
//        write!(f, "{}", self.icon)
//    }
//}

impl Display for FileIcon {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        write!(f, "\x1b[38;2;{}m{}", self.color, self.icon)
    }
}

const DEFAULT_FILE_ICON: FileIcon = FileIcon {
    icon: '\u{f016}',
    color: "#7e8e91",
};

const DEFAULT_DIR_ICON: FileIcon = FileIcon {
    icon: '\u{f115}',
    color: "#7e8e91",
};

pub fn icon_for_file(file: &File<'_>, theme: Option<Theme>) -> FileIcon {
    match theme {
        Some(Theme::Light) => light_icon_for_file(file),
        Some(Theme::Dark) => dark_icon_for_file(file),
        None => dark_icon_for_file(file),
    }
}

fn dark_icon_for_file(file: &File<'_>) -> FileIcon {
    if let Some(icon) = dark::ICONS_MAP.get(file.name) {
        return icon.clone();
    } else if let Some(extension) = &file.ext {
        if let Some(icon) = dark::ICONS_MAP.get(extension.as_str()) {
            return icon.clone();
        } else if let Some(icon) = dark::ICONS_MAP.get(extension.to_lowercase().as_str()) {
            return icon.clone();
        } else {
            DEFAULT_FILE_ICON
        }
    } else if file.points_to_directory() {
        DEFAULT_DIR_ICON
    } else {
        DEFAULT_FILE_ICON
    }
}

fn light_icon_for_file(file: &File<'_>) -> FileIcon {
    if let Some(icon) = light::ICONS_MAP.get(file.name) {
        return icon.clone();
    } else if let Some(extension) = &file.ext {
        if let Some(icon) = light::ICONS_MAP.get(extension.as_str()) {
            return icon.clone();
        } else if let Some(icon) = light::ICONS_MAP.get(extension.to_lowercase().as_str()) {
            return icon.clone();
        } else {
            DEFAULT_FILE_ICON
        }
    } else if file.points_to_directory() {
        DEFAULT_DIR_ICON
    } else {
        DEFAULT_FILE_ICON
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::path::Path;

    #[test]
    fn test_file_new() {
        let path = Path::new("example.txt");
        let file = File::new(path);

        assert_eq!(file.name, "example.txt");
        assert_eq!(file.ext, Some("txt".to_string()));
    }

    #[test]
    fn test_file_new_directory() {
        let path = Path::new("some_directory/");
        let file = File::new(path);

        assert_eq!(file.name, "some_directory");
        assert_eq!(file.ext, None);
    }

    #[test]
    fn test_points_to_directory() {
        let dir_path = Path::new("some_directory/");
        let file = File::new(dir_path);

        assert!(file.points_to_directory());

        let file_path = Path::new("file.txt");
        let file = File::new(file_path);

        assert!(!file.points_to_directory());
    }

    #[test]
    fn test_file_extension() {
        let path = Path::new("file.txt");
        let ext = File::ext(path);
        assert_eq!(ext, Some("txt".to_string()));

        let no_ext_path = Path::new("file");
        let ext = File::ext(no_ext_path);
        assert_eq!(ext, None);
    }

    #[test]
    fn test_icon_for_file_with_light_theme() {
        let path = Path::new("file.txt");
        let file = File::new(path);

        let icon = icon_for_file(&file, Some(Theme::Light));
        assert_eq!(icon.icon, '󰈙');
        assert_eq!(icon.color, "#447028");
    }

    #[test]
    fn test_icon_for_file_with_dark_theme() {
        let path = Path::new("file.txt");
        let file = File::new(path);

        let icon = icon_for_file(&file, Some(Theme::Dark));
        assert_eq!(icon.icon, '󰈙');
        assert_eq!(icon.color, "#89e051");
    }

    #[test]
    fn test_default_icon_for_directory() {
        let path = Path::new("some_directory/");
        let file = File::new(path);

        let icon = icon_for_file(&file, Some(Theme::Dark));
        assert_eq!(icon.icon, '\u{f115}'); // Default directory icon
        assert_eq!(icon.color, "#7e8e91");
    }

    #[test]
    fn test_icon_for_file_with_no_theme() {
        let path = Path::new("file.txt");
        let file = File::new(path);

        let icon = icon_for_file(&file, None); // Should default to Dark theme
        assert_eq!(icon.icon, '󰈙');
        assert_eq!(icon.color, "#89e051");
    }
}