-
-
Notifications
You must be signed in to change notification settings - Fork 439
/
icon.rs
237 lines (198 loc) · 8.9 KB
/
icon.rs
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use crate::flags::{IconOption, IconTheme as FlagTheme};
use crate::meta::{FileType, Name};
use crate::theme::{icon::IconTheme, Theme};
pub struct Icons {
icon_separator: String,
theme: Option<IconTheme>,
}
// In order to add a new icon, write the unicode value like "\ue5fb" then
// run the command below in vim:
//
// s#\\u[0-9a-f]*#\=eval('"'.submatch(0).'"')#
impl Icons {
pub fn new(tty: bool, when: IconOption, theme: FlagTheme, icon_separator: String) -> Self {
let icon_theme = match (tty, when, theme) {
(_, IconOption::Never, _) | (false, IconOption::Auto, _) => None,
(_, _, FlagTheme::Fancy) => {
if let Ok(t) = Theme::from_path::<IconTheme>("icons") {
Some(t)
} else {
Some(IconTheme::default())
}
}
(_, _, FlagTheme::Unicode) => Some(IconTheme::unicode()),
};
Self {
icon_separator,
theme: icon_theme,
}
}
pub fn get(&self, name: &Name) -> String {
match &self.theme {
None => String::new(),
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,
_ => {
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 {
FileType::Directory { .. } => &t.filetype.dir,
// 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,
}
}
}
};
format!("{}{}", icon, self.icon_separator)
}
}
}
}
#[cfg(test)]
mod test {
use super::{IconTheme, Icons};
use crate::flags::{IconOption, IconTheme as FlagTheme, PermissionFlag};
use crate::meta::Meta;
use std::fs::File;
use tempfile::tempdir;
#[test]
fn get_no_icon_never_tty() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let icons = Icons::new(true, IconOption::Never, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);
assert_eq!(icon, "");
}
#[test]
fn get_no_icon_never_not_tty() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let icons = Icons::new(false, IconOption::Never, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);
assert_eq!(icon, "");
}
#[test]
fn get_no_icon_auto() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let icons = Icons::new(false, IconOption::Auto, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);
assert_eq!(icon, "");
}
#[test]
fn get_icon_auto_tty() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path().join("file.txt");
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let icons = Icons::new(true, IconOption::Auto, FlagTheme::Fancy, " ".to_string());
let icon = icons.get(&meta.name);
assert_eq!(icon, "\u{f15c} ");
}
#[test]
fn get_icon_always_tty_default_file() {
let tmp_dir = tempdir().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 = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let icon = Icons::new(true, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
assert_eq!(icon_str, "\u{f016} "); //
}
#[test]
fn get_icon_always_not_tty_default_file() {
let tmp_dir = tempdir().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 = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
assert_eq!(icon_str, "\u{f016} "); //
}
#[test]
fn get_icon_default_file_icon_unicode() {
let tmp_dir = tempdir().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 = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let icon = Icons::new(
false,
IconOption::Always,
FlagTheme::Unicode,
" ".to_string(),
);
let icon_str = icon.get(&meta.name);
assert_eq!(icon_str, format!("{}{}", "\u{1f4c4}", icon.icon_separator));
}
#[test]
fn get_icon_default_directory() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = Meta::from_path(file_path, false, PermissionFlag::Rwx).unwrap();
let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
assert_eq!(icon_str, "\u{f115} "); //
}
#[test]
fn get_icon_default_directory_unicode() {
let tmp_dir = tempdir().expect("failed to create temp dir");
let file_path = tmp_dir.path();
let meta = Meta::from_path(file_path, false, PermissionFlag::Rwx).unwrap();
let icon = Icons::new(
false,
IconOption::Always,
FlagTheme::Unicode,
" ".to_string(),
);
let icon_str = icon.get(&meta.name);
assert_eq!(icon_str, format!("{}{}", "\u{1f4c2}", icon.icon_separator));
}
#[test]
fn get_icon_by_name() {
let tmp_dir = tempdir().expect("failed to create temp dir");
for (file_name, file_icon) in &IconTheme::get_default_icons_by_name() {
let file_path = tmp_dir.path().join(file_name);
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
assert_eq!(icon_str, format!("{}{}", file_icon, icon.icon_separator));
}
}
#[test]
fn get_icon_by_extension() {
let tmp_dir = tempdir().expect("failed to create temp dir");
for (ext, file_icon) in &IconTheme::get_default_icons_by_extension() {
let file_path = tmp_dir.path().join(format!("file.{ext}"));
File::create(&file_path).expect("failed to create file");
let meta = Meta::from_path(&file_path, false, PermissionFlag::Rwx).unwrap();
let icon = Icons::new(false, IconOption::Always, FlagTheme::Fancy, " ".to_string());
let icon_str = icon.get(&meta.name);
assert_eq!(icon_str, format!("{}{}", file_icon, icon.icon_separator));
}
}
}