From 5bc5c3f0b8fa89c1f09e1061c0fc6eef84ef405c Mon Sep 17 00:00:00 2001 From: Abin Simon Date: Sat, 26 Jan 2019 12:39:22 +0530 Subject: [PATCH 1/2] color files with lscolors --- Cargo.lock | 10 +++++++ Cargo.toml | 1 + src/color.rs | 73 +++++++++++++++++++++++++++++++++++++++++++++++- src/main.rs | 1 + src/meta/name.rs | 9 ++++-- 5 files changed, 90 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index f9107ee97..ca7566240 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -82,6 +82,14 @@ name = "libc" version = "0.2.44" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "lscolors" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "ansi_term 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "lsd" version = "0.12.1-pre" @@ -90,6 +98,7 @@ dependencies = [ "chrono-humanize 0.0.11 (registry+https://github.com/rust-lang/crates.io-index)", "clap 2.32.0 (registry+https://github.com/rust-lang/crates.io-index)", "libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)", + "lscolors 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)", "tempdir 0.3.7 (registry+https://github.com/rust-lang/crates.io-index)", "term_grid 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)", "terminal_size 0.1.8 (registry+https://github.com/rust-lang/crates.io-index)", @@ -276,6 +285,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum kernel32-sys 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7507624b29483431c0ba2d82aece8ca6cdba9382bff4ddd0f7490560c056098d" "checksum libc 0.2.44 (registry+https://github.com/rust-lang/crates.io-index)" = "10923947f84a519a45c8fefb7dd1b3e8c08747993381adee176d7a82b4195311" +"checksum lscolors 0.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e9938fd8c379393454f73ec4c9c5b40f3d8332d80b25a29da05e41ee0ecbb559" "checksum num-integer 0.1.39 (registry+https://github.com/rust-lang/crates.io-index)" = "e83d528d2677f0518c570baf2b7abdcf0cd2d248860b68507bdcb3e91d4c0cea" "checksum num-traits 0.2.6 (registry+https://github.com/rust-lang/crates.io-index)" = "0b3a5d7cc97d6d30d8b9bc8fa19bf45349ffe46241e8816f50f62f6d6aaabee1" "checksum rand 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "8356f47b32624fef5b3301c1be97e5944ecdd595409cc5da11d05f211db6cfbd" diff --git a/Cargo.toml b/Cargo.toml index d7c5a037d..96e6d7fd7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,6 +28,7 @@ time = "0.1.40" users = "0.8.0" chrono-humanize = "0.0.11" unicode-width = "0.1.5" +lscolors = "0.5.0" [dependencies.clap] features = ["suggestions", "color", "wrap_help"] diff --git a/src/color.rs b/src/color.rs index 4587e3673..baad8ac65 100644 --- a/src/color.rs +++ b/src/color.rs @@ -1,4 +1,5 @@ use ansi_term::{ANSIString, Colour, Style}; +use lscolors::{Indicator, LsColors}; use std::collections::HashMap; #[allow(dead_code)] @@ -62,6 +63,7 @@ pub enum Theme { pub struct Colors { colors: Option>, + lscolors: Option, } impl Colors { @@ -70,15 +72,55 @@ impl Colors { Theme::NoColor => None, Theme::Default => Some(Self::get_light_theme_colour_map()), }; + let lscolors = LsColors::from_env(); - Self { colors } + Self { colors, lscolors } } pub fn colorize<'a>(&self, input: String, elem: &Elem) -> ColoredString<'a> { self.style(elem).paint(input) } + pub fn colorize_using_path<'a>( + &self, + input: String, + path: &str, + elem: &Elem, + ) -> ColoredString<'a> { + let style_from_path = self.style_from_path(path); + match style_from_path { + Some(style_from_path) => style_from_path.paint(input), + None => self.colorize(input, elem), + } + } + + fn style_from_path(&self, path: &str) -> Option