filetype.rs 4.2 KB

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