filetype.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. use colours::{Plain, Style, Black, Red, Green, Yellow, Blue, Purple, Cyan, Fixed};
  2. use file::File;
  3. use std::io;
  4. pub enum FileType {
  5. Normal, Directory, Executable, Immediate, Compiled,
  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. impl FileType {
  31. pub fn style(&self) -> Style {
  32. match *self {
  33. Normal => Plain,
  34. Directory => Blue.bold(),
  35. Executable => Green.bold(),
  36. Image => Fixed(133).normal(),
  37. Video => Fixed(135).normal(),
  38. Music => Fixed(92).normal(),
  39. Lossless => Fixed(93).normal(),
  40. Crypto => Fixed(109).normal(),
  41. Document => Fixed(105).normal(),
  42. Compressed => Red.normal(),
  43. Temp => Fixed(244).normal(),
  44. Immediate => Yellow.bold().underline(),
  45. Compiled => Fixed(137).normal(),
  46. }
  47. }
  48. pub fn from_file(file: &File) -> FileType {
  49. if file.stat.kind == io::TypeDirectory {
  50. return Directory;
  51. }
  52. else if file.stat.perm.contains(io::UserExecute) {
  53. return Executable;
  54. }
  55. else if file.ext.is_some() {
  56. let ext = file.ext.unwrap();
  57. if IMAGE_TYPES.iter().any(|&s| s == ext) {
  58. return Image;
  59. }
  60. else if VIDEO_TYPES.iter().any(|&s| s == ext) {
  61. return Video;
  62. }
  63. else if MUSIC_TYPES.iter().any(|&s| s == ext) {
  64. return Music;
  65. }
  66. else if MUSIC_LOSSLESS.iter().any(|&s| s == ext) {
  67. return Lossless;
  68. }
  69. else if CRYPTO_TYPES.iter().any(|&s| s == ext) {
  70. return Crypto;
  71. }
  72. else if DOCUMENT_TYPES.iter().any(|&s| s == ext) {
  73. return Document;
  74. }
  75. else if COMPRESSED_TYPES.iter().any(|&s| s == ext) {
  76. return Compressed;
  77. }
  78. else if file.is_tmpfile() || TEMP_TYPES.iter().any(|&s| s == ext) {
  79. return Temp;
  80. }
  81. }
  82. if file.name.starts_with("README") {
  83. return Immediate;
  84. }
  85. let source_files = file.get_source_files();
  86. if source_files.len() == 0 {
  87. let source_files_usual = file.get_source_files_usual();
  88. if source_files_usual.iter().any(|path| file.dir.contains(path)) {
  89. Temp
  90. }
  91. else {
  92. Normal
  93. }
  94. }
  95. else if source_files.iter().any(|path| file.dir.contains(path)) {
  96. Temp
  97. }
  98. else {
  99. Compiled
  100. }
  101. }
  102. }