From 4d1aea12b0cbf86108a63da7d529c148206143bb Mon Sep 17 00:00:00 2001 From: Ben S Date: Sat, 24 May 2014 20:40:31 +0100 Subject: [PATCH] Add colouring for media and compressed files --- file.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/file.rs b/file.rs index 8633dac..9945a8b 100644 --- a/file.rs +++ b/file.rs @@ -6,6 +6,15 @@ use column::{Column, Permissions, FileName, FileSize, User, Group}; use format::{formatBinaryBytes, formatDecimalBytes}; use unix::{get_user_name, get_group_name}; +static MEDIA_TYPES: &'static [&'static str] = &[ + "png", "jpeg", "jpg", "gif", "bmp", "tiff", "tif", + "ppm", "pgm", "pbm", "pnm", "webp", "raw", "arw", + "svg", "pdf", "stl", "eps", "dvi", "ps" ]; + +static COMPRESSED_TYPES: &'static [&'static str] = &[ + "zip", "tar", "Z", "gz", "bz2", "a", "ar", "7z", + "iso", "dmg", "tc", "rar", "par" ]; + // Each file is definitely going to get `stat`ted at least once, if // only to determine what kind of file it is, so carry the `stat` // result around with the file for safe keeping. @@ -81,15 +90,26 @@ impl<'a> File<'a> { } } - fn file_colour(&self) -> Style { if self.stat.kind == io::TypeDirectory { Blue.normal() - } else if self.stat.perm.contains(io::UserExecute) { - Green.normal() - } else if self.name.ends_with("~") { + } + else if self.stat.perm.contains(io::UserExecute) { + Green.bold() + } + else if self.name.ends_with("~") { Black.bold() - } else { + } + else if self.name.starts_with("README") { + Yellow.bold().underline() + } + else if self.ext.is_some() && MEDIA_TYPES.iter().any(|&s| s == self.ext.unwrap()) { + Purple.normal() + } + else if self.ext.is_some() && COMPRESSED_TYPES.iter().any(|&s| s == self.ext.unwrap()) { + Red.normal() + } + else { Plain } }