Reify file extensions

Instead of having a File do its own extension checking, create a new type that takes a file and checks *that*. This new type (FileExtensions) is currently empty, but were it to contain values, those values could be used to determine the file’s colour.
This commit is contained in:
Benjamin Sago 2017-07-10 13:31:20 +01:00
parent bfa65b3bec
commit 0d8d723408
3 changed files with 48 additions and 69 deletions

View File

@ -7,21 +7,23 @@
use fs::File;
impl<'a> File<'a> {
pub struct FileExtensions;
impl FileExtensions {
/// An “immediate” file is something that can be run or activated somehow
/// in order to kick off the build of a project. Its usually only present
/// in directories full of source code.
pub fn is_immediate(&self) -> bool {
self.name.starts_with("README") || self.name_is_one_of( &[
pub fn is_immediate(&self, file: &File) -> bool {
file.name.starts_with("README") || file.name_is_one_of( &[
"Makefile", "Cargo.toml", "SConstruct", "CMakeLists.txt",
"build.gradle", "Rakefile", "Gruntfile.js",
"Gruntfile.coffee",
])
}
pub fn is_image(&self) -> bool {
self.extension_is_one_of( &[
pub fn is_image(&self, file: &File) -> bool {
file.extension_is_one_of( &[
"png", "jpeg", "jpg", "gif", "bmp", "tiff", "tif",
"ppm", "pgm", "pbm", "pnm", "webp", "raw", "arw",
"svg", "stl", "eps", "dvi", "ps", "cbr",
@ -29,92 +31,62 @@ impl<'a> File<'a> {
])
}
pub fn is_video(&self) -> bool {
self.extension_is_one_of( &[
pub fn is_video(&self, file: &File) -> bool {
file.extension_is_one_of( &[
"avi", "flv", "m2v", "mkv", "mov", "mp4", "mpeg",
"mpg", "ogm", "ogv", "vob", "wmv",
])
}
pub fn is_music(&self) -> bool {
self.extension_is_one_of( &[
pub fn is_music(&self, file: &File) -> bool {
file.extension_is_one_of( &[
"aac", "m4a", "mp3", "ogg", "wma",
])
}
// Lossless music, rather than any other kind of data...
pub fn is_lossless(&self) -> bool {
self.extension_is_one_of( &[
pub fn is_lossless(&self, file: &File) -> bool {
file.extension_is_one_of( &[
"alac", "ape", "flac", "wav",
])
}
pub fn is_crypto(&self) -> bool {
self.extension_is_one_of( &[
pub fn is_crypto(&self, file: &File) -> bool {
file.extension_is_one_of( &[
"asc", "enc", "gpg", "pgp", "sig", "signature", "pfx", "p12",
])
}
pub fn is_document(&self) -> bool {
self.extension_is_one_of( &[
pub fn is_document(&self, file: &File) -> bool {
file.extension_is_one_of( &[
"djvu", "doc", "docx", "dvi", "eml", "eps", "fotd",
"odp", "odt", "pdf", "ppt", "pptx", "rtf",
"xls", "xlsx",
])
}
pub fn is_compressed(&self) -> bool {
self.extension_is_one_of( &[
pub fn is_compressed(&self, file: &File) -> bool {
file.extension_is_one_of( &[
"zip", "tar", "Z", "gz", "bz2", "a", "ar", "7z",
"iso", "dmg", "tc", "rar", "par", "tgz",
])
}
pub 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" ])
pub fn is_temp(&self, file: &File) -> bool {
file.name.ends_with('~')
|| (file.name.starts_with('#') && file.name.ends_with('#'))
|| file.extension_is_one_of( &[ "tmp", "swp", "swo", "swn", "bak" ])
}
pub fn is_compiled(&self) -> bool {
if self.extension_is_one_of( &[ "class", "elc", "hi", "o", "pyc" ]) {
pub fn is_compiled(&self, file: &File) -> bool {
if file.extension_is_one_of( &[ "class", "elc", "hi", "o", "pyc" ]) {
true
}
else if let Some(dir) = self.parent_dir {
self.get_source_files().iter().any(|path| dir.contains(path))
else if let Some(dir) = file.parent_dir {
file.get_source_files().iter().any(|path| dir.contains(path))
}
else {
false
}
}
}
#[cfg(broken_test)]
mod test {
use file::test::{dummy_stat, new_file};
#[test]
fn lowercase() {
let file = new_file(dummy_stat(), "/barracks.wav");
assert_eq!(FileType::Lossless, file.get_type())
}
#[test]
fn uppercase() {
let file = new_file(dummy_stat(), "/BARRACKS.WAV");
assert_eq!(FileType::Lossless, file.get_type())
}
#[test]
fn cargo() {
let file = new_file(dummy_stat(), "/Cargo.toml");
assert_eq!(FileType::Immediate, file.get_type())
}
#[test]
fn not_cargo() {
let file = new_file(dummy_stat(), "/cargo.toml");
assert_eq!(FileType::Normal, file.get_type())
}
}

View File

@ -1,7 +1,7 @@
//! The "info" module contains routines that aren't about probing the
//! filesystem nor displaying output to the user, but are internal "business
//! The “info” module contains routines that arent about probing the
//! filesystem nor displaying output to the user, but are internal business
//! logic” routines that are performed on a files already-read metadata.
//! (This counts the file name as metadata.)
mod filetype;
pub mod filetype;
mod sources;

View File

@ -3,6 +3,7 @@ use std::path::Path;
use ansi_term::{ANSIString, Style};
use fs::{File, FileTarget};
use info::filetype::FileExtensions;
use output::Colours;
use output::escape;
use output::cell::TextCellContents;
@ -23,6 +24,7 @@ impl FileStyle {
pub fn for_file<'a, 'dir>(&self, file: &'a File<'dir>, colours: &'a Colours) -> FileName<'a, 'dir> {
FileName {
file, colours,
exts: FileExtensions,
link_style: LinkStyle::JustFilenames,
classify: self.classify,
target: if file.is_link() { Some(file.link_target()) }
@ -86,6 +88,9 @@ pub struct FileName<'a, 'dir: 'a> {
/// Whether to append file class characters to file names.
classify: Classify,
/// Mapping of file extensions to colours, to highlight regular files.
exts: FileExtensions,
}
@ -137,6 +142,7 @@ impl<'a, 'dir> FileName<'a, 'dir> {
target: None,
link_style: LinkStyle::FullLinkPaths,
classify: Classify::JustFilenames,
exts: FileExtensions,
};
for bit in target.coloured_file_name() {
@ -246,17 +252,18 @@ impl<'a, 'dir> FileName<'a, 'dir> {
| f.is_block_device() => self.colours.filetypes.device,
f if f.is_socket() => self.colours.filetypes.socket,
f if !f.is_file() => self.colours.filetypes.special,
f if f.is_immediate() => self.colours.filetypes.immediate,
f if f.is_image() => self.colours.filetypes.image,
f if f.is_video() => self.colours.filetypes.video,
f if f.is_music() => self.colours.filetypes.music,
f if f.is_lossless() => self.colours.filetypes.lossless,
f if f.is_crypto() => self.colours.filetypes.crypto,
f if f.is_document() => self.colours.filetypes.document,
f if f.is_compressed() => self.colours.filetypes.compressed,
f if f.is_temp() => self.colours.filetypes.temp,
f if f.is_compiled() => self.colours.filetypes.compiled,
_ => self.colours.filetypes.normal,
f if self.exts.is_immediate(f) => self.colours.filetypes.immediate,
f if self.exts.is_image(f) => self.colours.filetypes.image,
f if self.exts.is_video(f) => self.colours.filetypes.video,
f if self.exts.is_music(f) => self.colours.filetypes.music,
f if self.exts.is_lossless(f) => self.colours.filetypes.lossless,
f if self.exts.is_crypto(f) => self.colours.filetypes.crypto,
f if self.exts.is_document(f) => self.colours.filetypes.document,
f if self.exts.is_compressed(f) => self.colours.filetypes.compressed,
f if self.exts.is_temp(f) => self.colours.filetypes.temp,
f if self.exts.is_compiled(f) => self.colours.filetypes.compiled,
_ => self.colours.filetypes.normal,
}
}
}