11use crate :: visualizer:: coloring:: Color ;
2+ #[ cfg( feature = "color" ) ]
23use lscolors:: { self , Indicator } ;
34
45/// ANSI color prefix strings for terminal output, initialized from the `LS_COLORS` environment
56/// variable.
67#[ derive( Debug , Clone ) ]
78pub struct LsColors {
9+ #[ cfg( feature = "color" ) ]
810 directory : String ,
11+ #[ cfg( feature = "color" ) ]
912 normal : String ,
13+ #[ cfg( feature = "color" ) ]
1014 executable : String ,
15+ #[ cfg( feature = "color" ) ]
1116 symlink : String ,
1217}
1318
1419impl LsColors {
1520 /// Initialize by reading the current environment's `LS_COLORS`.
1621 pub fn from_env ( ) -> Self {
17- Self :: from_ls_colors_crate ( & lscolors:: LsColors :: from_env ( ) . unwrap_or_default ( ) )
22+ #[ cfg( feature = "color" ) ]
23+ return Self :: from_ls_colors_crate ( & lscolors:: LsColors :: from_env ( ) . unwrap_or_default ( ) ) ;
24+ #[ cfg( not( feature = "color" ) ) ]
25+ LsColors { }
1826 }
1927
2028 /// Parse an `LS_COLORS`-format string into an [`LsColors`].
2129 ///
2230 /// Unrecognized or invalid entries are silently ignored.
2331 pub fn from_ls_colors_string ( input : & str ) -> Self {
24- Self :: from_ls_colors_crate ( & lscolors:: LsColors :: from_string ( input) )
32+ #[ cfg( feature = "color" ) ]
33+ return Self :: from_ls_colors_crate ( & lscolors:: LsColors :: from_string ( input) ) ;
34+ #[ cfg( not( feature = "color" ) ) ]
35+ {
36+ let _ = input;
37+ LsColors { }
38+ }
2539 }
2640
41+ #[ cfg( feature = "color" ) ]
2742 fn from_ls_colors_crate ( ls_colors : & lscolors:: LsColors ) -> Self {
2843 let prefix_for = |indicator : Indicator | {
2944 ls_colors
@@ -41,11 +56,17 @@ impl LsColors {
4156
4257 /// Return the ANSI prefix string for the given [`Color`] variant.
4358 pub ( crate ) fn prefix_str ( & self , color : Color ) -> & str {
44- match color {
59+ #[ cfg( feature = "color" ) ]
60+ return match color {
4561 Color :: Directory => & self . directory ,
4662 Color :: Normal => & self . normal ,
4763 Color :: Executable => & self . executable ,
4864 Color :: Symlink => & self . symlink ,
65+ } ;
66+ #[ cfg( not( feature = "color" ) ) ]
67+ {
68+ let _ = color;
69+ ""
4970 }
5071 }
5172}
0 commit comments