filetype.rs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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::file_name::FileColours;
  9. use crate::output::icons::FileIcon;
  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. ])
  25. }
  26. fn is_image(&self, file: &File<'_>) -> bool {
  27. file.extension_is_one_of( &[
  28. "png", "jpeg", "jpg", "gif", "bmp", "tiff", "tif",
  29. "ppm", "pgm", "pbm", "pnm", "webp", "raw", "arw",
  30. "svg", "stl", "eps", "dvi", "ps", "cbr", "jpf",
  31. "cbz", "xpm", "ico", "cr2", "orf", "nef", "heif",
  32. ])
  33. }
  34. fn is_video(&self, file: &File<'_>) -> bool {
  35. file.extension_is_one_of( &[
  36. "avi", "flv", "m2v", "m4v", "mkv", "mov", "mp4", "mpeg",
  37. "mpg", "ogm", "ogv", "vob", "wmv", "webm", "m2ts", "heic",
  38. ])
  39. }
  40. fn is_music(&self, file: &File<'_>) -> bool {
  41. file.extension_is_one_of( &[
  42. "aac", "m4a", "mp3", "ogg", "wma", "mka", "opus",
  43. ])
  44. }
  45. // Lossless music, rather than any other kind of data...
  46. fn is_lossless(&self, file: &File<'_>) -> bool {
  47. file.extension_is_one_of( &[
  48. "alac", "ape", "flac", "wav",
  49. ])
  50. }
  51. fn is_crypto(&self, file: &File<'_>) -> bool {
  52. file.extension_is_one_of( &[
  53. "asc", "enc", "gpg", "pgp", "sig", "signature", "pfx", "p12",
  54. ])
  55. }
  56. fn is_document(&self, file: &File<'_>) -> bool {
  57. file.extension_is_one_of( &[
  58. "djvu", "doc", "docx", "dvi", "eml", "eps", "fotd", "key",
  59. "keynote", "numbers", "odp", "odt", "pages", "pdf", "ppt",
  60. "pptx", "rtf", "xls", "xlsx",
  61. ])
  62. }
  63. fn is_compressed(&self, file: &File<'_>) -> bool {
  64. file.extension_is_one_of( &[
  65. "zip", "tar", "Z", "z", "gz", "bz2", "a", "ar", "7z",
  66. "iso", "dmg", "tc", "rar", "par", "tgz", "xz", "txz",
  67. "lz", "tlz", "lzma", "deb", "rpm", "zst",
  68. ])
  69. }
  70. fn is_temp(&self, file: &File<'_>) -> bool {
  71. file.name.ends_with('~')
  72. || (file.name.starts_with('#') && file.name.ends_with('#'))
  73. || file.extension_is_one_of( &[ "tmp", "swp", "swo", "swn", "bak", "bk" ])
  74. }
  75. fn is_compiled(&self, file: &File<'_>) -> bool {
  76. if file.extension_is_one_of( &[ "class", "elc", "hi", "o", "pyc", "zwc", "ko" ]) {
  77. true
  78. }
  79. else if let Some(dir) = file.parent_dir {
  80. file.get_source_files().iter().any(|path| dir.contains(path))
  81. }
  82. else {
  83. false
  84. }
  85. }
  86. }
  87. impl FileColours for FileExtensions {
  88. fn colour_file(&self, file: &File<'_>) -> Option<Style> {
  89. use ansi_term::Colour::*;
  90. Some(match file {
  91. f if self.is_temp(f) => Fixed(244).normal(),
  92. f if self.is_immediate(f) => Yellow.bold().underline(),
  93. f if self.is_image(f) => Fixed(133).normal(),
  94. f if self.is_video(f) => Fixed(135).normal(),
  95. f if self.is_music(f) => Fixed(92).normal(),
  96. f if self.is_lossless(f) => Fixed(93).normal(),
  97. f if self.is_crypto(f) => Fixed(109).normal(),
  98. f if self.is_document(f) => Fixed(105).normal(),
  99. f if self.is_compressed(f) => Red.normal(),
  100. f if self.is_compiled(f) => Fixed(137).normal(),
  101. _ => return None,
  102. })
  103. }
  104. }
  105. impl FileIcon for FileExtensions {
  106. fn icon_file(&self, file: &File<'_>) -> Option<char> {
  107. use crate::output::icons::Icons;
  108. if self.is_music(file) || self.is_lossless(file) {
  109. Some(Icons::Audio.value())
  110. }
  111. else if self.is_image(file) {
  112. Some(Icons::Image.value())
  113. }
  114. else if self.is_video(file) {
  115. Some(Icons::Video.value())
  116. }
  117. else {
  118. None
  119. }
  120. }
  121. }