1
0

icons.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. use ansi_term::Style;
  2. use crate::fs::File;
  3. use crate::info::filetype::FileExtensions;
  4. use lazy_static::lazy_static;
  5. use std::collections::HashMap;
  6. pub trait FileIcon {
  7. fn icon_file(&self, file: &File<'_>) -> Option<char>;
  8. }
  9. #[derive(Copy, Clone)]
  10. pub enum Icons {
  11. Audio,
  12. Image,
  13. Video,
  14. Compressed,
  15. }
  16. impl Icons {
  17. pub fn value(self) -> char {
  18. match self {
  19. Self::Audio => '\u{f001}', // 
  20. Self::Image => '\u{f1c5}', // 
  21. Self::Video => '\u{f03d}', // 
  22. Self::Compressed => '\u{f410}', // 
  23. }
  24. }
  25. }
  26. /// Converts the style used to paint a file name into the style that should be
  27. /// used to paint an icon.
  28. ///
  29. /// - The background colour should be preferred to the foreground colour, as
  30. /// if one is set, it’s the more “obvious” colour choice.
  31. /// - If neither is set, just use the default style.
  32. /// - Attributes such as bold or underline should not be used to paint the
  33. /// icon, as they can make it look weird.
  34. pub fn iconify_style(style: Style) -> Style {
  35. style.background.or(style.foreground)
  36. .map(Style::from)
  37. .unwrap_or_default()
  38. }
  39. lazy_static! {
  40. static ref MAP_BY_NAME: HashMap<&'static str, char> = {
  41. let mut m = HashMap::new();
  42. m.insert(".Trash", '\u{f1f8}'); // 
  43. m.insert(".atom", '\u{e764}'); // 
  44. m.insert(".bashprofile", '\u{e615}'); // 
  45. m.insert(".bashrc", '\u{f489}'); // 
  46. m.insert(".git", '\u{f1d3}'); // 
  47. m.insert(".gitattributes", '\u{f1d3}'); // 
  48. m.insert(".gitconfig", '\u{f1d3}'); // 
  49. m.insert(".github", '\u{f408}'); // 
  50. m.insert(".gitignore", '\u{f1d3}'); // 
  51. m.insert(".gitignore_global", '\u{f1d3}'); // 
  52. m.insert(".gitmodules", '\u{f1d3}'); // 
  53. m.insert(".rvm", '\u{e21e}'); // 
  54. m.insert(".vimrc", '\u{e7c5}'); // 
  55. m.insert(".vscode", '\u{e70c}'); // 
  56. m.insert(".zshrc", '\u{f489}'); // 
  57. m.insert(".emacs", '\u{e632}'); // 
  58. m.insert("LICENSE", '\u{f02d}'); // 
  59. m.insert("Cargo.lock", '\u{e7a8}'); // 
  60. m.insert("bin", '\u{e5fc}'); // 
  61. m.insert("config", '\u{e5fc}'); // 
  62. m.insert("docker-compose.yml", '\u{f308}'); // 
  63. m.insert("Dockerfile", '\u{f308}'); // 
  64. m.insert("Earthfile", '\u{f30d}'); // 🌍
  65. m.insert("ds_store", '\u{f179}'); // 
  66. m.insert("gitignore_global", '\u{f1d3}'); // 
  67. m.insert("go.mod", '\u{e626}'); // 
  68. m.insert("go.sum", '\u{e626}'); // 
  69. m.insert("gradle", '\u{e256}'); // 
  70. m.insert("gruntfile.coffee", '\u{e611}'); // 
  71. m.insert("gruntfile.js", '\u{e611}'); // 
  72. m.insert("gruntfile.ls", '\u{e611}'); // 
  73. m.insert("gulpfile.coffee", '\u{e610}'); // 
  74. m.insert("gulpfile.js", '\u{e610}'); // 
  75. m.insert("gulpfile.ls", '\u{e610}'); // 
  76. m.insert("hidden", '\u{f023}'); // 
  77. m.insert("include", '\u{e5fc}'); // 
  78. m.insert("lib", '\u{f121}'); // 
  79. m.insert("localized", '\u{f179}'); // 
  80. m.insert("Makefile", '\u{f489}'); // 
  81. m.insert("node_modules", '\u{e718}'); // 
  82. m.insert("npmignore", '\u{e71e}'); // 
  83. m.insert("PKGBUILD", '\u{f303}'); // 
  84. m.insert("rubydoc", '\u{e73b}'); // 
  85. m.insert("yarn.lock", '\u{e718}'); // 
  86. m.insert("Vagrantfile", '\u{2371}'); //⍱
  87. m
  88. };
  89. }
  90. pub fn icon_for_file(file: &File<'_>) -> char {
  91. let extensions = Box::new(FileExtensions);
  92. if let Some(icon) = MAP_BY_NAME.get(file.name.as_str()) { *icon }
  93. else if file.points_to_directory() {
  94. match file.name.as_str() {
  95. "bin" => '\u{e5fc}', // 
  96. ".git" => '\u{f1d3}', // 
  97. ".idea" => '\u{e7b5}', // 
  98. _ => '\u{f413}' // 
  99. }
  100. }
  101. else if let Some(icon) = extensions.icon_file(file) { icon }
  102. else if let Some(ext) = file.ext.as_ref() {
  103. match ext.as_str() {
  104. "ai" => '\u{e7b4}', // 
  105. "android" => '\u{e70e}', // 
  106. "apk" => '\u{e70e}', // 
  107. "apple" => '\u{f179}', // 
  108. "avi" => '\u{f03d}', // 
  109. "avif" => '\u{f1c5}', // 
  110. "avro" => '\u{e60b}', // 
  111. "awk" => '\u{f489}', // 
  112. "bash" => '\u{f489}', // 
  113. "bash_history" => '\u{f489}', // 
  114. "bash_profile" => '\u{f489}', // 
  115. "bashrc" => '\u{f489}', // 
  116. "bat" => '\u{f17a}', // 
  117. "bats" => '\u{f489}', // 
  118. "bmp" => '\u{f1c5}', // 
  119. "bz" => '\u{f410}', // 
  120. "bz2" => '\u{f410}', // 
  121. "c" => '\u{e61e}', // 
  122. "c++" => '\u{e61d}', // 
  123. "cab" => '\u{e70f}', // 
  124. "cc" => '\u{e61d}', // 
  125. "cfg" => '\u{e615}', // 
  126. "cjs" => '\u{e74e}', // 
  127. "class" => '\u{e256}', // 
  128. "clj" => '\u{e768}', // 
  129. "cljs" => '\u{e76a}', // 
  130. "cls" => '\u{f034}', // 
  131. "cmd" => '\u{e70f}', // 
  132. "coffee" => '\u{f0f4}', // 
  133. "conf" => '\u{e615}', // 
  134. "cp" => '\u{e61d}', // 
  135. "cpio" => '\u{f410}', // 
  136. "cpp" => '\u{e61d}', // 
  137. "cs" => '\u{f031b}', // 󰌛
  138. "csh" => '\u{f489}', // 
  139. "cshtml" => '\u{f1fa}', // 
  140. "csproj" => '\u{f031b}', // 󰌛
  141. "css" => '\u{e749}', // 
  142. "csv" => '\u{f1c3}', // 
  143. "csx" => '\u{f031b}', // 󰌛
  144. "cts" => '\u{e628}', // 
  145. "cxx" => '\u{e61d}', // 
  146. "d" => '\u{e7af}', // 
  147. "dart" => '\u{e798}', // 
  148. "db" => '\u{f1c0}', // 
  149. "deb" => '\u{e77d}', // 
  150. "diff" => '\u{f440}', // 
  151. "djvu" => '\u{f02d}', // 
  152. "dll" => '\u{e70f}', // 
  153. "doc" => '\u{f1c2}', // 
  154. "docx" => '\u{f1c2}', // 
  155. "ds_store" => '\u{f179}', // 
  156. "DS_store" => '\u{f179}', // 
  157. "dump" => '\u{f1c0}', // 
  158. "ebook" => '\u{e28b}', // 
  159. "ebuild" => '\u{f30d}', // 
  160. "editorconfig" => '\u{e615}', // 
  161. "ejs" => '\u{e618}', // 
  162. "el" => '\u{e632}', // 
  163. "elm" => '\u{e62c}', // 
  164. "env" => '\u{f462}', // 
  165. "eot" => '\u{f031}', // 
  166. "epub" => '\u{e28a}', // 
  167. "erb" => '\u{e73b}', // 
  168. "erl" => '\u{e7b1}', // 
  169. "ex" => '\u{e62d}', // 
  170. "exe" => '\u{f17a}', // 
  171. "exs" => '\u{e62d}', // 
  172. "fish" => '\u{f489}', // 
  173. "flac" => '\u{f001}', // 
  174. "flv" => '\u{f03d}', // 
  175. "font" => '\u{f031}', // 
  176. "fs" => '\u{e7a7}', // 
  177. "fsi" => '\u{e7a7}', // 
  178. "fsx" => '\u{e7a7}', // 
  179. "gdoc" => '\u{f1c2}', // 
  180. "gem" => '\u{e21e}', // 
  181. "gemfile" => '\u{e21e}', // 
  182. "gemspec" => '\u{e21e}', // 
  183. "gform" => '\u{f298}', // 
  184. "gif" => '\u{f1c5}', // 
  185. "git" => '\u{f1d3}', // 
  186. "gitattributes" => '\u{f1d3}', // 
  187. "gitignore" => '\u{f1d3}', // 
  188. "gitmodules" => '\u{f1d3}', // 
  189. "go" => '\u{e626}', // 
  190. "gpg" => '\u{e60a}', // 
  191. "gradle" => '\u{e256}', // 
  192. "groovy" => '\u{e775}', // 
  193. "gsheet" => '\u{f1c3}', // 
  194. "gslides" => '\u{f1c4}', // 
  195. "guardfile" => '\u{e21e}', // 
  196. "gz" => '\u{f410}', // 
  197. "h" => '\u{f0fd}', // 
  198. "hbs" => '\u{e60f}', // 
  199. "hpp" => '\u{f0fd}', // 
  200. "hs" => '\u{e777}', // 
  201. "htm" => '\u{f13b}', // 
  202. "html" => '\u{f13b}', // 
  203. "hxx" => '\u{f0fd}', // 
  204. "ico" => '\u{f1c5}', // 
  205. "image" => '\u{f1c5}', // 
  206. "img" => '\u{e271}', // 
  207. "iml" => '\u{e7b5}', // 
  208. "ini" => '\u{f17a}', // 
  209. "ipynb" => '\u{e678}', // 
  210. "iso" => '\u{e271}', // 
  211. "j2c" => '\u{f1c5}', // 
  212. "j2k" => '\u{f1c5}', // 
  213. "jad" => '\u{e256}', // 
  214. "jar" => '\u{e256}', // 
  215. "java" => '\u{e256}', // 
  216. "jfi" => '\u{f1c5}', // 
  217. "jfif" => '\u{f1c5}', // 
  218. "jif" => '\u{f1c5}', // 
  219. "jl" => '\u{e624}', // 
  220. "jmd" => '\u{f48a}', // 
  221. "jp2" => '\u{f1c5}', // 
  222. "jpe" => '\u{f1c5}', // 
  223. "jpeg" => '\u{f1c5}', // 
  224. "jpg" => '\u{f1c5}', // 
  225. "jpx" => '\u{f1c5}', // 
  226. "js" => '\u{e74e}', // 
  227. "json" => '\u{e60b}', // 
  228. "jsx" => '\u{e7ba}', // 
  229. "jxl" => '\u{f1c5}', // 
  230. "ksh" => '\u{f489}', // 
  231. "latex" => '\u{f034}', // 
  232. "less" => '\u{e758}', // 
  233. "lhs" => '\u{e777}', // 
  234. "license" => '\u{f02d}', // 
  235. "localized" => '\u{f179}', // 
  236. "lock" => '\u{f023}', // 
  237. "log" => '\u{f18d}', // 
  238. "lua" => '\u{e620}', // 
  239. "lz" => '\u{f410}', // 
  240. "lz4" => '\u{f410}', // 
  241. "lzh" => '\u{f410}', // 
  242. "lzma" => '\u{f410}', // 
  243. "lzo" => '\u{f410}', // 
  244. "m" => '\u{e61e}', // 
  245. "ml" => '\u{1d77a}',// 𝝺
  246. "mm" => '\u{e61d}', // 
  247. "m4a" => '\u{f001}', // 
  248. "markdown" => '\u{f48a}', // 
  249. "md" => '\u{f48a}', // 
  250. "mjs" => '\u{e74e}', // 
  251. "mk" => '\u{f489}', // 
  252. "mkd" => '\u{f48a}', // 
  253. "mkv" => '\u{f03d}', // 
  254. "mobi" => '\u{e28b}', // 
  255. "mov" => '\u{f03d}', // 
  256. "mp2" => '\u{f001}', // 
  257. "mp3" => '\u{f001}', // 
  258. "mp4" => '\u{f03d}', // 
  259. "msi" => '\u{e70f}', // 
  260. "mts" => '\u{e628}', // 
  261. "mustache" => '\u{e60f}', // 
  262. "nix" => '\u{f313}', // 
  263. "node" => '\u{f0399}', // 󰎙
  264. "npmignore" => '\u{e71e}', // 
  265. "odp" => '\u{f1c4}', // 
  266. "ods" => '\u{f1c3}', // 
  267. "odt" => '\u{f1c2}', // 
  268. "ogg" => '\u{f001}', // 
  269. "ogv" => '\u{f03d}', // 
  270. "org" => '\u{e633}', // 
  271. "otf" => '\u{f031}', // 
  272. "part" => '\u{f43a}', // 
  273. "patch" => '\u{f440}', // 
  274. "pdf" => '\u{f1c1}', // 
  275. "php" => '\u{e73d}', // 
  276. "pl" => '\u{e769}', // 
  277. "plx" => '\u{e769}', // 
  278. "pm" => '\u{e769}', // 
  279. "png" => '\u{f1c5}', // 
  280. "pod" => '\u{e769}', // 
  281. "ppt" => '\u{f1c4}', // 
  282. "pptx" => '\u{f1c4}', // 
  283. "procfile" => '\u{e21e}', // 
  284. "properties" => '\u{e60b}', // 
  285. "ps1" => '\u{f489}', // 
  286. "psd" => '\u{e7b8}', // 
  287. "pxm" => '\u{f1c5}', // 
  288. "py" => '\u{e606}', // 
  289. "pyc" => '\u{e606}', // 
  290. "r" => '\u{f25d}', // 
  291. "rakefile" => '\u{e21e}', // 
  292. "rar" => '\u{f410}', // 
  293. "razor" => '\u{f1fa}', // 
  294. "rb" => '\u{e21e}', // 
  295. "rdata" => '\u{f25d}', // 
  296. "rdb" => '\u{e76d}', // 
  297. "rdoc" => '\u{f48a}', // 
  298. "rds" => '\u{f25d}', // 
  299. "readme" => '\u{f48a}', // 
  300. "rlib" => '\u{e7a8}', // 
  301. "rmd" => '\u{f48a}', // 
  302. "rmeta" => '\u{e7a8}', // 
  303. "rpm" => '\u{e7bb}', // 
  304. "rs" => '\u{e7a8}', // 
  305. "rspec" => '\u{e21e}', // 
  306. "rspec_parallel"=> '\u{e21e}', // 
  307. "rspec_status" => '\u{e21e}', // 
  308. "rss" => '\u{f09e}', // 
  309. "rst" => '\u{f15c}', // 
  310. "rtf" => '\u{f0219}', // 󰈙
  311. "ru" => '\u{e21e}', // 
  312. "rubydoc" => '\u{e73b}', // 
  313. "sass" => '\u{e603}', // 
  314. "scala" => '\u{e737}', // 
  315. "scss" => '\u{e749}', // 
  316. "sh" => '\u{f489}', // 
  317. "shell" => '\u{f489}', // 
  318. "slim" => '\u{e73b}', // 
  319. "sln" => '\u{e70c}', // 
  320. "so" => '\u{f17c}', // 
  321. "sql" => '\u{f1c0}', // 
  322. "sqlite3" => '\u{e7c4}', // 
  323. "sty" => '\u{f034}', // 
  324. "styl" => '\u{e600}', // 
  325. "stylus" => '\u{e600}', // 
  326. "svg" => '\u{f1c5}', // 
  327. "swift" => '\u{e755}', // 
  328. "t" => '\u{e769}', // 
  329. "tar" => '\u{f410}', // 
  330. "taz" => '\u{f410}', // 
  331. "tbz" => '\u{f410}', // 
  332. "tbz2" => '\u{f410}', // 
  333. "tex" => '\u{f034}', // 
  334. "tgz" => '\u{f410}', // 
  335. "tiff" => '\u{f1c5}', // 
  336. "tlz" => '\u{f410}', // 
  337. "toml" => '\u{e615}', // 
  338. "torrent" => '\u{e275}', // 
  339. "ts" => '\u{e628}', // 
  340. "tsv" => '\u{f1c3}', // 
  341. "tsx" => '\u{e7ba}', // 
  342. "ttf" => '\u{f031}', // 
  343. "twig" => '\u{e61c}', // 
  344. "txt" => '\u{f15c}', // 
  345. "txz" => '\u{f410}', // 
  346. "tz" => '\u{f410}', // 
  347. "tzo" => '\u{f410}', // 
  348. "video" => '\u{f03d}', // 
  349. "vim" => '\u{e7c5}', // 
  350. "vue" => '\u{f0844}', // 󰡄
  351. "war" => '\u{e256}', // 
  352. "wav" => '\u{f001}', // 
  353. "webm" => '\u{f03d}', // 
  354. "webp" => '\u{f1c5}', // 
  355. "windows" => '\u{f17a}', // 
  356. "woff" => '\u{f031}', // 
  357. "woff2" => '\u{f031}', // 
  358. "xhtml" => '\u{f13b}', // 
  359. "xls" => '\u{f1c3}', // 
  360. "xlsx" => '\u{f1c3}', // 
  361. "xml" => '\u{f05c0}', // 󰗀
  362. "xul" => '\u{f05c0}', // 󰗀
  363. "xz" => '\u{f410}', // 
  364. "yaml" => '\u{f481}', // 
  365. "yml" => '\u{f481}', // 
  366. "zig" => '\u{21af}', // ↯
  367. "zip" => '\u{f410}', // 
  368. "zsh" => '\u{f489}', // 
  369. "zsh-theme" => '\u{f489}', // 
  370. "zshrc" => '\u{f489}', // 
  371. "zst" => '\u{f410}', // 
  372. "svelte" => '\u{e697}', // 
  373. _ => '\u{f15b}' // 
  374. }
  375. }
  376. else {
  377. '\u{f016}'
  378. }
  379. }