filetype.rs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. use file::{File, GREY};
  2. use self::FileType::*;
  3. use std::io;
  4. use std::ascii::AsciiExt;
  5. use ansi_term::Style;
  6. use ansi_term::Style::Plain;
  7. use ansi_term::Colour::{Red, Green, Yellow, Blue, Cyan, Fixed};
  8. pub enum FileType {
  9. Normal, Directory, Executable, Immediate, Compiled, Symlink, Special,
  10. Image, Video, Music, Lossless, Compressed, Document, Temp, Crypto,
  11. }
  12. impl Copy for FileType { }
  13. static IMAGE_TYPES: &'static [&'static str] = &[
  14. "png", "jpeg", "jpg", "gif", "bmp", "tiff", "tif",
  15. "ppm", "pgm", "pbm", "pnm", "webp", "raw", "arw",
  16. "svg", "stl", "eps", "dvi", "ps", "cbr",
  17. "cbz", "xpm", "ico" ];
  18. static VIDEO_TYPES: &'static [&'static str] = &[
  19. "avi", "flv", "m2v", "mkv", "mov", "mp4", "mpeg",
  20. "mpg", "ogm", "ogv", "vob", "wmv" ];
  21. static MUSIC_TYPES: &'static [&'static str] = &[
  22. "aac", "m4a", "mp3", "ogg", "wma" ];
  23. static MUSIC_LOSSLESS: &'static [&'static str] = &[
  24. "alac", "ape", "flac", "wav" ];
  25. static COMPRESSED_TYPES: &'static [&'static str] = &[
  26. "zip", "tar", "Z", "gz", "bz2", "a", "ar", "7z",
  27. "iso", "dmg", "tc", "rar", "par" ];
  28. static DOCUMENT_TYPES: &'static [&'static str] = &[
  29. "djvu", "doc", "docx", "dvi", "eml", "eps", "fotd",
  30. "odp", "odt", "pdf", "ppt", "pptx", "rtf",
  31. "xls", "xlsx" ];
  32. static TEMP_TYPES: &'static [&'static str] = &[
  33. "tmp", "swp", "swo", "swn", "bak" ];
  34. static CRYPTO_TYPES: &'static [&'static str] = &[
  35. "asc", "gpg", "sig", "signature", "pgp" ];
  36. static COMPILED_TYPES: &'static [&'static str] = &[
  37. "class", "elc", "hi", "o", "pyc" ];
  38. static BUILD_TYPES: &'static [&'static str] = &[
  39. "Makefile", "Cargo.toml", "SConstruct", "CMakeLists.txt",
  40. "build.gradle", "Rakefile", "Gruntfile.js",
  41. "Gruntfile.coffee" ];
  42. impl FileType {
  43. pub fn style(&self) -> Style {
  44. match *self {
  45. Normal => Plain,
  46. Directory => Blue.bold(),
  47. Symlink => Cyan.normal(),
  48. Special => Yellow.normal(),
  49. Executable => Green.bold(),
  50. Image => Fixed(133).normal(),
  51. Video => Fixed(135).normal(),
  52. Music => Fixed(92).normal(),
  53. Lossless => Fixed(93).normal(),
  54. Crypto => Fixed(109).normal(),
  55. Document => Fixed(105).normal(),
  56. Compressed => Red.normal(),
  57. Temp => GREY.normal(),
  58. Immediate => Yellow.bold().underline(),
  59. Compiled => Fixed(137).normal(),
  60. }
  61. }
  62. }
  63. pub trait HasType {
  64. fn get_type(&self) -> FileType;
  65. }
  66. impl<'a> HasType for File<'a> {
  67. fn get_type(&self) -> FileType {
  68. let name = self.name.as_slice();
  69. if self.stat.kind == io::FileType::Directory {
  70. return Directory;
  71. }
  72. else if self.stat.kind == io::FileType::Symlink {
  73. return Symlink;
  74. }
  75. else if self.stat.kind == io::FileType::BlockSpecial || self.stat.kind == io::FileType::NamedPipe || self.stat.kind == io::FileType::Unknown {
  76. return Special;
  77. }
  78. else if self.stat.perm.contains(io::USER_EXECUTE) {
  79. return Executable;
  80. }
  81. else if name.starts_with("README") || BUILD_TYPES.iter().any(|&s| s == name) {
  82. return Immediate;
  83. }
  84. else if let Some(ref e) = self.ext {
  85. let ext = e.as_slice().to_ascii_lowercase();
  86. if IMAGE_TYPES.iter().any(|&s| s == ext) {
  87. return Image;
  88. }
  89. else if VIDEO_TYPES.iter().any(|&s| s == ext) {
  90. return Video;
  91. }
  92. else if MUSIC_TYPES.iter().any(|&s| s == ext) {
  93. return Music;
  94. }
  95. else if MUSIC_LOSSLESS.iter().any(|&s| s == ext) {
  96. return Lossless;
  97. }
  98. else if CRYPTO_TYPES.iter().any(|&s| s == ext) {
  99. return Crypto;
  100. }
  101. else if DOCUMENT_TYPES.iter().any(|&s| s == ext) {
  102. return Document;
  103. }
  104. else if COMPRESSED_TYPES.iter().any(|&s| s == ext) {
  105. return Compressed;
  106. }
  107. else if self.is_tmpfile() || TEMP_TYPES.iter().any(|&s| s == ext) {
  108. return Temp;
  109. }
  110. let source_files = self.get_source_files();
  111. if source_files.is_empty() {
  112. return Normal;
  113. }
  114. else if source_files.iter().any(|path| self.dir.map(|d| d.contains(path)).unwrap_or(false)) {
  115. return Temp;
  116. }
  117. else {
  118. if COMPILED_TYPES.iter().any(|&s| s == ext) {
  119. return Compiled;
  120. }
  121. else {
  122. return Normal;
  123. }
  124. }
  125. }
  126. return Normal; // no filetype
  127. }
  128. }