Skip to content

feat: Inherit glyphs #31

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
177 changes: 177 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ pre-release-replacements = [
[dependencies]

[dev-dependencies]
snapbox = "0.6.10"

[lints]
workspace = true
82 changes: 50 additions & 32 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub struct Tree<D: Display> {
pub root: D,
pub leaves: Vec<Tree<D>>,
multiline: bool,
glyphs: GlyphPalette,
glyphs: Option<GlyphPalette>,
}

impl<D: Display> Tree<D> {
Expand All @@ -25,7 +25,7 @@ impl<D: Display> Tree<D> {
root,
leaves: Vec::new(),
multiline: false,
glyphs: GlyphPalette::new(),
glyphs: None,
}
}

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

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

/// Customize the rendering of this node
pub fn set_glyphs(&mut self, glyphs: GlyphPalette) -> &mut Self {
self.glyphs = glyphs;
self.glyphs = Some(glyphs);
self
}
}
Expand Down Expand Up @@ -92,25 +92,27 @@ impl<D: Display> Display for Tree<D> {
writeln!(f)?;
let mut queue = DisplauQueue::new();
let no_space = Rc::new(Vec::new());
enqueue_leaves(&mut queue, self, no_space);
while let Some((last, leaf, spaces)) = queue.pop_front() {
let default_glyphs = GlyphPalette::new();
let glyphs = self.glyphs.as_ref().unwrap_or(&default_glyphs);
enqueue_leaves(&mut queue, self, glyphs, no_space);
while let Some((last, leaf, glyphs, spaces)) = queue.pop_front() {
let mut prefix = (
if last {
leaf.glyphs.last_item
glyphs.last_item
} else {
leaf.glyphs.middle_item
glyphs.middle_item
},
leaf.glyphs.item_indent,
glyphs.item_indent,
);

if leaf.multiline {
let rest_prefix = (
if last {
leaf.glyphs.last_skip
glyphs.last_skip
} else {
leaf.glyphs.middle_skip
glyphs.middle_skip
},
leaf.glyphs.skip_indent,
glyphs.skip_indent,
);
debug_assert_eq!(prefix.0.chars().count(), rest_prefix.0.chars().count());
debug_assert_eq!(prefix.1.chars().count(), rest_prefix.1.chars().count());
Expand All @@ -123,13 +125,8 @@ impl<D: Display> Display for Tree<D> {
for line in root.lines() {
// print single line
for s in spaces.as_slice() {
if *s {
self.glyphs.last_skip.fmt(f)?;
self.glyphs.skip_indent.fmt(f)?;
} else {
self.glyphs.middle_skip.fmt(f)?;
self.glyphs.skip_indent.fmt(f)?;
}
s.skip.fmt(f)?;
s.indent.fmt(f)?;
}
prefix.0.fmt(f)?;
prefix.1.fmt(f)?;
Expand All @@ -140,13 +137,8 @@ impl<D: Display> Display for Tree<D> {
} else {
// print single line
for s in spaces.as_slice() {
if *s {
self.glyphs.last_skip.fmt(f)?;
self.glyphs.skip_indent.fmt(f)?;
} else {
self.glyphs.middle_skip.fmt(f)?;
self.glyphs.skip_indent.fmt(f)?;
}
s.skip.fmt(f)?;
s.indent.fmt(f)?;
}
prefix.0.fmt(f)?;
prefix.1.fmt(f)?;
Expand All @@ -156,30 +148,42 @@ impl<D: Display> Display for Tree<D> {

// recurse
if !leaf.leaves.is_empty() {
let s: &Vec<bool> = &spaces;
let s: &Vec<SpacePalette> = &spaces;
let mut child_spaces = s.clone();
child_spaces.push(last);
child_spaces.push(if last {
glyphs.last_space()
} else {
glyphs.middle_space()
});
let child_spaces = Rc::new(child_spaces);
enqueue_leaves(&mut queue, leaf, child_spaces);
enqueue_leaves(&mut queue, leaf, glyphs, child_spaces);
}
}
Ok(())
}
}

type DisplauQueue<'t, D> = VecDeque<(bool, &'t Tree<D>, Rc<Vec<bool>>)>;
type DisplauQueue<'t, D> = VecDeque<(bool, &'t Tree<D>, &'t GlyphPalette, Rc<Vec<SpacePalette>>)>;

fn enqueue_leaves<'t, D: Display>(
queue: &mut DisplauQueue<'t, D>,
parent: &'t Tree<D>,
spaces: Rc<Vec<bool>>,
parent_glyphs: &'t GlyphPalette,
spaces: Rc<Vec<SpacePalette>>,
) {
for (i, leaf) in parent.leaves.iter().rev().enumerate() {
let last = i == 0;
queue.push_front((last, leaf, spaces.clone()));
let glyphs = leaf.glyphs.as_ref().unwrap_or(parent_glyphs);
queue.push_front((last, leaf, glyphs, spaces.clone()));
}
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
struct SpacePalette {
skip: &'static str,
indent: &'static str,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub struct GlyphPalette {
pub middle_item: &'static str,
Expand All @@ -203,6 +207,20 @@ impl GlyphPalette {
skip_indent: " ",
}
}

fn middle_space(&self) -> SpacePalette {
SpacePalette {
skip: self.middle_skip,
indent: self.skip_indent,
}
}

fn last_space(&self) -> SpacePalette {
SpacePalette {
skip: self.last_skip,
indent: self.skip_indent,
}
}
}

impl Default for GlyphPalette {
Expand Down
Loading
Loading