icons.rs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. }
  15. impl Icons {
  16. pub fn value(self) -> char {
  17. match self {
  18. Self::Audio => '\u{f001}',
  19. Self::Image => '\u{f1c5}',
  20. Self::Video => '\u{f03d}',
  21. }
  22. }
  23. }
  24. /// Converts the style used to paint a file name into the style that should be
  25. /// used to paint an icon.
  26. ///
  27. /// - The background colour should be preferred to the foreground colour, as
  28. /// if one is set, itโ€™s the more โ€œobviousโ€ colour choice.
  29. /// - If neither is set, just use the default style.
  30. /// - Attributes such as bold or underline should not be used to paint the
  31. /// icon, as they can make it look weird.
  32. pub fn iconify_style(style: Style) -> Style {
  33. style.background.or(style.foreground)
  34. .map(Style::from)
  35. .unwrap_or_default()
  36. }
  37. lazy_static! {
  38. static ref MAP_BY_NAME: HashMap<&'static str, char> = {
  39. let mut m = HashMap::new();
  40. m.insert(".Trash", '\u{f1f8}'); // ๏‡ธ
  41. m.insert(".atom", '\u{e764}'); // ๎ค
  42. m.insert(".bashprofile", '\u{e615}'); // ๎˜•
  43. m.insert(".bashrc", '\u{f489}'); // ๏’‰
  44. m.insert(".git", '\u{f1d3}'); // ๏‡“
  45. m.insert(".gitattributes", '\u{f1d3}'); // ๏‡“
  46. m.insert(".gitconfig", '\u{f1d3}'); // ๏‡“
  47. m.insert(".github", '\u{f408}'); // ๏ˆ
  48. m.insert(".gitignore", '\u{f1d3}'); // ๏‡“
  49. m.insert(".gitmodules", '\u{f1d3}'); // ๏‡“
  50. m.insert(".rvm", '\u{e21e}'); // ๎ˆž
  51. m.insert(".vimrc", '\u{e62b}'); // ๎˜ซ
  52. m.insert(".vscode", '\u{e70c}'); // ๎œŒ
  53. m.insert(".zshrc", '\u{f489}'); // ๏’‰
  54. m.insert("Cargo.lock", '\u{e7a8}'); // ๎žจ
  55. m.insert("bin", '\u{e5fc}'); // ๎—ผ
  56. m.insert("config", '\u{e5fc}'); // ๎—ผ
  57. m.insert("docker-compose.yml", '\u{f308}'); // ๏Œˆ
  58. m.insert("Dockerfile", '\u{f308}'); // ๏Œˆ
  59. m.insert("ds_store", '\u{f179}'); // ๏…น
  60. m.insert("gitignore_global", '\u{f1d3}'); // ๏‡“
  61. m.insert("gradle", '\u{e70e}'); // ๎œŽ
  62. m.insert("gruntfile.coffee", '\u{e611}'); // ๎˜‘
  63. m.insert("gruntfile.js", '\u{e611}'); // ๎˜‘
  64. m.insert("gruntfile.ls", '\u{e611}'); // ๎˜‘
  65. m.insert("gulpfile.coffee", '\u{e610}'); // ๎˜
  66. m.insert("gulpfile.js", '\u{e610}'); // ๎˜
  67. m.insert("gulpfile.ls", '\u{e610}'); // ๎˜
  68. m.insert("hidden", '\u{f023}'); // ๏€ฃ
  69. m.insert("include", '\u{e5fc}'); // ๎—ผ
  70. m.insert("lib", '\u{f121}'); // ๏„ก
  71. m.insert("localized", '\u{f179}'); // ๏…น
  72. m.insert("Makefile", '\u{f489}'); // ๏’‰
  73. m.insert("node_modules", '\u{e718}'); // ๎œ˜
  74. m.insert("npmignore", '\u{e71e}'); // ๎œž
  75. m.insert("rubydoc", '\u{e73b}'); // ๎œป
  76. m.insert("yarn.lock", '\u{e718}'); // ๎œ˜
  77. m
  78. };
  79. }
  80. pub fn icon_for_file(file: &File<'_>) -> char {
  81. let extensions = Box::new(FileExtensions);
  82. if let Some(icon) = MAP_BY_NAME.get(file.name.as_str()) { *icon }
  83. else if file.points_to_directory() {
  84. match file.name.as_str() {
  85. "bin" => '\u{e5fc}', // ๎—ผ
  86. ".git" => '\u{f1d3}', // ๏‡“
  87. ".idea" => '\u{e7b5}', // ๎žต
  88. _ => '\u{f115}' // ๏„•
  89. }
  90. }
  91. else if let Some(icon) = extensions.icon_file(file) { icon }
  92. else if let Some(ext) = file.ext.as_ref() {
  93. match ext.as_str() {
  94. "ai" => '\u{e7b4}', // ๎žด
  95. "android" => '\u{e70e}', // ๎œŽ
  96. "apk" => '\u{e70e}', // ๎œŽ
  97. "apple" => '\u{f179}', // ๏…น
  98. "avi" => '\u{f03d}', // ๏€ฝ
  99. "avro" => '\u{e60b}', // ๎˜‹
  100. "awk" => '\u{f489}', // ๏’‰
  101. "bash" => '\u{f489}', // ๏’‰
  102. "bash_history" => '\u{f489}', // ๏’‰
  103. "bash_profile" => '\u{f489}', // ๏’‰
  104. "bashrc" => '\u{f489}', // ๏’‰
  105. "bat" => '\u{f17a}', // ๏…บ
  106. "bmp" => '\u{f1c5}', // ๏‡…
  107. "bz" => '\u{f410}', // ๏
  108. "bz2" => '\u{f410}', // ๏
  109. "c" => '\u{e61e}', // ๎˜ž
  110. "c++" => '\u{e61d}', // ๎˜
  111. "cab" => '\u{e70f}', // ๎œ
  112. "cc" => '\u{e61d}', // ๎˜
  113. "cfg" => '\u{e615}', // ๎˜•
  114. "class" => '\u{e256}', // ๎‰–
  115. "clj" => '\u{e768}', // ๎จ
  116. "cljs" => '\u{e76a}', // ๎ช
  117. "cls" => '\u{f034}', // ๏€ด
  118. "cmd" => '\u{e70f}', // ๎œ
  119. "coffee" => '\u{f0f4}', // ๏ƒด
  120. "conf" => '\u{e615}', // ๎˜•
  121. "cp" => '\u{e61d}', // ๎˜
  122. "cpp" => '\u{e61d}', // ๎˜
  123. "cs" => '\u{f81a}', // ๏ š
  124. "csh" => '\u{f489}', // ๏’‰
  125. "cshtml" => '\u{f1fa}', // ๏‡บ
  126. "csproj" => '\u{f81a}', // ๏ š
  127. "css" => '\u{e749}', // ๎‰
  128. "csv" => '\u{f1c3}', // ๏‡ƒ
  129. "csx" => '\u{f81a}', // ๏ š
  130. "cxx" => '\u{e61d}', // ๎˜
  131. "d" => '\u{e7af}', // ๎žฏ
  132. "dart" => '\u{e798}', // ๎ž˜
  133. "db" => '\u{f1c0}', // ๏‡€
  134. "deb" => '\u{e77d}', // ๎ฝ
  135. "diff" => '\u{f440}', // ๏‘€
  136. "djvu" => '\u{f02d}', // ๏€ญ
  137. "dll" => '\u{e70f}', // ๎œ
  138. "doc" => '\u{f1c2}', // ๏‡‚
  139. "docx" => '\u{f1c2}', // ๏‡‚
  140. "ds_store" => '\u{f179}', // ๏…น
  141. "DS_store" => '\u{f179}', // ๏…น
  142. "dump" => '\u{f1c0}', // ๎œ†
  143. "ebook" => '\u{e28b}', // ๎Š‹
  144. "editorconfig" => '\u{e615}', // ๎˜•
  145. "ejs" => '\u{e618}', // ๎˜˜
  146. "elm" => '\u{e62c}', // ๎˜ฌ
  147. "env" => '\u{f462}', // ๏‘ข
  148. "eot" => '\u{f031}', // ๏€ฑ
  149. "epub" => '\u{e28a}', // ๎ŠŠ
  150. "erb" => '\u{e73b}', // ๎œป
  151. "erl" => '\u{e7b1}', // ๎žฑ
  152. "ex" => '\u{e62d}', // ๎˜ญ
  153. "exe" => '\u{f17a}', // ๏…บ
  154. "exs" => '\u{e62d}', // ๎˜ญ
  155. "fish" => '\u{f489}', // ๏’‰
  156. "flac" => '\u{f001}', // ๏€
  157. "flv" => '\u{f03d}', // ๏€ฝ
  158. "font" => '\u{f031}', // ๏€ฑ
  159. "fs" => '\u{e7a7}', // ๎žง
  160. "fsi" => '\u{e7a7}', // ๎žง
  161. "fsx" => '\u{e7a7}', // ๎žง
  162. "gdoc" => '\u{f1c2}', // ๏‡‚
  163. "gem" => '\u{e21e}', // ๎ˆž
  164. "gemfile" => '\u{e21e}', // ๎ˆž
  165. "gemspec" => '\u{e21e}', // ๎ˆž
  166. "gform" => '\u{f298}', // ๏Š˜
  167. "gif" => '\u{f1c5}', // ๏‡…
  168. "git" => '\u{f1d3}', // ๏‡“
  169. "gitattributes" => '\u{f1d3}', // ๏‡“
  170. "gitignore" => '\u{f1d3}', // ๏‡“
  171. "gitmodules" => '\u{f1d3}', // ๏‡“
  172. "go" => '\u{e626}', // ๎˜ฆ
  173. "gradle" => '\u{e70e}', // ๎œŽ
  174. "groovy" => '\u{e775}', // ๎ต
  175. "gsheet" => '\u{f1c3}', // ๏‡ƒ
  176. "gslides" => '\u{f1c4}', // ๏‡„
  177. "guardfile" => '\u{e21e}', // ๎ˆž
  178. "gz" => '\u{f410}', // ๏
  179. "h" => '\u{f0fd}', // ๏ƒฝ
  180. "hbs" => '\u{e60f}', // ๎˜
  181. "hpp" => '\u{f0fd}', // ๏ƒฝ
  182. "hs" => '\u{e777}', // ๎ท
  183. "htm" => '\u{f13b}', // ๏„ป
  184. "html" => '\u{f13b}', // ๏„ป
  185. "hxx" => '\u{f0fd}', // ๏ƒฝ
  186. "ico" => '\u{f1c5}', // ๏‡…
  187. "image" => '\u{f1c5}', // ๏‡…
  188. "iml" => '\u{e7b5}', // ๎žต
  189. "ini" => '\u{f17a}', // ๏…บ
  190. "ipynb" => '\u{e606}', // ๎˜†
  191. "iso" => '\u{e271}', // ๎‰ฑ
  192. "jad" => '\u{e256}', // ๎‰–
  193. "jar" => '\u{e204}', // ๎ˆ„
  194. "java" => '\u{e204}', // ๎ˆ„
  195. "jfi" => '\u{f1c5}', // ๏‡…
  196. "jfif" => '\u{f1c5}', // ๏‡…
  197. "jif" => '\u{f1c5}', // ๏‡…
  198. "jpe" => '\u{f1c5}', // ๏‡…
  199. "jpeg" => '\u{f1c5}', // ๏‡…
  200. "jpg" => '\u{f1c5}', // ๏‡…
  201. "js" => '\u{e74e}', // ๎Ž
  202. "json" => '\u{e60b}', // ๎˜‹
  203. "jsx" => '\u{e7ba}', // ๎žบ
  204. "ksh" => '\u{f489}', // ๏’‰
  205. "latex" => '\u{f034}', // ๏€ด
  206. "less" => '\u{e758}', // ๎˜
  207. "lhs" => '\u{e777}', // ๎ท
  208. "license" => '\u{f718}', // ๏œ˜
  209. "localized" => '\u{f179}', // ๏…น
  210. "lock" => '\u{f023}', // ๏€ฃ
  211. "log" => '\u{f18d}', // ๏†
  212. "lua" => '\u{e620}', // ๎˜ 
  213. "lz" => '\u{f410}', // ๏
  214. "lzh" => '\u{f410}', // ๏
  215. "lzma" => '\u{f410}', // ๏
  216. "lzo" => '\u{f410}', // ๏
  217. "m" => '\u{e61e}', // ๎˜ž
  218. "mm" => '\u{e61d}', // ๎˜
  219. "m4a" => '\u{f001}', // ๏€
  220. "markdown" => '\u{f48a}', // ๏’Š
  221. "md" => '\u{f48a}', // ๏’Š
  222. "mjs" => '\u{e74e}', // ๎Ž
  223. "mk" => '\u{f489}', // ๏’‰
  224. "mkd" => '\u{f48a}', // ๏’Š
  225. "mkv" => '\u{f03d}', // ๏€ฝ
  226. "mobi" => '\u{e28b}', // ๎Š‹
  227. "mov" => '\u{f03d}', // ๏€ฝ
  228. "mp3" => '\u{f001}', // ๏€
  229. "mp4" => '\u{f03d}', // ๏€ฝ
  230. "msi" => '\u{e70f}', // ๎œ
  231. "mustache" => '\u{e60f}', // ๎˜
  232. "nix" => '\u{f313}', // ๏Œ“
  233. "node" => '\u{f898}', // ๏ข˜
  234. "npmignore" => '\u{e71e}', // ๎œž
  235. "odp" => '\u{f1c4}', // ๏‡„
  236. "ods" => '\u{f1c3}', // ๏‡ƒ
  237. "odt" => '\u{f1c2}', // ๏‡‚
  238. "ogg" => '\u{f001}', // ๏€
  239. "ogv" => '\u{f03d}', // ๏€ฝ
  240. "otf" => '\u{f031}', // ๏€ฑ
  241. "patch" => '\u{f440}', // ๏‘€
  242. "pdf" => '\u{f1c1}', // ๏‡
  243. "php" => '\u{e73d}', // ๎œฝ
  244. "pl" => '\u{e769}', // ๎ฉ
  245. "png" => '\u{f1c5}', // ๏‡…
  246. "ppt" => '\u{f1c4}', // ๏‡„
  247. "pptx" => '\u{f1c4}', // ๏‡„
  248. "procfile" => '\u{e21e}', // ๎ˆž
  249. "properties" => '\u{e60b}', // ๎˜‹
  250. "ps1" => '\u{f489}', // ๏’‰
  251. "psd" => '\u{e7b8}', // ๎žธ
  252. "pxm" => '\u{f1c5}', // ๏‡…
  253. "py" => '\u{e606}', // ๎˜†
  254. "pyc" => '\u{e606}', // ๎˜†
  255. "r" => '\u{f25d}', // ๏‰
  256. "rakefile" => '\u{e21e}', // ๎ˆž
  257. "rar" => '\u{f410}', // ๏
  258. "razor" => '\u{f1fa}', // ๏‡บ
  259. "rb" => '\u{e21e}', // ๎ˆž
  260. "rdata" => '\u{f25d}', // ๏‰
  261. "rdb" => '\u{e76d}', // ๎ญ
  262. "rdoc" => '\u{f48a}', // ๏’Š
  263. "rds" => '\u{f25d}', // ๏‰
  264. "readme" => '\u{f48a}', // ๏’Š
  265. "rlib" => '\u{e7a8}', // ๎žจ
  266. "rmd" => '\u{f48a}', // ๏’Š
  267. "rpm" => '\u{e7bb}', // ๎žป
  268. "rs" => '\u{e7a8}', // ๎žจ
  269. "rspec" => '\u{e21e}', // ๎ˆž
  270. "rspec_parallel"=> '\u{e21e}', // ๎ˆž
  271. "rspec_status" => '\u{e21e}', // ๎ˆž
  272. "rss" => '\u{f09e}', // ๏‚ž
  273. "rtf" => '\u{f718}', // ๏œ˜
  274. "ru" => '\u{e21e}', // ๎ˆž
  275. "rubydoc" => '\u{e73b}', // ๎œป
  276. "sass" => '\u{e603}', // ๎˜ƒ
  277. "scala" => '\u{e737}', // ๎œท
  278. "scss" => '\u{e749}', // ๎‰
  279. "sh" => '\u{f489}', // ๏’‰
  280. "shell" => '\u{f489}', // ๏’‰
  281. "slim" => '\u{e73b}', // ๎œป
  282. "sln" => '\u{e70c}', // ๎œŒ
  283. "so" => '\u{f17c}', // ๏…ผ
  284. "sql" => '\u{f1c0}', // ๎œ†
  285. "sqlite3" => '\u{e7c4}', // ๎Ÿ„
  286. "sty" => '\u{f034}', // ๏€ด
  287. "styl" => '\u{e600}', // ๎˜€
  288. "stylus" => '\u{e600}', // ๎˜€
  289. "svg" => '\u{f1c5}', // ๏‡…
  290. "swift" => '\u{e755}', // ๎•
  291. "tar" => '\u{f410}', // ๏
  292. "taz" => '\u{f410}', // ๏
  293. "tbz" => '\u{f410}', // ๏
  294. "tbz2" => '\u{f410}', // ๏
  295. "tex" => '\u{f034}', // ๏€ด
  296. "tgz" => '\u{f410}', // ๏
  297. "tiff" => '\u{f1c5}', // ๏‡…
  298. "tlz" => '\u{f410}', // ๏
  299. "toml" => '\u{e615}', // ๎˜•
  300. "ts" => '\u{e628}', // ๎˜จ
  301. "tsv" => '\u{f1c3}', // ๏‡ƒ
  302. "tsx" => '\u{e7ba}', // ๎žบ
  303. "ttf" => '\u{f031}', // ๏€ฑ
  304. "twig" => '\u{e61c}', // ๎˜œ
  305. "txt" => '\u{f15c}', // ๏…œ
  306. "txz" => '\u{f410}', // ๏
  307. "tz" => '\u{f410}', // ๏
  308. "tzo" => '\u{f410}', // ๏
  309. "video" => '\u{f03d}', // ๏€ฝ
  310. "vim" => '\u{e62b}', // ๎˜ซ
  311. "vue" => '\u{fd42}', // ๏ต‚
  312. "war" => '\u{e256}', // ๎‰–
  313. "wav" => '\u{f001}', // ๏€
  314. "webm" => '\u{f03d}', // ๏€ฝ
  315. "webp" => '\u{f1c5}', // ๏‡…
  316. "windows" => '\u{f17a}', // ๏…บ
  317. "woff" => '\u{f031}', // ๏€ฑ
  318. "woff2" => '\u{f031}', // ๏€ฑ
  319. "xhtml" => '\u{f13b}', // ๏„ป
  320. "xls" => '\u{f1c3}', // ๏‡ƒ
  321. "xlsx" => '\u{f1c3}', // ๏‡ƒ
  322. "xml" => '\u{fabf}', // ๏ชฟ
  323. "xul" => '\u{fabf}', // ๏ชฟ
  324. "xz" => '\u{f410}', // ๏
  325. "yaml" => '\u{f481}', // ๏’
  326. "yml" => '\u{f481}', // ๏’
  327. "zip" => '\u{f410}', // ๏
  328. "zsh" => '\u{f489}', // ๏’‰
  329. "zsh-theme" => '\u{f489}', // ๏’‰
  330. "zshrc" => '\u{f489}', // ๏’‰
  331. _ => '\u{f15b}' // ๏…›
  332. }
  333. }
  334. else {
  335. '\u{f016}'
  336. }
  337. }