icons.rs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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("go.mod", '\u{e626}'); // ๎˜ฆ
  62. m.insert("go.sum", '\u{e626}'); // ๎˜ฆ
  63. m.insert("gradle", '\u{e256}'); // ๎‰–
  64. m.insert("gruntfile.coffee", '\u{e611}'); // ๎˜‘
  65. m.insert("gruntfile.js", '\u{e611}'); // ๎˜‘
  66. m.insert("gruntfile.ls", '\u{e611}'); // ๎˜‘
  67. m.insert("gulpfile.coffee", '\u{e610}'); // ๎˜
  68. m.insert("gulpfile.js", '\u{e610}'); // ๎˜
  69. m.insert("gulpfile.ls", '\u{e610}'); // ๎˜
  70. m.insert("hidden", '\u{f023}'); // ๏€ฃ
  71. m.insert("include", '\u{e5fc}'); // ๎—ผ
  72. m.insert("lib", '\u{f121}'); // ๏„ก
  73. m.insert("localized", '\u{f179}'); // ๏…น
  74. m.insert("Makefile", '\u{f489}'); // ๏’‰
  75. m.insert("node_modules", '\u{e718}'); // ๎œ˜
  76. m.insert("npmignore", '\u{e71e}'); // ๎œž
  77. m.insert("PKGBUILD", '\u{f303}'); // ๏Œƒ
  78. m.insert("rubydoc", '\u{e73b}'); // ๎œป
  79. m.insert("yarn.lock", '\u{e718}'); // ๎œ˜
  80. m
  81. };
  82. }
  83. pub fn icon_for_file(file: &File<'_>) -> char {
  84. let extensions = Box::new(FileExtensions);
  85. if let Some(icon) = MAP_BY_NAME.get(file.name.as_str()) { *icon }
  86. else if file.points_to_directory() {
  87. match file.name.as_str() {
  88. "bin" => '\u{e5fc}', // ๎—ผ
  89. ".git" => '\u{f1d3}', // ๏‡“
  90. ".idea" => '\u{e7b5}', // ๎žต
  91. _ => '\u{f115}' // ๏„•
  92. }
  93. }
  94. else if let Some(icon) = extensions.icon_file(file) { icon }
  95. else if let Some(ext) = file.ext.as_ref() {
  96. match ext.as_str() {
  97. "ai" => '\u{e7b4}', // ๎žด
  98. "android" => '\u{e70e}', // ๎œŽ
  99. "apk" => '\u{e70e}', // ๎œŽ
  100. "apple" => '\u{f179}', // ๏…น
  101. "avi" => '\u{f03d}', // ๏€ฝ
  102. "avif" => '\u{f1c5}', // ๏‡…
  103. "avro" => '\u{e60b}', // ๎˜‹
  104. "awk" => '\u{f489}', // ๏’‰
  105. "bash" => '\u{f489}', // ๏’‰
  106. "bash_history" => '\u{f489}', // ๏’‰
  107. "bash_profile" => '\u{f489}', // ๏’‰
  108. "bashrc" => '\u{f489}', // ๏’‰
  109. "bat" => '\u{f17a}', // ๏…บ
  110. "bats" => '\u{f489}', // ๏’‰
  111. "bmp" => '\u{f1c5}', // ๏‡…
  112. "bz" => '\u{f410}', // ๏
  113. "bz2" => '\u{f410}', // ๏
  114. "c" => '\u{e61e}', // ๎˜ž
  115. "c++" => '\u{e61d}', // ๎˜
  116. "cab" => '\u{e70f}', // ๎œ
  117. "cc" => '\u{e61d}', // ๎˜
  118. "cfg" => '\u{e615}', // ๎˜•
  119. "cjs" => '\u{e74e}', // ๎Ž
  120. "class" => '\u{e256}', // ๎‰–
  121. "clj" => '\u{e768}', // ๎จ
  122. "cljs" => '\u{e76a}', // ๎ช
  123. "cls" => '\u{f034}', // ๏€ด
  124. "cmd" => '\u{e70f}', // ๎œ
  125. "coffee" => '\u{f0f4}', // ๏ƒด
  126. "conf" => '\u{e615}', // ๎˜•
  127. "cp" => '\u{e61d}', // ๎˜
  128. "cpio" => '\u{f410}', // ๏
  129. "cpp" => '\u{e61d}', // ๎˜
  130. "cs" => '\u{f81a}', // ๏ š
  131. "csh" => '\u{f489}', // ๏’‰
  132. "cshtml" => '\u{f1fa}', // ๏‡บ
  133. "csproj" => '\u{f81a}', // ๏ š
  134. "css" => '\u{e749}', // ๎‰
  135. "csv" => '\u{f1c3}', // ๏‡ƒ
  136. "csx" => '\u{f81a}', // ๏ š
  137. "cts" => '\u{e628}', // ๎˜จ
  138. "cxx" => '\u{e61d}', // ๎˜
  139. "d" => '\u{e7af}', // ๎žฏ
  140. "dart" => '\u{e798}', // ๎ž˜
  141. "db" => '\u{f1c0}', // ๏‡€
  142. "deb" => '\u{e77d}', // ๎ฝ
  143. "diff" => '\u{f440}', // ๏‘€
  144. "djvu" => '\u{f02d}', // ๏€ญ
  145. "dll" => '\u{e70f}', // ๎œ
  146. "doc" => '\u{f1c2}', // ๏‡‚
  147. "docx" => '\u{f1c2}', // ๏‡‚
  148. "ds_store" => '\u{f179}', // ๏…น
  149. "DS_store" => '\u{f179}', // ๏…น
  150. "dump" => '\u{f1c0}', // ๎œ†
  151. "ebook" => '\u{e28b}', // ๎Š‹
  152. "ebuild" => '\u{f30d}', // ๏Œ
  153. "editorconfig" => '\u{e615}', // ๎˜•
  154. "ejs" => '\u{e618}', // ๎˜˜
  155. "elm" => '\u{e62c}', // ๎˜ฌ
  156. "env" => '\u{f462}', // ๏‘ข
  157. "eot" => '\u{f031}', // ๏€ฑ
  158. "epub" => '\u{e28a}', // ๎ŠŠ
  159. "erb" => '\u{e73b}', // ๎œป
  160. "erl" => '\u{e7b1}', // ๎žฑ
  161. "ex" => '\u{e62d}', // ๎˜ญ
  162. "exe" => '\u{f17a}', // ๏…บ
  163. "exs" => '\u{e62d}', // ๎˜ญ
  164. "fish" => '\u{f489}', // ๏’‰
  165. "flac" => '\u{f001}', // ๏€
  166. "flv" => '\u{f03d}', // ๏€ฝ
  167. "font" => '\u{f031}', // ๏€ฑ
  168. "fs" => '\u{e7a7}', // ๎žง
  169. "fsi" => '\u{e7a7}', // ๎žง
  170. "fsx" => '\u{e7a7}', // ๎žง
  171. "gdoc" => '\u{f1c2}', // ๏‡‚
  172. "gem" => '\u{e21e}', // ๎ˆž
  173. "gemfile" => '\u{e21e}', // ๎ˆž
  174. "gemspec" => '\u{e21e}', // ๎ˆž
  175. "gform" => '\u{f298}', // ๏Š˜
  176. "gif" => '\u{f1c5}', // ๏‡…
  177. "git" => '\u{f1d3}', // ๏‡“
  178. "gitattributes" => '\u{f1d3}', // ๏‡“
  179. "gitignore" => '\u{f1d3}', // ๏‡“
  180. "gitmodules" => '\u{f1d3}', // ๏‡“
  181. "go" => '\u{e626}', // ๎˜ฆ
  182. "gradle" => '\u{e256}', // ๎‰–
  183. "groovy" => '\u{e775}', // ๎ต
  184. "gsheet" => '\u{f1c3}', // ๏‡ƒ
  185. "gslides" => '\u{f1c4}', // ๏‡„
  186. "guardfile" => '\u{e21e}', // ๎ˆž
  187. "gz" => '\u{f410}', // ๏
  188. "h" => '\u{f0fd}', // ๏ƒฝ
  189. "hbs" => '\u{e60f}', // ๎˜
  190. "hpp" => '\u{f0fd}', // ๏ƒฝ
  191. "hs" => '\u{e777}', // ๎ท
  192. "htm" => '\u{f13b}', // ๏„ป
  193. "html" => '\u{f13b}', // ๏„ป
  194. "hxx" => '\u{f0fd}', // ๏ƒฝ
  195. "ico" => '\u{f1c5}', // ๏‡…
  196. "image" => '\u{f1c5}', // ๏‡…
  197. "img" => '\u{e271}', // ๎‰ฑ
  198. "iml" => '\u{e7b5}', // ๎žต
  199. "ini" => '\u{f17a}', // ๏…บ
  200. "ipynb" => '\u{e606}', // ๎˜†
  201. "iso" => '\u{e271}', // ๎‰ฑ
  202. "j2c" => '\u{f1c5}', // ๏‡…
  203. "j2k" => '\u{f1c5}', // ๏‡…
  204. "jad" => '\u{e256}', // ๎‰–
  205. "jar" => '\u{e256}', // ๎‰–
  206. "java" => '\u{e256}', // ๎‰–
  207. "jfi" => '\u{f1c5}', // ๏‡…
  208. "jfif" => '\u{f1c5}', // ๏‡…
  209. "jif" => '\u{f1c5}', // ๏‡…
  210. "jl" => '\u{e624}', // ๎˜ค
  211. "jmd" => '\u{f48a}', // ๏’Š
  212. "jp2" => '\u{f1c5}', // ๏‡…
  213. "jpe" => '\u{f1c5}', // ๏‡…
  214. "jpeg" => '\u{f1c5}', // ๏‡…
  215. "jpg" => '\u{f1c5}', // ๏‡…
  216. "jpx" => '\u{f1c5}', // ๏‡…
  217. "js" => '\u{e74e}', // ๎Ž
  218. "json" => '\u{e60b}', // ๎˜‹
  219. "jsx" => '\u{e7ba}', // ๎žบ
  220. "jxl" => '\u{f1c5}', // ๏‡…
  221. "ksh" => '\u{f489}', // ๏’‰
  222. "latex" => '\u{f034}', // ๏€ด
  223. "less" => '\u{e758}', // ๎˜
  224. "lhs" => '\u{e777}', // ๎ท
  225. "license" => '\u{f718}', // ๏œ˜
  226. "localized" => '\u{f179}', // ๏…น
  227. "lock" => '\u{f023}', // ๏€ฃ
  228. "log" => '\u{f18d}', // ๏†
  229. "lua" => '\u{e620}', // ๎˜ 
  230. "lz" => '\u{f410}', // ๏
  231. "lz4" => '\u{f410}', // ๏
  232. "lzh" => '\u{f410}', // ๏
  233. "lzma" => '\u{f410}', // ๏
  234. "lzo" => '\u{f410}', // ๏
  235. "m" => '\u{e61e}', // ๎˜ž
  236. "mm" => '\u{e61d}', // ๎˜
  237. "m4a" => '\u{f001}', // ๏€
  238. "markdown" => '\u{f48a}', // ๏’Š
  239. "md" => '\u{f48a}', // ๏’Š
  240. "mjs" => '\u{e74e}', // ๎Ž
  241. "mk" => '\u{f489}', // ๏’‰
  242. "mkd" => '\u{f48a}', // ๏’Š
  243. "mkv" => '\u{f03d}', // ๏€ฝ
  244. "mobi" => '\u{e28b}', // ๎Š‹
  245. "mov" => '\u{f03d}', // ๏€ฝ
  246. "mp3" => '\u{f001}', // ๏€
  247. "mp4" => '\u{f03d}', // ๏€ฝ
  248. "msi" => '\u{e70f}', // ๎œ
  249. "mts" => '\u{e628}', // ๎˜จ
  250. "mustache" => '\u{e60f}', // ๎˜
  251. "nix" => '\u{f313}', // ๏Œ“
  252. "node" => '\u{f898}', // ๏ข˜
  253. "npmignore" => '\u{e71e}', // ๎œž
  254. "odp" => '\u{f1c4}', // ๏‡„
  255. "ods" => '\u{f1c3}', // ๏‡ƒ
  256. "odt" => '\u{f1c2}', // ๏‡‚
  257. "ogg" => '\u{f001}', // ๏€
  258. "ogv" => '\u{f03d}', // ๏€ฝ
  259. "otf" => '\u{f031}', // ๏€ฑ
  260. "part" => '\u{f43a}', // ๏บ
  261. "patch" => '\u{f440}', // ๏‘€
  262. "pdf" => '\u{f1c1}', // ๏‡
  263. "php" => '\u{e73d}', // ๎œฝ
  264. "pl" => '\u{e769}', // ๎ฉ
  265. "plx" => '\u{e769}', // ๎ฉ
  266. "pm" => '\u{e769}', // ๎ฉ
  267. "png" => '\u{f1c5}', // ๏‡…
  268. "pod" => '\u{e769}', // ๎ฉ
  269. "ppt" => '\u{f1c4}', // ๏‡„
  270. "pptx" => '\u{f1c4}', // ๏‡„
  271. "procfile" => '\u{e21e}', // ๎ˆž
  272. "properties" => '\u{e60b}', // ๎˜‹
  273. "ps1" => '\u{f489}', // ๏’‰
  274. "psd" => '\u{e7b8}', // ๎žธ
  275. "pxm" => '\u{f1c5}', // ๏‡…
  276. "py" => '\u{e606}', // ๎˜†
  277. "pyc" => '\u{e606}', // ๎˜†
  278. "r" => '\u{f25d}', // ๏‰
  279. "rakefile" => '\u{e21e}', // ๎ˆž
  280. "rar" => '\u{f410}', // ๏
  281. "razor" => '\u{f1fa}', // ๏‡บ
  282. "rb" => '\u{e21e}', // ๎ˆž
  283. "rdata" => '\u{f25d}', // ๏‰
  284. "rdb" => '\u{e76d}', // ๎ญ
  285. "rdoc" => '\u{f48a}', // ๏’Š
  286. "rds" => '\u{f25d}', // ๏‰
  287. "readme" => '\u{f48a}', // ๏’Š
  288. "rlib" => '\u{e7a8}', // ๎žจ
  289. "rmd" => '\u{f48a}', // ๏’Š
  290. "rpm" => '\u{e7bb}', // ๎žป
  291. "rs" => '\u{e7a8}', // ๎žจ
  292. "rspec" => '\u{e21e}', // ๎ˆž
  293. "rspec_parallel"=> '\u{e21e}', // ๎ˆž
  294. "rspec_status" => '\u{e21e}', // ๎ˆž
  295. "rss" => '\u{f09e}', // ๏‚ž
  296. "rtf" => '\u{f718}', // ๏œ˜
  297. "ru" => '\u{e21e}', // ๎ˆž
  298. "rubydoc" => '\u{e73b}', // ๎œป
  299. "sass" => '\u{e603}', // ๎˜ƒ
  300. "scala" => '\u{e737}', // ๎œท
  301. "scss" => '\u{e749}', // ๎‰
  302. "sh" => '\u{f489}', // ๏’‰
  303. "shell" => '\u{f489}', // ๏’‰
  304. "slim" => '\u{e73b}', // ๎œป
  305. "sln" => '\u{e70c}', // ๎œŒ
  306. "so" => '\u{f17c}', // ๏…ผ
  307. "sql" => '\u{f1c0}', // ๎œ†
  308. "sqlite3" => '\u{e7c4}', // ๎Ÿ„
  309. "sty" => '\u{f034}', // ๏€ด
  310. "styl" => '\u{e600}', // ๎˜€
  311. "stylus" => '\u{e600}', // ๎˜€
  312. "svg" => '\u{f1c5}', // ๏‡…
  313. "swift" => '\u{e755}', // ๎•
  314. "t" => '\u{e769}', // ๎ฉ
  315. "tar" => '\u{f410}', // ๏
  316. "taz" => '\u{f410}', // ๏
  317. "tbz" => '\u{f410}', // ๏
  318. "tbz2" => '\u{f410}', // ๏
  319. "tex" => '\u{f034}', // ๏€ด
  320. "tgz" => '\u{f410}', // ๏
  321. "tiff" => '\u{f1c5}', // ๏‡…
  322. "tlz" => '\u{f410}', // ๏
  323. "toml" => '\u{e615}', // ๎˜•
  324. "torrent" => '\u{e275}', // ๎‰ต
  325. "ts" => '\u{e628}', // ๎˜จ
  326. "tsv" => '\u{f1c3}', // ๏‡ƒ
  327. "tsx" => '\u{e7ba}', // ๎žบ
  328. "ttf" => '\u{f031}', // ๏€ฑ
  329. "twig" => '\u{e61c}', // ๎˜œ
  330. "txt" => '\u{f15c}', // ๏…œ
  331. "txz" => '\u{f410}', // ๏
  332. "tz" => '\u{f410}', // ๏
  333. "tzo" => '\u{f410}', // ๏
  334. "video" => '\u{f03d}', // ๏€ฝ
  335. "vim" => '\u{e62b}', // ๎˜ซ
  336. "vue" => '\u{fd42}', // ๏ต‚
  337. "war" => '\u{e256}', // ๎‰–
  338. "wav" => '\u{f001}', // ๏€
  339. "webm" => '\u{f03d}', // ๏€ฝ
  340. "webp" => '\u{f1c5}', // ๏‡…
  341. "windows" => '\u{f17a}', // ๏…บ
  342. "woff" => '\u{f031}', // ๏€ฑ
  343. "woff2" => '\u{f031}', // ๏€ฑ
  344. "xhtml" => '\u{f13b}', // ๏„ป
  345. "xls" => '\u{f1c3}', // ๏‡ƒ
  346. "xlsx" => '\u{f1c3}', // ๏‡ƒ
  347. "xml" => '\u{f121}', // ๏„ก
  348. "xul" => '\u{f121}', // ๏„ก
  349. "xz" => '\u{f410}', // ๏
  350. "yaml" => '\u{f481}', // ๏’
  351. "yml" => '\u{f481}', // ๏’
  352. "zip" => '\u{f410}', // ๏
  353. "zsh" => '\u{f489}', // ๏’‰
  354. "zsh-theme" => '\u{f489}', // ๏’‰
  355. "zshrc" => '\u{f489}', // ๏’‰
  356. "zst" => '\u{f410}', // ๏
  357. _ => '\u{f15b}' // ๏…›
  358. }
  359. }
  360. else {
  361. '\u{f016}'
  362. }
  363. }