filetype.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. //! Tests for various types of file (video, image, compressed, etc).
  2. //!
  3. //! Currently this is dependent on the file’s name and extension, because
  4. //! those are the only metadata that we have access to without reading the
  5. //! file’s contents.
  6. use ansi_term::Style;
  7. use crate::fs::File;
  8. use crate::output::icons::FileIcon;
  9. use crate::theme::FileColours;
  10. #[derive(Debug, Default, PartialEq)]
  11. pub struct FileExtensions;
  12. impl FileExtensions {
  13. /// An “immediate” file is something that can be run or activated somehow
  14. /// in order to kick off the build of a project. It’s usually only present
  15. /// in directories full of source code.
  16. fn is_immediate(&self, file: &File<'_>) -> bool {
  17. file.name.to_lowercase().starts_with("readme") ||
  18. file.name.ends_with(".ninja") ||
  19. file.name_is_one_of( &[
  20. "Makefile", "Cargo.toml", "SConstruct", "CMakeLists.txt",
  21. "build.gradle", "pom.xml", "Rakefile", "package.json", "Gruntfile.js",
  22. "Gruntfile.coffee", "BUILD", "BUILD.bazel", "WORKSPACE", "build.xml",
  23. "webpack.config.js", "meson.build", "composer.json", "RoboFile.php", "PKGBUILD",
  24. "Justfile", "Procfile", "Dockerfile", "Containerfile", "Vagrantfile", "Brewfile",
  25. "Gemfile", "Pipfile", "build.sbt", "mix.exs", "bsconfig.json", "tsconfig.json",
  26. ])
  27. }
  28. fn is_image(&self, file: &File<'_>) -> bool {
  29. file.extension_is_one_of( &[
  30. "png", "jpeg", "jpg", "gif", "bmp", "tiff", "tif",
  31. "ppm", "pgm", "pbm", "pnm", "webp", "raw", "arw",
  32. "svg", "stl", "eps", "dvi", "ps", "cbr", "jpf",
  33. "cbz", "xpm", "ico", "cr2", "orf", "nef", "heif",
  34. ])
  35. }
  36. fn is_video(&self, file: &File<'_>) -> bool {
  37. file.extension_is_one_of( &[
  38. "avi", "flv", "m2v", "m4v", "mkv", "mov", "mp4", "mpeg",
  39. "mpg", "ogm", "ogv", "vob", "wmv", "webm", "m2ts", "heic",
  40. ])
  41. }
  42. fn is_music(&self, file: &File<'_>) -> bool {
  43. file.extension_is_one_of( &[
  44. "aac", "m4a", "mp3", "ogg", "wma", "mka", "opus",
  45. ])
  46. }
  47. // Lossless music, rather than any other kind of data...
  48. fn is_lossless(&self, file: &File<'_>) -> bool {
  49. file.extension_is_one_of( &[
  50. "alac", "ape", "flac", "wav",
  51. ])
  52. }
  53. fn is_crypto(&self, file: &File<'_>) -> bool {
  54. file.extension_is_one_of( &[
  55. "asc", "enc", "gpg", "pgp", "sig", "signature", "pfx", "p12",
  56. ])
  57. }
  58. fn is_document(&self, file: &File<'_>) -> bool {
  59. file.extension_is_one_of( &[
  60. "djvu", "doc", "docx", "dvi", "eml", "eps", "fotd", "key",
  61. "keynote", "numbers", "odp", "odt", "pages", "pdf", "ppt",
  62. "pptx", "rtf", "xls", "xlsx",
  63. ])
  64. }
  65. fn is_compressed(&self, file: &File<'_>) -> bool {
  66. file.extension_is_one_of( &[
  67. "zip", "tar", "Z", "z", "gz", "bz2", "a", "ar", "7z",
  68. "iso", "dmg", "tc", "rar", "par", "tgz", "xz", "txz",
  69. "lz", "tlz", "lzma", "deb", "rpm", "zst",
  70. ])
  71. }
  72. fn is_temp(&self, file: &File<'_>) -> bool {
  73. file.name.ends_with('~')
  74. || (file.name.starts_with('#') && file.name.ends_with('#'))
  75. || file.extension_is_one_of( &[ "tmp", "swp", "swo", "swn", "bak", "bk" ])
  76. }
  77. fn is_compiled(&self, file: &File<'_>) -> bool {
  78. if file.extension_is_one_of( &[ "class", "elc", "hi", "o", "pyc", "zwc", "ko" ]) {
  79. true
  80. }
  81. else if let Some(dir) = file.parent_dir {
  82. file.get_source_files().iter().any(|path| dir.contains(path))
  83. }
  84. else {
  85. false
  86. }
  87. }
  88. }
  89. impl FileColours for FileExtensions {
  90. fn colour_file(&self, file: &File<'_>) -> Option<Style> {
  91. use ansi_term::Colour::*;
  92. Some(match file {
  93. f if self.is_temp(f) => Fixed(244).normal(),
  94. f if self.is_immediate(f) => Yellow.bold().underline(),
  95. f if self.is_image(f) => Fixed(133).normal(),
  96. f if self.is_video(f) => Fixed(135).normal(),
  97. f if self.is_music(f) => Fixed(92).normal(),
  98. f if self.is_lossless(f) => Fixed(93).normal(),
  99. f if self.is_crypto(f) => Fixed(109).normal(),
  100. f if self.is_document(f) => Fixed(105).normal(),
  101. f if self.is_compressed(f) => Red.normal(),
  102. f if self.is_compiled(f) => Fixed(137).normal(),
  103. _ => return None,
  104. })
  105. }
  106. }
  107. impl FileIcon for FileExtensions {
  108. fn icon_file(&self, file: &File<'_>) -> Option<char> {
  109. use crate::output::icons::Icons;
  110. if self.is_music(file) || self.is_lossless(file) {
  111. Some(Icons::Audio.value())
  112. }
  113. else if self.is_image(file) {
  114. Some(Icons::Image.value())
  115. }
  116. else if self.is_video(file) {
  117. Some(Icons::Video.value())
  118. }
  119. else {
  120. None
  121. }
  122. }
  123. }