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 : LsColors ,
6567}
6668
6769impl Colors {
@@ -70,24 +72,83 @@ impl Colors {
7072 Theme :: NoColor => None ,
7173 Theme :: Default => Some ( Self :: get_light_theme_colour_map ( ) ) ,
7274 } ;
75+ let lscolors = LsColors :: from_env ( ) . unwrap_or_default ( ) ;
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 > ( & self , input : String , path : & str ) -> ColoredString < ' a > {
85+ self . style_from_path ( path) . paint ( input)
86+ }
87+
88+ fn style_from_path ( & self , path : & str ) -> Style {
89+ let style = self . lscolors . style_for_path ( path) ;
90+ style
91+ . map ( lscolors:: Style :: to_ansi_term_style)
92+ . unwrap_or_default ( )
93+ }
94+
8195 fn style ( & self , elem : & Elem ) -> Style {
82- if let Some ( ref colors) = self . colors {
83- let style_fg = Style :: default ( ) . fg ( colors[ elem] ) ;
84- if elem. has_suid ( ) {
85- style_fg. on ( Colour :: Fixed ( 124 ) ) // Red3
86- } else {
87- style_fg
96+ let indicator = self . get_indicator_from_elem ( elem) ;
97+ match indicator {
98+ Some ( style) => {
99+ let style = self . lscolors . style_for_indicator ( style) ;
100+ let ans = style
101+ . map ( lscolors:: Style :: to_ansi_term_style)
102+ . unwrap_or_default ( ) ;
103+ ans
104+ }
105+ None => {
106+ if let Some ( ref colors) = self . colors {
107+ let style_fg = Style :: default ( ) . fg ( colors[ elem] ) ;
108+ if elem. has_suid ( ) {
109+ style_fg. on ( Colour :: Fixed ( 124 ) ) // Red3
110+ } else {
111+ style_fg
112+ }
113+ } else {
114+ Style :: default ( )
115+ }
116+ }
117+ }
118+ }
119+
120+ fn get_indicator_from_elem ( & self , elem : & Elem ) -> Option < Indicator > {
121+ let indicator_string = match elem {
122+ Elem :: File { exec, uid } => {
123+ if !uid {
124+ if !exec {
125+ Some ( "fi" )
126+ } else {
127+ Some ( "ex" )
128+ }
129+ } else {
130+ None
131+ }
88132 }
89- } else {
90- Style :: default ( )
133+ Elem :: Dir { uid } => {
134+ if !uid {
135+ Some ( "di" )
136+ } else {
137+ None
138+ }
139+ }
140+ Elem :: SymLink => Some ( "ln" ) ,
141+ Elem :: Pipe => Some ( "pi" ) ,
142+ Elem :: Socket => Some ( "so" ) ,
143+ Elem :: BlockDevice => Some ( "bd" ) ,
144+ Elem :: CharDevice => Some ( "cd" ) ,
145+ Elem :: BrokenSymLink => Some ( "or" ) ,
146+ _ => None ,
147+ } ;
148+
149+ match indicator_string {
150+ Some ( ids) => Indicator :: from ( ids) ,
151+ None => None ,
91152 }
92153 }
93154
0 commit comments