@@ -16,20 +16,6 @@ impl<'a> Coloring<'a> {
1616 pub fn new ( ls_colors : LsColors , map : HashMap < Vec < & ' a OsStr > , Color > ) -> Self {
1717 Coloring { ls_colors, map }
1818 }
19-
20- /// Return `(color, ls_colors)` for a node, used to build a colored slice for rendering.
21- pub ( super ) fn node_color (
22- & self ,
23- path_components : & [ & ' a OsStr ] ,
24- has_children : bool ,
25- ) -> Option < ( Color , & LsColors ) > {
26- let color = if has_children {
27- Some ( Color :: Directory )
28- } else {
29- self . map . get ( path_components) . copied ( )
30- } ?;
31- Some ( ( color, & self . ls_colors ) )
32- }
3319}
3420
3521/// The coloring to apply to a node name.
@@ -46,19 +32,15 @@ pub enum Color {
4632}
4733
4834impl Color {
49- // TODO: reconsider the visibility of this function once the TODOs in
50- // `visualizer/methods.rs` have been dealt with.
5135 /// Get the ANSI prefix for this color from the given prefix table.
52- pub ( super ) fn ansi_prefix ( self , prefixes : & LsColors ) -> AnsiPrefix < ' _ > {
36+ fn ansi_prefix ( self , prefixes : & LsColors ) -> AnsiPrefix < ' _ > {
5337 AnsiPrefix ( prefixes. prefix_str ( self ) )
5438 }
5539}
5640
57- // TODO: reconsider the of this struct once the TODOs in
58- // `visualizer/methods.rs` have been dealt with.
5941/// ANSI prefix wrapper for a [`Color`] variant, implements [`Display`].
6042#[ derive( Display ) ]
61- pub ( super ) struct AnsiPrefix < ' a > ( & ' a str ) ;
43+ struct AnsiPrefix < ' a > ( & ' a str ) ;
6244
6345impl AnsiPrefix < ' _ > {
6446 /// Returns the reset suffix to emit after this prefix, or `""` if no prefix.
@@ -73,11 +55,9 @@ impl AnsiPrefix<'_> {
7355
7456/// A [`TreeHorizontalSlice`] with its color applied, used for rendering.
7557pub ( super ) struct ColoredTreeHorizontalSlice < ' a > {
76- // TODO: reconsider the following visibilities once the TODOs in
77- // `visualizer/methods.rs` have been dealt with.
78- pub ( super ) slice : TreeHorizontalSlice < String > ,
79- pub ( super ) color : Color ,
80- pub ( super ) ls_colors : & ' a LsColors ,
58+ slice : TreeHorizontalSlice < String > ,
59+ color : Color ,
60+ ls_colors : & ' a LsColors ,
8161}
8262
8363impl fmt:: Display for ColoredTreeHorizontalSlice < ' _ > {
@@ -106,6 +86,37 @@ impl Width for ColoredTreeHorizontalSlice<'_> {
10686 }
10787}
10888
89+ /// Wrap a [`TreeHorizontalSlice`] with color if coloring is available, otherwise return it as-is.
90+ ///
91+ /// Path components are only constructed when coloring is enabled, avoiding
92+ /// unnecessary allocation in the common no-color case.
93+ pub ( super ) fn maybe_colored_slice < ' a , ' b > (
94+ coloring : Option < & ' b Coloring < ' a > > ,
95+ ancestors : impl Iterator < Item = & ' a OsStr > ,
96+ name : & ' a OsStr ,
97+ has_children : bool ,
98+ slice : TreeHorizontalSlice < String > ,
99+ ) -> MaybeColoredTreeHorizontalSlice < ' b > {
100+ let coloring = match coloring {
101+ Some ( coloring) => coloring,
102+ None => return MaybeColoredTreeHorizontalSlice :: Colorless ( slice) ,
103+ } ;
104+ let path_components: Vec < & OsStr > = ancestors. chain ( std:: iter:: once ( name) ) . collect ( ) ;
105+ let color = if has_children {
106+ Some ( Color :: Directory )
107+ } else {
108+ coloring. map . get ( & path_components) . copied ( )
109+ } ;
110+ match color {
111+ Some ( color) => MaybeColoredTreeHorizontalSlice :: Colorful ( ColoredTreeHorizontalSlice {
112+ slice,
113+ color,
114+ ls_colors : & coloring. ls_colors ,
115+ } ) ,
116+ None => MaybeColoredTreeHorizontalSlice :: Colorless ( slice) ,
117+ }
118+ }
119+
109120/// Either a [`TreeHorizontalSlice`] (colorless) or a [`ColoredTreeHorizontalSlice`] (colorful).
110121#[ derive( Display ) ]
111122pub ( super ) enum MaybeColoredTreeHorizontalSlice < ' a > {
0 commit comments