From c448b3747fe10241b203288ebb322b5ed3c7a073 Mon Sep 17 00:00:00 2001 From: Alex Soderman <5639572+asoderman@users.noreply.github.com> Date: Tue, 27 Mar 2018 13:00:38 -0400 Subject: [PATCH] added icons for video/audio/images remove unnecessary clone --- src/info/filetype.rs | 14 ++++++++++++++ src/output/icons.rs | 29 ++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/src/info/filetype.rs b/src/info/filetype.rs index e1d3b2d..25a45ca 100644 --- a/src/info/filetype.rs +++ b/src/info/filetype.rs @@ -8,6 +8,7 @@ use ansi_term::Style; use fs::File; use output::file_name::FileColours; +use output::icons::FileIcon; #[derive(Debug, Default, PartialEq)] @@ -115,3 +116,16 @@ impl FileColours for FileExtensions { }) } } + +impl FileIcon for FileExtensions { + fn icon_file(&self, file: &File) -> Option { + use output::icons::Icons; + + Some(match file { + f if self.is_music(f) || self.is_lossless(f) => Icons::Audio.value(), + f if self.is_image(f) => Icons::Image.value(), + f if self.is_video(f) => Icons::Video.value(), + _ => return None, + }) + } +} diff --git a/src/output/icons.rs b/src/output/icons.rs index 6303378..799ead0 100644 --- a/src/output/icons.rs +++ b/src/output/icons.rs @@ -1,7 +1,28 @@ use ansi_term::Style; use fs::File; +use info::filetype::FileExtensions; use output::file_name::FileStyle; +pub trait FileIcon { + fn icon_file(&self, file: &File) -> Option; +} + +pub enum Icons { + Audio, + Image, + Video, +} + +impl Icons { + pub fn value(&self) -> char { + match *self { + Icons::Audio => '\u{f001}', + Icons::Image => '\u{f1c5}', + Icons::Video => '\u{f03d}', + } + } +} + pub fn painted_icon(file: &File, style: &FileStyle) -> String { let file_icon = icon(&file).to_string(); let painted = style.exts @@ -21,15 +42,15 @@ pub fn painted_icon(file: &File, style: &FileStyle) -> String { } fn icon(file: &File) -> char { + let extensions = Box::new(FileExtensions); if file.is_directory() { '\u{f115}' } + else if let Some(icon) = extensions.icon_file(file) { icon } else { - // possible unnecessary clone - if let Some(ext) = file.ext.clone() { + if let Some(ext) = file.ext.as_ref() { match ext.as_str() { "ai" => '\u{e7b4}', "android" => '\u{e70e}', "apple" => '\u{f179}', - "audio" => '\u{f001}', "avro" => '\u{e60b}', "c" => '\u{e61e}', "clj" => '\u{e768}', @@ -52,7 +73,6 @@ fn icon(file: &File) -> char { "go" => '\u{e626}', "hs" => '\u{e777}', "html" => '\u{f13b}', - "image" => '\u{f1c5}', "iml" => '\u{e7b5}', "java" => '\u{e204}', "js" => '\u{e74e}', @@ -87,7 +107,6 @@ fn icon(file: &File) -> char { "txt" => '\u{f15c}', "video" => '\u{f03d}', "vim" => '\u{e62b}', - "windows" => '\u{f17a}', "xls" => '\u{f1c3}', "xml" => '\u{e619}', "yml" => '\u{f481}',