11use ansi_term:: { ANSIString , Colour , Style } ;
2+ use lscolors:: { Indicator , LsColors } ;
23use std:: collections:: HashMap ;
34
45#[ allow( dead_code) ]
@@ -62,6 +63,7 @@ pub enum Theme {
6263
6364pub struct Colors {
6465 colors : Option < HashMap < Elem , Colour > > ,
66+ lscolors : Option < LsColors > ,
6567}
6668
6769impl Colors {
@@ -70,15 +72,55 @@ impl Colors {
7072 Theme :: NoColor => None ,
7173 Theme :: Default => Some ( Self :: get_light_theme_colour_map ( ) ) ,
7274 } ;
75+ let lscolors = LsColors :: from_env ( ) ;
7376
74- Self { colors }
77+ Self { colors, lscolors }
7578 }
7679
7780 pub fn colorize < ' a > ( & self , input : String , elem : & Elem ) -> ColoredString < ' a > {
7881 self . style ( elem) . paint ( input)
7982 }
8083
84+ pub fn colorize_using_path < ' a > (
85+ & self ,
86+ input : String ,
87+ path : & str ,
88+ elem : & Elem ,
89+ ) -> ColoredString < ' a > {
90+ let style_from_path = self . style_from_path ( path) ;
91+ match style_from_path {
92+ Some ( style_from_path) => style_from_path. paint ( input) ,
93+ None => self . colorize ( input, elem) ,
94+ }
95+ }
96+
97+ fn style_from_path ( & self , path : & str ) -> Option < Style > {
98+ match & self . lscolors {
99+ Some ( lscolors) => lscolors
100+ . style_for_path ( path)
101+ . map ( lscolors:: Style :: to_ansi_term_style) ,
102+ None => None ,
103+ }
104+ }
105+
81106 fn style ( & self , elem : & Elem ) -> Style {
107+ match & self . lscolors {
108+ Some ( lscolors) => {
109+ match self . get_indicator_from_elem ( elem) {
110+ Some ( style) => {
111+ let style = lscolors. style_for_indicator ( style) ;
112+ style
113+ . map ( lscolors:: Style :: to_ansi_term_style)
114+ . unwrap_or_default ( )
115+ }
116+ None => self . style_default ( elem) ,
117+ }
118+ }
119+ None => self . style_default ( elem) ,
120+ }
121+ }
122+
123+ fn style_default ( & self , elem : & Elem ) -> Style {
82124 if let Some ( ref colors) = self . colors {
83125 let style_fg = Style :: default ( ) . fg ( colors[ elem] ) ;
84126 if elem. has_suid ( ) {
@@ -91,6 +133,35 @@ impl Colors {
91133 }
92134 }
93135
136+ fn get_indicator_from_elem ( & self , elem : & Elem ) -> Option < Indicator > {
137+ let indicator_string = match elem {
138+ Elem :: File { exec, uid } => match ( exec, uid) {
139+ ( _, true ) => None ,
140+ ( true , false ) => Some ( "ex" ) ,
141+ ( false , false ) => Some ( "fi" ) ,
142+ } ,
143+ Elem :: Dir { uid } => {
144+ if * uid {
145+ None
146+ } else {
147+ Some ( "di" )
148+ }
149+ }
150+ Elem :: SymLink => Some ( "ln" ) ,
151+ Elem :: Pipe => Some ( "pi" ) ,
152+ Elem :: Socket => Some ( "so" ) ,
153+ Elem :: BlockDevice => Some ( "bd" ) ,
154+ Elem :: CharDevice => Some ( "cd" ) ,
155+ Elem :: BrokenSymLink => Some ( "or" ) ,
156+ _ => None ,
157+ } ;
158+
159+ match indicator_string {
160+ Some ( ids) => Indicator :: from ( ids) ,
161+ None => None ,
162+ }
163+ }
164+
94165 // You can find the table for each color, code, and display at:
95166 //
96167 //https://jonasjacek.github.io/colors/
0 commit comments