Skip to content

Commit 87f567d

Browse files
authored
Merge pull request #31 from epage/inherit
feat: Inherit glyphs
2 parents fee7d05 + 5826341 commit 87f567d

File tree

4 files changed

+313
-42
lines changed

4 files changed

+313
-42
lines changed

Cargo.lock

Lines changed: 177 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ pre-release-replacements = [
116116
[dependencies]
117117

118118
[dev-dependencies]
119+
snapbox = "0.6.10"
119120

120121
[lints]
121122
workspace = true

src/lib.rs

Lines changed: 50 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ pub struct Tree<D: Display> {
1616
pub root: D,
1717
pub leaves: Vec<Tree<D>>,
1818
multiline: bool,
19-
glyphs: GlyphPalette,
19+
glyphs: Option<GlyphPalette>,
2020
}
2121

2222
impl<D: Display> Tree<D> {
@@ -25,7 +25,7 @@ impl<D: Display> Tree<D> {
2525
root,
2626
leaves: Vec::new(),
2727
multiline: false,
28-
glyphs: GlyphPalette::new(),
28+
glyphs: None,
2929
}
3030
}
3131

@@ -42,7 +42,7 @@ impl<D: Display> Tree<D> {
4242

4343
/// Customize the rendering of this node
4444
pub fn with_glyphs(mut self, glyphs: GlyphPalette) -> Self {
45-
self.glyphs = glyphs;
45+
self.glyphs = Some(glyphs);
4646
self
4747
}
4848
}
@@ -56,7 +56,7 @@ impl<D: Display> Tree<D> {
5656

5757
/// Customize the rendering of this node
5858
pub fn set_glyphs(&mut self, glyphs: GlyphPalette) -> &mut Self {
59-
self.glyphs = glyphs;
59+
self.glyphs = Some(glyphs);
6060
self
6161
}
6262
}
@@ -92,25 +92,27 @@ impl<D: Display> Display for Tree<D> {
9292
writeln!(f)?;
9393
let mut queue = DisplauQueue::new();
9494
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() {
9799
let mut prefix = (
98100
if last {
99-
leaf.glyphs.last_item
101+
glyphs.last_item
100102
} else {
101-
leaf.glyphs.middle_item
103+
glyphs.middle_item
102104
},
103-
leaf.glyphs.item_indent,
105+
glyphs.item_indent,
104106
);
105107

106108
if leaf.multiline {
107109
let rest_prefix = (
108110
if last {
109-
leaf.glyphs.last_skip
111+
glyphs.last_skip
110112
} else {
111-
leaf.glyphs.middle_skip
113+
glyphs.middle_skip
112114
},
113-
leaf.glyphs.skip_indent,
115+
glyphs.skip_indent,
114116
);
115117
debug_assert_eq!(prefix.0.chars().count(), rest_prefix.0.chars().count());
116118
debug_assert_eq!(prefix.1.chars().count(), rest_prefix.1.chars().count());
@@ -123,13 +125,8 @@ impl<D: Display> Display for Tree<D> {
123125
for line in root.lines() {
124126
// print single line
125127
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)?;
133130
}
134131
prefix.0.fmt(f)?;
135132
prefix.1.fmt(f)?;
@@ -140,13 +137,8 @@ impl<D: Display> Display for Tree<D> {
140137
} else {
141138
// print single line
142139
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)?;
150142
}
151143
prefix.0.fmt(f)?;
152144
prefix.1.fmt(f)?;
@@ -156,30 +148,42 @@ impl<D: Display> Display for Tree<D> {
156148

157149
// recurse
158150
if !leaf.leaves.is_empty() {
159-
let s: &Vec<bool> = &spaces;
151+
let s: &Vec<SpacePalette> = &spaces;
160152
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+
});
162158
let child_spaces = Rc::new(child_spaces);
163-
enqueue_leaves(&mut queue, leaf, child_spaces);
159+
enqueue_leaves(&mut queue, leaf, glyphs, child_spaces);
164160
}
165161
}
166162
Ok(())
167163
}
168164
}
169165

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>>)>;
171167

172168
fn enqueue_leaves<'t, D: Display>(
173169
queue: &mut DisplauQueue<'t, D>,
174170
parent: &'t Tree<D>,
175-
spaces: Rc<Vec<bool>>,
171+
parent_glyphs: &'t GlyphPalette,
172+
spaces: Rc<Vec<SpacePalette>>,
176173
) {
177174
for (i, leaf) in parent.leaves.iter().rev().enumerate() {
178175
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()));
180178
}
181179
}
182180

181+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
182+
struct SpacePalette {
183+
skip: &'static str,
184+
indent: &'static str,
185+
}
186+
183187
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
184188
pub struct GlyphPalette {
185189
pub middle_item: &'static str,
@@ -203,6 +207,20 @@ impl GlyphPalette {
203207
skip_indent: " ",
204208
}
205209
}
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+
}
206224
}
207225

208226
impl Default for GlyphPalette {

0 commit comments

Comments
 (0)