2014-11-23 21:29:11 +00:00
|
|
|
use file::{File, GREY};
|
|
|
|
use self::FileType::*;
|
2014-06-17 08:35:40 +00:00
|
|
|
|
2014-11-23 21:29:11 +00:00
|
|
|
use std::io;
|
|
|
|
use std::ascii::AsciiExt;
|
2014-07-01 18:00:36 +00:00
|
|
|
|
2014-11-23 21:29:11 +00:00
|
|
|
use ansi_term::{Paint, Plain, Style, Red, Green, Yellow, Blue, Cyan, Fixed};
|
2014-07-01 18:00:36 +00:00
|
|
|
|
2014-06-17 08:35:40 +00:00
|
|
|
pub enum FileType {
|
2014-06-21 17:27:26 +00:00
|
|
|
Normal, Directory, Executable, Immediate, Compiled, Symlink, Special,
|
2014-06-17 08:35:40 +00:00
|
|
|
Image, Video, Music, Lossless, Compressed, Document, Temp, Crypto,
|
|
|
|
}
|
|
|
|
|
|
|
|
static IMAGE_TYPES: &'static [&'static str] = &[
|
|
|
|
"png", "jpeg", "jpg", "gif", "bmp", "tiff", "tif",
|
|
|
|
"ppm", "pgm", "pbm", "pnm", "webp", "raw", "arw",
|
|
|
|
"svg", "stl", "eps", "dvi", "ps", "cbr",
|
|
|
|
"cbz", "xpm", "ico" ];
|
|
|
|
|
|
|
|
static VIDEO_TYPES: &'static [&'static str] = &[
|
|
|
|
"avi", "flv", "m2v", "mkv", "mov", "mp4", "mpeg",
|
2014-06-18 07:27:06 +00:00
|
|
|
"mpg", "ogm", "ogv", "vob", "wmv" ];
|
2014-06-17 08:35:40 +00:00
|
|
|
|
|
|
|
static MUSIC_TYPES: &'static [&'static str] = &[
|
2014-11-23 23:01:06 +00:00
|
|
|
"aac", "m4a", "mp3", "ogg", "wma" ];
|
2014-06-17 08:35:40 +00:00
|
|
|
|
|
|
|
static MUSIC_LOSSLESS: &'static [&'static str] = &[
|
|
|
|
"alac", "ape", "flac", "wav" ];
|
|
|
|
|
|
|
|
static COMPRESSED_TYPES: &'static [&'static str] = &[
|
|
|
|
"zip", "tar", "Z", "gz", "bz2", "a", "ar", "7z",
|
|
|
|
"iso", "dmg", "tc", "rar", "par" ];
|
|
|
|
|
|
|
|
static DOCUMENT_TYPES: &'static [&'static str] = &[
|
2014-11-23 23:01:06 +00:00
|
|
|
"djvu", "doc", "docx", "dvi", "eml", "eps", "fotd",
|
|
|
|
"odp", "odt", "pdf", "ppt", "pptx", "rtf",
|
|
|
|
"xls", "xlsx" ];
|
2014-06-17 08:35:40 +00:00
|
|
|
|
|
|
|
static TEMP_TYPES: &'static [&'static str] = &[
|
|
|
|
"tmp", "swp", "swo", "swn", "bak" ];
|
|
|
|
|
|
|
|
static CRYPTO_TYPES: &'static [&'static str] = &[
|
|
|
|
"asc", "gpg", "sig", "signature", "pgp" ];
|
|
|
|
|
2014-06-18 07:27:06 +00:00
|
|
|
static COMPILED_TYPES: &'static [&'static str] = &[
|
|
|
|
"class", "elc", "hi", "o", "pyc" ];
|
2014-06-28 16:56:57 +00:00
|
|
|
|
|
|
|
static BUILD_TYPES: &'static [&'static str] = &[
|
|
|
|
"Makefile", "Cargo.toml", "SConstruct", "CMakeLists.txt",
|
|
|
|
"build.gradle", "Rakefile", "Gruntfile.js",
|
|
|
|
"Gruntfile.coffee" ];
|
2014-06-18 07:27:06 +00:00
|
|
|
|
2014-06-17 08:35:40 +00:00
|
|
|
impl FileType {
|
|
|
|
pub fn style(&self) -> Style {
|
|
|
|
match *self {
|
2014-11-23 23:01:06 +00:00
|
|
|
Normal => Plain,
|
|
|
|
Directory => Blue.bold(),
|
|
|
|
Symlink => Cyan.normal(),
|
|
|
|
Special => Yellow.normal(),
|
2014-06-17 08:35:40 +00:00
|
|
|
Executable => Green.bold(),
|
2014-11-23 23:01:06 +00:00
|
|
|
Image => Fixed(133).normal(),
|
|
|
|
Video => Fixed(135).normal(),
|
|
|
|
Music => Fixed(92).normal(),
|
|
|
|
Lossless => Fixed(93).normal(),
|
|
|
|
Crypto => Fixed(109).normal(),
|
|
|
|
Document => Fixed(105).normal(),
|
2014-06-17 08:35:40 +00:00
|
|
|
Compressed => Red.normal(),
|
2014-11-23 23:01:06 +00:00
|
|
|
Temp => GREY.normal(),
|
|
|
|
Immediate => Yellow.bold().underline(),
|
|
|
|
Compiled => Fixed(137).normal(),
|
2014-06-17 08:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-17 21:17:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub trait HasType {
|
|
|
|
fn get_type(&self) -> FileType;
|
|
|
|
}
|
2014-06-17 08:35:40 +00:00
|
|
|
|
2014-06-17 21:17:22 +00:00
|
|
|
impl<'a> HasType for File<'a> {
|
|
|
|
fn get_type(&self) -> FileType {
|
2014-06-29 20:02:14 +00:00
|
|
|
let name = self.name.as_slice();
|
2014-06-17 21:17:22 +00:00
|
|
|
if self.stat.kind == io::TypeDirectory {
|
2014-06-17 08:35:40 +00:00
|
|
|
return Directory;
|
|
|
|
}
|
2014-06-21 17:17:24 +00:00
|
|
|
else if self.stat.kind == io::TypeSymlink {
|
|
|
|
return Symlink;
|
|
|
|
}
|
2014-06-21 17:27:26 +00:00
|
|
|
else if self.stat.kind == io::TypeBlockSpecial || self.stat.kind == io::TypeNamedPipe || self.stat.kind == io::TypeUnknown {
|
|
|
|
return Special;
|
|
|
|
}
|
2014-11-23 21:29:11 +00:00
|
|
|
else if self.stat.perm.contains(io::USER_EXECUTE) {
|
2014-06-17 08:35:40 +00:00
|
|
|
return Executable;
|
|
|
|
}
|
2014-06-29 20:02:14 +00:00
|
|
|
else if name.starts_with("README") || BUILD_TYPES.iter().any(|&s| s == name) {
|
2014-06-18 07:27:06 +00:00
|
|
|
return Immediate;
|
|
|
|
}
|
2014-06-17 21:17:22 +00:00
|
|
|
else if self.ext.is_some() {
|
2014-11-23 21:29:11 +00:00
|
|
|
let e = self.ext.clone().unwrap().to_ascii_lower();
|
2014-06-29 20:02:14 +00:00
|
|
|
let ext = e.as_slice();
|
2014-06-17 08:35:40 +00:00
|
|
|
if IMAGE_TYPES.iter().any(|&s| s == ext) {
|
|
|
|
return Image;
|
|
|
|
}
|
|
|
|
else if VIDEO_TYPES.iter().any(|&s| s == ext) {
|
|
|
|
return Video;
|
|
|
|
}
|
|
|
|
else if MUSIC_TYPES.iter().any(|&s| s == ext) {
|
|
|
|
return Music;
|
|
|
|
}
|
|
|
|
else if MUSIC_LOSSLESS.iter().any(|&s| s == ext) {
|
|
|
|
return Lossless;
|
|
|
|
}
|
|
|
|
else if CRYPTO_TYPES.iter().any(|&s| s == ext) {
|
|
|
|
return Crypto;
|
|
|
|
}
|
|
|
|
else if DOCUMENT_TYPES.iter().any(|&s| s == ext) {
|
|
|
|
return Document;
|
|
|
|
}
|
|
|
|
else if COMPRESSED_TYPES.iter().any(|&s| s == ext) {
|
|
|
|
return Compressed;
|
|
|
|
}
|
2014-06-17 21:17:22 +00:00
|
|
|
else if self.is_tmpfile() || TEMP_TYPES.iter().any(|&s| s == ext) {
|
2014-06-17 08:35:40 +00:00
|
|
|
return Temp;
|
|
|
|
}
|
2014-06-18 07:27:06 +00:00
|
|
|
|
|
|
|
let source_files = self.get_source_files();
|
|
|
|
if source_files.len() == 0 {
|
|
|
|
return Normal;
|
|
|
|
}
|
|
|
|
else if source_files.iter().any(|path| self.dir.contains(path)) {
|
|
|
|
return Temp;
|
2014-06-17 08:35:40 +00:00
|
|
|
}
|
|
|
|
else {
|
2014-06-18 07:27:06 +00:00
|
|
|
if COMPILED_TYPES.iter().any(|&s| s == ext) {
|
|
|
|
return Compiled;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return Normal;
|
|
|
|
}
|
2014-06-17 08:35:40 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-18 07:27:06 +00:00
|
|
|
return Normal; // no filetype
|
2014-06-17 08:35:40 +00:00
|
|
|
}
|
|
|
|
}
|