@@ -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( ) ) ;
@@ -123,13 +125,8 @@ impl<D: Display> Display for Tree<D> {
123
125
for line in root. lines ( ) {
124
126
// print single line
125
127
for s in spaces. as_slice ( ) {
126
- if * s {
127
- self . glyphs . last_skip . fmt ( f) ?;
128
- self . glyphs . skip_indent . fmt ( f) ?;
129
- } else {
130
- self . glyphs . middle_skip . fmt ( f) ?;
131
- self . glyphs . skip_indent . fmt ( f) ?;
132
- }
128
+ s. skip . fmt ( f) ?;
129
+ s. indent . fmt ( f) ?;
133
130
}
134
131
prefix. 0 . fmt ( f) ?;
135
132
prefix. 1 . fmt ( f) ?;
@@ -140,13 +137,8 @@ impl<D: Display> Display for Tree<D> {
140
137
} else {
141
138
// print single line
142
139
for s in spaces. as_slice ( ) {
143
- if * s {
144
- self . glyphs . last_skip . fmt ( f) ?;
145
- self . glyphs . skip_indent . fmt ( f) ?;
146
- } else {
147
- self . glyphs . middle_skip . fmt ( f) ?;
148
- self . glyphs . skip_indent . fmt ( f) ?;
149
- }
140
+ s. skip . fmt ( f) ?;
141
+ s. indent . fmt ( f) ?;
150
142
}
151
143
prefix. 0 . fmt ( f) ?;
152
144
prefix. 1 . fmt ( f) ?;
@@ -156,30 +148,42 @@ impl<D: Display> Display for Tree<D> {
156
148
157
149
// recurse
158
150
if !leaf. leaves . is_empty ( ) {
159
- let s: & Vec < bool > = & spaces;
151
+ let s: & Vec < SpacePalette > = & spaces;
160
152
let mut child_spaces = s. clone ( ) ;
161
- child_spaces. push ( last) ;
153
+ child_spaces. push ( if last {
154
+ glyphs. last_space ( )
155
+ } else {
156
+ glyphs. middle_space ( )
157
+ } ) ;
162
158
let child_spaces = Rc :: new ( child_spaces) ;
163
- enqueue_leaves ( & mut queue, leaf, child_spaces) ;
159
+ enqueue_leaves ( & mut queue, leaf, glyphs , child_spaces) ;
164
160
}
165
161
}
166
162
Ok ( ( ) )
167
163
}
168
164
}
169
165
170
- type DisplauQueue < ' t , D > = VecDeque < ( bool , & ' t Tree < D > , Rc < Vec < bool > > ) > ;
166
+ type DisplauQueue < ' t , D > = VecDeque < ( bool , & ' t Tree < D > , & ' t GlyphPalette , Rc < Vec < SpacePalette > > ) > ;
171
167
172
168
fn enqueue_leaves < ' t , D : Display > (
173
169
queue : & mut DisplauQueue < ' t , D > ,
174
170
parent : & ' t Tree < D > ,
175
- spaces : Rc < Vec < bool > > ,
171
+ parent_glyphs : & ' t GlyphPalette ,
172
+ spaces : Rc < Vec < SpacePalette > > ,
176
173
) {
177
174
for ( i, leaf) in parent. leaves . iter ( ) . rev ( ) . enumerate ( ) {
178
175
let last = i == 0 ;
179
- 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 ( ) ) ) ;
180
178
}
181
179
}
182
180
181
+ #[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
182
+ struct SpacePalette {
183
+ skip : & ' static str ,
184
+ indent : & ' static str ,
185
+ }
186
+
183
187
#[ derive( Copy , Clone , Debug , PartialEq , Eq ) ]
184
188
pub struct GlyphPalette {
185
189
pub middle_item : & ' static str ,
@@ -203,6 +207,20 @@ impl GlyphPalette {
203
207
skip_indent : " " ,
204
208
}
205
209
}
210
+
211
+ fn middle_space ( & self ) -> SpacePalette {
212
+ SpacePalette {
213
+ skip : self . middle_skip ,
214
+ indent : self . skip_indent ,
215
+ }
216
+ }
217
+
218
+ fn last_space ( & self ) -> SpacePalette {
219
+ SpacePalette {
220
+ skip : self . last_skip ,
221
+ indent : self . skip_indent ,
222
+ }
223
+ }
206
224
}
207
225
208
226
impl Default for GlyphPalette {
0 commit comments