@@ -16,7 +16,7 @@ pub struct Tree<D: Display> {
16
16
pub root : D ,
17
17
pub leaves : Vec < Tree < D > > ,
18
18
multiline : bool ,
19
- glyphs : GlyphPalette ,
19
+ glyphs : Option < GlyphPalette > ,
20
20
}
21
21
22
22
impl < D : Display > Tree < D > {
@@ -25,7 +25,7 @@ impl<D: Display> Tree<D> {
25
25
root,
26
26
leaves : Vec :: new ( ) ,
27
27
multiline : false ,
28
- glyphs : GlyphPalette :: new ( ) ,
28
+ glyphs : None ,
29
29
}
30
30
}
31
31
@@ -42,7 +42,7 @@ impl<D: Display> Tree<D> {
42
42
43
43
/// Customize the rendering of this node
44
44
pub fn with_glyphs ( mut self , glyphs : GlyphPalette ) -> Self {
45
- self . glyphs = glyphs;
45
+ self . glyphs = Some ( glyphs) ;
46
46
self
47
47
}
48
48
}
@@ -56,7 +56,7 @@ impl<D: Display> Tree<D> {
56
56
57
57
/// Customize the rendering of this node
58
58
pub fn set_glyphs ( & mut self , glyphs : GlyphPalette ) -> & mut Self {
59
- self . glyphs = glyphs;
59
+ self . glyphs = Some ( glyphs) ;
60
60
self
61
61
}
62
62
}
@@ -92,25 +92,27 @@ impl<D: Display> Display for Tree<D> {
92
92
writeln ! ( f) ?;
93
93
let mut queue = DisplauQueue :: new ( ) ;
94
94
let no_space = Rc :: new ( Vec :: new ( ) ) ;
95
- enqueue_leaves ( & mut queue, self , no_space) ;
96
- while let Some ( ( last, leaf, spaces) ) = queue. pop_front ( ) {
95
+ let default_glyphs = GlyphPalette :: new ( ) ;
96
+ let glyphs = self . glyphs . as_ref ( ) . unwrap_or ( & default_glyphs) ;
97
+ enqueue_leaves ( & mut queue, self , glyphs, no_space) ;
98
+ while let Some ( ( last, leaf, glyphs, spaces) ) = queue. pop_front ( ) {
97
99
let mut prefix = (
98
100
if last {
99
- leaf . glyphs . last_item
101
+ glyphs. last_item
100
102
} else {
101
- leaf . glyphs . middle_item
103
+ glyphs. middle_item
102
104
} ,
103
- leaf . glyphs . item_indent ,
105
+ glyphs. item_indent ,
104
106
) ;
105
107
106
108
if leaf. multiline {
107
109
let rest_prefix = (
108
110
if last {
109
- leaf . glyphs . last_skip
111
+ glyphs. last_skip
110
112
} else {
111
- leaf . glyphs . middle_skip
113
+ glyphs. middle_skip
112
114
} ,
113
- leaf . glyphs . skip_indent ,
115
+ glyphs. skip_indent ,
114
116
) ;
115
117
debug_assert_eq ! ( prefix. 0 . chars( ) . count( ) , rest_prefix. 0 . chars( ) . count( ) ) ;
116
118
debug_assert_eq ! ( prefix. 1 . chars( ) . count( ) , rest_prefix. 1 . chars( ) . count( ) ) ;
@@ -149,28 +151,30 @@ impl<D: Display> Display for Tree<D> {
149
151
let s: & Vec < SpacePalette > = & spaces;
150
152
let mut child_spaces = s. clone ( ) ;
151
153
child_spaces. push ( if last {
152
- leaf . glyphs . last_space ( )
154
+ glyphs. last_space ( )
153
155
} else {
154
- leaf . glyphs . middle_space ( )
156
+ glyphs. middle_space ( )
155
157
} ) ;
156
158
let child_spaces = Rc :: new ( child_spaces) ;
157
- enqueue_leaves ( & mut queue, leaf, child_spaces) ;
159
+ enqueue_leaves ( & mut queue, leaf, glyphs , child_spaces) ;
158
160
}
159
161
}
160
162
Ok ( ( ) )
161
163
}
162
164
}
163
165
164
- type DisplauQueue < ' t , D > = VecDeque < ( bool , & ' t Tree < D > , Rc < Vec < SpacePalette > > ) > ;
166
+ type DisplauQueue < ' t , D > = VecDeque < ( bool , & ' t Tree < D > , & ' t GlyphPalette , Rc < Vec < SpacePalette > > ) > ;
165
167
166
168
fn enqueue_leaves < ' t , D : Display > (
167
169
queue : & mut DisplauQueue < ' t , D > ,
168
170
parent : & ' t Tree < D > ,
171
+ parent_glyphs : & ' t GlyphPalette ,
169
172
spaces : Rc < Vec < SpacePalette > > ,
170
173
) {
171
174
for ( i, leaf) in parent. leaves . iter ( ) . rev ( ) . enumerate ( ) {
172
175
let last = i == 0 ;
173
- queue. push_front ( ( last, leaf, spaces. clone ( ) ) ) ;
176
+ let glyphs = leaf. glyphs . as_ref ( ) . unwrap_or ( parent_glyphs) ;
177
+ queue. push_front ( ( last, leaf, glyphs, spaces. clone ( ) ) ) ;
174
178
}
175
179
}
176
180
0 commit comments