filetype.rs 4.4 KB
Newer Older
B
Ben S 已提交
1
use ansi_term::Style;
2

3 4
use file::File;
use colours::Colours;
B
Ben S 已提交
5

B
Ben S 已提交
6

7
pub fn file_colour(colours: &Colours, file: &File) -> Style {
B
Ben S 已提交
8
    match file {
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
        f if f.is_directory()        => colours.filetypes.directory,
        f if f.is_executable_file()  => colours.filetypes.executable,
        f if f.is_link()             => colours.filetypes.symlink,
        f if !f.is_file()            => colours.filetypes.special,
        f if f.is_immediate()        => colours.filetypes.immediate,
        f if f.is_image()            => colours.filetypes.image,
        f if f.is_video()            => colours.filetypes.video,
        f if f.is_music()            => colours.filetypes.music,
        f if f.is_lossless()         => colours.filetypes.lossless,
        f if f.is_crypto()           => colours.filetypes.crypto,
        f if f.is_document()         => colours.filetypes.document,
        f if f.is_compressed()       => colours.filetypes.compressed,
        f if f.is_temp()             => colours.filetypes.temp,
        f if f.is_compiled()         => colours.filetypes.compiled,
        _                            => colours.filetypes.normal,
B
Ben S 已提交
24
    }
B
Ben S 已提交
25
}
26

B
Ben S 已提交
27

B
Ben S 已提交
28 29 30 31 32 33 34 35 36 37 38 39
trait FileTypes {
    fn is_immediate(&self) -> bool;
    fn is_image(&self) -> bool;
    fn is_video(&self) -> bool;
    fn is_music(&self) -> bool;
    fn is_lossless(&self) -> bool;
    fn is_crypto(&self) -> bool;
    fn is_document(&self) -> bool;
    fn is_compressed(&self) -> bool;
    fn is_temp(&self) -> bool;
    fn is_compiled(&self) -> bool;
}
B
Ben S 已提交
40

B
Ben S 已提交
41 42 43 44 45 46 47
impl<'_> FileTypes for File<'_> {
    fn is_immediate(&self) -> bool {
        self.name.starts_with("README") || self.name_is_one_of( &[
            "Makefile", "Cargo.toml", "SConstruct", "CMakeLists.txt",
            "build.gradle", "Rakefile", "Gruntfile.js",
            "Gruntfile.coffee",
        ])
48
    }
B
Ben S 已提交
49 50 51 52 53 54 55 56

    fn is_image(&self) -> bool {
        self.extension_is_one_of( &[
            "png", "jpeg", "jpg", "gif", "bmp", "tiff", "tif",
            "ppm", "pgm", "pbm", "pnm", "webp", "raw", "arw",
            "svg", "stl", "eps", "dvi", "ps", "cbr",
            "cbz", "xpm", "ico",
        ])
57
    }
B
Ben S 已提交
58 59 60 61 62 63

    fn is_video(&self) -> bool {
        self.extension_is_one_of( &[
            "avi", "flv", "m2v", "mkv", "mov", "mp4", "mpeg",
            "mpg", "ogm", "ogv", "vob", "wmv",
        ])
64
    }
B
Ben S 已提交
65 66 67 68 69

    fn is_music(&self) -> bool {
        self.extension_is_one_of( &[
            "aac", "m4a", "mp3", "ogg", "wma",
        ])
70
    }
B
Ben S 已提交
71 72 73 74 75

    fn is_lossless(&self) -> bool {
        self.extension_is_one_of( &[
            "alac", "ape", "flac", "wav",
        ])
76
    }
B
Ben S 已提交
77 78 79 80 81 82

    fn is_crypto(&self) -> bool {
        self.extension_is_one_of( &[
            "zip", "tar", "Z", "gz", "bz2", "a", "ar", "7z",
            "iso", "dmg", "tc", "rar", "par",
        ])
83
    }
B
Ben S 已提交
84 85 86 87 88 89 90

    fn is_document(&self) -> bool {
        self.extension_is_one_of( &[
            "djvu", "doc", "docx", "dvi", "eml", "eps", "fotd",
            "odp", "odt", "pdf", "ppt", "pptx", "rtf",
            "xls", "xlsx",
        ])
91
    }
B
Ben S 已提交
92 93 94 95 96 97

    fn is_compressed(&self) -> bool {
        self.extension_is_one_of( &[
            "zip", "tar", "Z", "gz", "bz2", "a", "ar", "7z",
            "iso", "dmg", "tc", "rar", "par"
        ])
98
    }
B
Ben S 已提交
99 100 101 102 103

    fn is_temp(&self) -> bool {
        self.name.ends_with("~")
            || (self.name.starts_with("#") && self.name.ends_with("#"))
            || self.extension_is_one_of( &[ "tmp", "swp", "swo", "swn", "bak" ])
104
    }
B
Ben S 已提交
105 106 107 108

    fn is_compiled(&self) -> bool {
        if self.extension_is_one_of( &[ "class", "elc", "hi", "o", "pyc" ]) {
            true
109
        }
B
Ben S 已提交
110 111
        else if let Some(dir) = self.dir {
            self.get_source_files().iter().any(|path| dir.contains(path))
112
        }
B
Ben S 已提交
113 114
        else {
            false
115
        }
116
    }
117
}
B
Ben S 已提交
118

B
Ben S 已提交
119

B
Ben S 已提交
120
#[cfg(broken_test)]
B
Ben S 已提交
121 122
mod test {
    use super::*;
B
Ben S 已提交
123
    use file::test::{dummy_stat, new_file};
B
Ben S 已提交
124 125 126

    #[test]
    fn lowercase() {
B
Ben S 已提交
127
        let file = new_file(dummy_stat(), "/barracks.wav");
B
Ben S 已提交
128 129 130 131 132
        assert_eq!(FileType::Lossless, file.get_type())
    }

    #[test]
    fn uppercase() {
B
Ben S 已提交
133
        let file = new_file(dummy_stat(), "/BARRACKS.WAV");
B
Ben S 已提交
134 135 136 137 138
        assert_eq!(FileType::Lossless, file.get_type())
    }

    #[test]
    fn cargo() {
B
Ben S 已提交
139
        let file = new_file(dummy_stat(), "/Cargo.toml");
B
Ben S 已提交
140 141 142 143 144
        assert_eq!(FileType::Immediate, file.get_type())
    }

    #[test]
    fn not_cargo() {
B
Ben S 已提交
145
        let file = new_file(dummy_stat(), "/cargo.toml");
B
Ben S 已提交
146 147 148
        assert_eq!(FileType::Normal, file.get_type())
    }
}