icons.rs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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("PKGBUILD", '\u{f303}'); // 
  76. m.insert("rubydoc", '\u{e73b}'); // 
  77. m.insert("yarn.lock", '\u{e718}'); // 
  78. m
  79. };
  80. }
  81. pub fn icon_for_file(file: &File<'_>) -> char {
  82. let extensions = Box::new(FileExtensions);
  83. if let Some(icon) = MAP_BY_NAME.get(file.name.as_str()) { *icon }
  84. else if file.points_to_directory() {
  85. match file.name.as_str() {
  86. "bin" => '\u{e5fc}', // 
  87. ".git" => '\u{f1d3}', // 
  88. ".idea" => '\u{e7b5}', // 
  89. _ => '\u{f115}' // 
  90. }
  91. }
  92. else if let Some(icon) = extensions.icon_file(file) { icon }
  93. else if let Some(ext) = file.ext.as_ref() {
  94. match ext.as_str() {
  95. "ai" => '\u{e7b4}', // 
  96. "android" => '\u{e70e}', // 
  97. "apk" => '\u{e70e}', // 
  98. "apple" => '\u{f179}', // 
  99. "avi" => '\u{f03d}', // 
  100. "avif" => '\u{f1c5}', // 
  101. "avro" => '\u{e60b}', // 
  102. "awk" => '\u{f489}', // 
  103. "bash" => '\u{f489}', // 
  104. "bash_history" => '\u{f489}', // 
  105. "bash_profile" => '\u{f489}', // 
  106. "bashrc" => '\u{f489}', // 
  107. "bat" => '\u{f17a}', // 
  108. "bats" => '\u{f489}', // 
  109. "bmp" => '\u{f1c5}', // 
  110. "bz" => '\u{f410}', // 
  111. "bz2" => '\u{f410}', // 
  112. "c" => '\u{e61e}', // 
  113. "c++" => '\u{e61d}', // 
  114. "cab" => '\u{e70f}', // 
  115. "cc" => '\u{e61d}', // 
  116. "cfg" => '\u{e615}', // 
  117. "class" => '\u{e256}', // 
  118. "clj" => '\u{e768}', // 
  119. "cljs" => '\u{e76a}', // 
  120. "cls" => '\u{f034}', // 
  121. "cmd" => '\u{e70f}', // 
  122. "coffee" => '\u{f0f4}', // 
  123. "conf" => '\u{e615}', // 
  124. "cp" => '\u{e61d}', // 
  125. "cpp" => '\u{e61d}', // 
  126. "cs" => '\u{f81a}', // 
  127. "csh" => '\u{f489}', // 
  128. "cshtml" => '\u{f1fa}', // 
  129. "csproj" => '\u{f81a}', // 
  130. "css" => '\u{e749}', // 
  131. "csv" => '\u{f1c3}', // 
  132. "csx" => '\u{f81a}', // 
  133. "cxx" => '\u{e61d}', // 
  134. "d" => '\u{e7af}', // 
  135. "dart" => '\u{e798}', // 
  136. "db" => '\u{f1c0}', // 
  137. "deb" => '\u{e77d}', // 
  138. "diff" => '\u{f440}', // 
  139. "djvu" => '\u{f02d}', // 
  140. "dll" => '\u{e70f}', // 
  141. "doc" => '\u{f1c2}', // 
  142. "docx" => '\u{f1c2}', // 
  143. "ds_store" => '\u{f179}', // 
  144. "DS_store" => '\u{f179}', // 
  145. "dump" => '\u{f1c0}', // 
  146. "ebook" => '\u{e28b}', // 
  147. "ebuild" => '\u{f30d}', // 
  148. "editorconfig" => '\u{e615}', // 
  149. "ejs" => '\u{e618}', // 
  150. "elm" => '\u{e62c}', // 
  151. "env" => '\u{f462}', // 
  152. "eot" => '\u{f031}', // 
  153. "epub" => '\u{e28a}', // 
  154. "erb" => '\u{e73b}', // 
  155. "erl" => '\u{e7b1}', // 
  156. "ex" => '\u{e62d}', // 
  157. "exe" => '\u{f17a}', // 
  158. "exs" => '\u{e62d}', // 
  159. "fish" => '\u{f489}', // 
  160. "flac" => '\u{f001}', // 
  161. "flv" => '\u{f03d}', // 
  162. "font" => '\u{f031}', // 
  163. "fs" => '\u{e7a7}', // 
  164. "fsi" => '\u{e7a7}', // 
  165. "fsx" => '\u{e7a7}', // 
  166. "gdoc" => '\u{f1c2}', // 
  167. "gem" => '\u{e21e}', // 
  168. "gemfile" => '\u{e21e}', // 
  169. "gemspec" => '\u{e21e}', // 
  170. "gform" => '\u{f298}', // 
  171. "gif" => '\u{f1c5}', // 
  172. "git" => '\u{f1d3}', // 
  173. "gitattributes" => '\u{f1d3}', // 
  174. "gitignore" => '\u{f1d3}', // 
  175. "gitmodules" => '\u{f1d3}', // 
  176. "go" => '\u{e626}', // 
  177. "gradle" => '\u{e70e}', // 
  178. "groovy" => '\u{e775}', // 
  179. "gsheet" => '\u{f1c3}', // 
  180. "gslides" => '\u{f1c4}', // 
  181. "guardfile" => '\u{e21e}', // 
  182. "gz" => '\u{f410}', // 
  183. "h" => '\u{f0fd}', // 
  184. "hbs" => '\u{e60f}', // 
  185. "hpp" => '\u{f0fd}', // 
  186. "hs" => '\u{e777}', // 
  187. "htm" => '\u{f13b}', // 
  188. "html" => '\u{f13b}', // 
  189. "hxx" => '\u{f0fd}', // 
  190. "ico" => '\u{f1c5}', // 
  191. "image" => '\u{f1c5}', // 
  192. "iml" => '\u{e7b5}', // 
  193. "ini" => '\u{f17a}', // 
  194. "ipynb" => '\u{e606}', // 
  195. "iso" => '\u{e271}', // 
  196. "jad" => '\u{e256}', // 
  197. "jar" => '\u{e204}', // 
  198. "java" => '\u{e204}', // 
  199. "jfi" => '\u{f1c5}', // 
  200. "jfif" => '\u{f1c5}', // 
  201. "jif" => '\u{f1c5}', // 
  202. "jl" => '\u{e624}', // 
  203. "jpe" => '\u{f1c5}', // 
  204. "jpeg" => '\u{f1c5}', // 
  205. "jpg" => '\u{f1c5}', // 
  206. "js" => '\u{e74e}', // 
  207. "json" => '\u{e60b}', // 
  208. "jsx" => '\u{e7ba}', // 
  209. "jxl" => '\u{f1c5}', // 
  210. "ksh" => '\u{f489}', // 
  211. "latex" => '\u{f034}', // 
  212. "less" => '\u{e758}', // 
  213. "lhs" => '\u{e777}', // 
  214. "license" => '\u{f718}', // 
  215. "localized" => '\u{f179}', // 
  216. "lock" => '\u{f023}', // 
  217. "log" => '\u{f18d}', // 
  218. "lua" => '\u{e620}', // 
  219. "lz" => '\u{f410}', // 
  220. "lz4" => '\u{f410}', // 
  221. "lzh" => '\u{f410}', // 
  222. "lzma" => '\u{f410}', // 
  223. "lzo" => '\u{f410}', // 
  224. "m" => '\u{e61e}', // 
  225. "mm" => '\u{e61d}', // 
  226. "m4a" => '\u{f001}', // 
  227. "markdown" => '\u{f48a}', // 
  228. "md" => '\u{f48a}', // 
  229. "mjs" => '\u{e74e}', // 
  230. "mk" => '\u{f489}', // 
  231. "mkd" => '\u{f48a}', // 
  232. "mkv" => '\u{f03d}', // 
  233. "mobi" => '\u{e28b}', // 
  234. "mov" => '\u{f03d}', // 
  235. "mp3" => '\u{f001}', // 
  236. "mp4" => '\u{f03d}', // 
  237. "msi" => '\u{e70f}', // 
  238. "mustache" => '\u{e60f}', // 
  239. "nix" => '\u{f313}', // 
  240. "node" => '\u{f898}', // 
  241. "npmignore" => '\u{e71e}', // 
  242. "odp" => '\u{f1c4}', // 
  243. "ods" => '\u{f1c3}', // 
  244. "odt" => '\u{f1c2}', // 
  245. "ogg" => '\u{f001}', // 
  246. "ogv" => '\u{f03d}', // 
  247. "otf" => '\u{f031}', // 
  248. "part" => '\u{f43a}', // 
  249. "patch" => '\u{f440}', // 
  250. "pdf" => '\u{f1c1}', // 
  251. "php" => '\u{e73d}', // 
  252. "pl" => '\u{e769}', // 
  253. "png" => '\u{f1c5}', // 
  254. "ppt" => '\u{f1c4}', // 
  255. "pptx" => '\u{f1c4}', // 
  256. "procfile" => '\u{e21e}', // 
  257. "properties" => '\u{e60b}', // 
  258. "ps1" => '\u{f489}', // 
  259. "psd" => '\u{e7b8}', // 
  260. "pxm" => '\u{f1c5}', // 
  261. "py" => '\u{e606}', // 
  262. "pyc" => '\u{e606}', // 
  263. "r" => '\u{f25d}', // 
  264. "rakefile" => '\u{e21e}', // 
  265. "rar" => '\u{f410}', // 
  266. "razor" => '\u{f1fa}', // 
  267. "rb" => '\u{e21e}', // 
  268. "rdata" => '\u{f25d}', // 
  269. "rdb" => '\u{e76d}', // 
  270. "rdoc" => '\u{f48a}', // 
  271. "rds" => '\u{f25d}', // 
  272. "readme" => '\u{f48a}', // 
  273. "rlib" => '\u{e7a8}', // 
  274. "rmd" => '\u{f48a}', // 
  275. "rpm" => '\u{e7bb}', // 
  276. "rs" => '\u{e7a8}', // 
  277. "rspec" => '\u{e21e}', // 
  278. "rspec_parallel"=> '\u{e21e}', // 
  279. "rspec_status" => '\u{e21e}', // 
  280. "rss" => '\u{f09e}', // 
  281. "rtf" => '\u{f718}', // 
  282. "ru" => '\u{e21e}', // 
  283. "rubydoc" => '\u{e73b}', // 
  284. "sass" => '\u{e603}', // 
  285. "scala" => '\u{e737}', // 
  286. "scss" => '\u{e749}', // 
  287. "sh" => '\u{f489}', // 
  288. "shell" => '\u{f489}', // 
  289. "slim" => '\u{e73b}', // 
  290. "sln" => '\u{e70c}', // 
  291. "so" => '\u{f17c}', // 
  292. "sql" => '\u{f1c0}', // 
  293. "sqlite3" => '\u{e7c4}', // 
  294. "sty" => '\u{f034}', // 
  295. "styl" => '\u{e600}', // 
  296. "stylus" => '\u{e600}', // 
  297. "svg" => '\u{f1c5}', // 
  298. "swift" => '\u{e755}', // 
  299. "tar" => '\u{f410}', // 
  300. "taz" => '\u{f410}', // 
  301. "tbz" => '\u{f410}', // 
  302. "tbz2" => '\u{f410}', // 
  303. "tex" => '\u{f034}', // 
  304. "tgz" => '\u{f410}', // 
  305. "tiff" => '\u{f1c5}', // 
  306. "tlz" => '\u{f410}', // 
  307. "toml" => '\u{e615}', // 
  308. "torrent" => '\u{e275}', // 
  309. "ts" => '\u{e628}', // 
  310. "tsv" => '\u{f1c3}', // 
  311. "tsx" => '\u{e7ba}', // 
  312. "ttf" => '\u{f031}', // 
  313. "twig" => '\u{e61c}', // 
  314. "txt" => '\u{f15c}', // 
  315. "txz" => '\u{f410}', // 
  316. "tz" => '\u{f410}', // 
  317. "tzo" => '\u{f410}', // 
  318. "video" => '\u{f03d}', // 
  319. "vim" => '\u{e62b}', // 
  320. "vue" => '\u{fd42}', // ﵂
  321. "war" => '\u{e256}', // 
  322. "wav" => '\u{f001}', // 
  323. "webm" => '\u{f03d}', // 
  324. "webp" => '\u{f1c5}', // 
  325. "windows" => '\u{f17a}', // 
  326. "woff" => '\u{f031}', // 
  327. "woff2" => '\u{f031}', // 
  328. "xhtml" => '\u{f13b}', // 
  329. "xls" => '\u{f1c3}', // 
  330. "xlsx" => '\u{f1c3}', // 
  331. "xml" => '\u{fabf}', // 謹
  332. "xul" => '\u{fabf}', // 謹
  333. "xz" => '\u{f410}', // 
  334. "yaml" => '\u{f481}', // 
  335. "yml" => '\u{f481}', // 
  336. "zip" => '\u{f410}', // 
  337. "zsh" => '\u{f489}', // 
  338. "zsh-theme" => '\u{f489}', // 
  339. "zshrc" => '\u{f489}', // 
  340. "zst" => '\u{f410}', // 
  341. _ => '\u{f15b}' // 
  342. }
  343. }
  344. else {
  345. '\u{f016}'
  346. }
  347. }