filetype.rs 4.7 KB

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