filetype.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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,
  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. Executable => Green.bold(),
  39. Image => Fixed(133).normal(),
  40. Video => Fixed(135).normal(),
  41. Music => Fixed(92).normal(),
  42. Lossless => Fixed(93).normal(),
  43. Crypto => Fixed(109).normal(),
  44. Document => Fixed(105).normal(),
  45. Compressed => Red.normal(),
  46. Temp => Fixed(244).normal(),
  47. Immediate => Yellow.bold().underline(),
  48. Compiled => Fixed(137).normal(),
  49. }
  50. }
  51. }
  52. pub trait HasType {
  53. fn get_type(&self) -> FileType;
  54. }
  55. impl<'a> HasType for File<'a> {
  56. fn get_type(&self) -> FileType {
  57. if self.stat.kind == io::TypeDirectory {
  58. return Directory;
  59. }
  60. else if self.stat.kind == io::TypeSymlink {
  61. return Symlink;
  62. }
  63. else if self.stat.perm.contains(io::UserExecute) {
  64. return Executable;
  65. }
  66. else if self.name.starts_with("README") {
  67. return Immediate;
  68. }
  69. else if self.ext.is_some() {
  70. let ext = self.ext.unwrap();
  71. if IMAGE_TYPES.iter().any(|&s| s == ext) {
  72. return Image;
  73. }
  74. else if VIDEO_TYPES.iter().any(|&s| s == ext) {
  75. return Video;
  76. }
  77. else if MUSIC_TYPES.iter().any(|&s| s == ext) {
  78. return Music;
  79. }
  80. else if MUSIC_LOSSLESS.iter().any(|&s| s == ext) {
  81. return Lossless;
  82. }
  83. else if CRYPTO_TYPES.iter().any(|&s| s == ext) {
  84. return Crypto;
  85. }
  86. else if DOCUMENT_TYPES.iter().any(|&s| s == ext) {
  87. return Document;
  88. }
  89. else if COMPRESSED_TYPES.iter().any(|&s| s == ext) {
  90. return Compressed;
  91. }
  92. else if self.is_tmpfile() || TEMP_TYPES.iter().any(|&s| s == ext) {
  93. return Temp;
  94. }
  95. let source_files = self.get_source_files();
  96. if source_files.len() == 0 {
  97. return Normal;
  98. }
  99. else if source_files.iter().any(|path| self.dir.contains(path)) {
  100. return Temp;
  101. }
  102. else {
  103. if COMPILED_TYPES.iter().any(|&s| s == ext) {
  104. return Compiled;
  105. }
  106. else {
  107. return Normal;
  108. }
  109. }
  110. }
  111. return Normal; // no filetype
  112. }
  113. }