From f3f783a3a0bc3c96e4678c7fa986dc31bcb8d585 Mon Sep 17 00:00:00 2001 From: rabite Date: Fri, 25 Jan 2019 11:14:41 +0100 Subject: [PATCH] colored file names --- Cargo.toml | 3 ++- src/files.rs | 23 +++++++++++++++++++---- src/listview.rs | 22 +++++++++++++--------- src/main.rs | 1 + 4 files changed, 35 insertions(+), 14 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 218ebd5..610b9c9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,5 @@ termion = "*" unicode-width = "0.1.5" lazy_static = "*" x11-clipboard = "*" -alphanumeric-sort = "1.0.6" \ No newline at end of file +alphanumeric-sort = "1.0.6" +lscolors = { version = "0.5.0", features = [ "ansi_term" ] } \ No newline at end of file diff --git a/src/files.rs b/src/files.rs index d1422f5..1192659 100644 --- a/src/files.rs +++ b/src/files.rs @@ -5,12 +5,15 @@ use std::ffi::OsStr; use std::cmp::{Ord, Ordering}; pub struct Files(Vec); +use lscolors::{LsColors, Style}; impl Index for Files { type Output = File; fn index(&self, pos: usize) -> &Self::Output { &self.0[pos] } +lazy_static! { + static ref COLORS: LsColors = LsColors::from_env().unwrap(); } impl PartialOrd for File { @@ -45,8 +48,14 @@ impl Files { let name = name.to_string_lossy(); let kind = get_kind(&file); let path = file.path(); - let size = file.metadata()?.len() / 1024; - let file = File::new(&name, path, kind, size as usize); + let meta = file.metadata()?; + let size = meta.len() / 1024; + let style + = match COLORS.style_for_path_with_metadata(file.path(), Some(&meta)) { + Some(style) => Some(style.clone()), + None => None + }; + let file = File::new(&name, path, kind, size as usize, style); match kind { Kind::Directory => dirs.push(file), _ => files.push(file), @@ -84,6 +93,7 @@ pub struct File { pub path: PathBuf, pub size: Option, pub kind: Kind, + pub style: Option