display_tree icon indicating copy to clipboard operation
display_tree copied to clipboard

Does this work for trees where branches contain a recursive list of children?

Open colbyn opened this issue 2 years ago • 3 comments

Something like this doesn't work,

#[derive(Debug, Clone, DisplayTree)]
pub enum Html {
    Text(String),
    Element {
        tag: String,
        /// Can't use this `#[tree]`
        children: Vec<Html>,
    }
}

I'm cloned the repo and looking through the source code to implement the change.

If you don't mind, could you give me some hints for implementing this change?

colbyn avatar Jan 28 '23 07:01 colbyn

This is something that I was planning on implementing eventually, it would be a great feature.

I'm currently working on a 2.0 version of this project that should simplify things quite a bit and make implementing new features easier, so I was waiting on that to make any other major changes. I pushed my current progress to a new branch, but things still might change a decent amount.

The way I would probably go about adding this feature is just by manually implementing DisplayTree for Vec or Iterator, though an attribute of some kind might also work. One thing to consider is how it knows whether to display the Vec's elements as trees or just using Display. I haven't really thought of a good solution for this yet.

Once I get 2.0 closer to complete I could probably come up with some more specific ideas for how to do this.

captain-camel avatar Feb 01 '23 04:02 captain-camel

image

I'm running on this exact issue Is there a way to skip_branch?

EDIT : Ok most recursion or nesting aren't supported from what I see I'll have to just entirely skip this crate for now Sad it looked cool

AuracleTech avatar Mar 13 '24 14:03 AuracleTech

For all its worth I wrote some code that allows for easy implementation of tree printing. @captain-camel Would you be interested in implementing something like this? I might consider making a pull request since I am already implementing this functionality into my interpreter for debugging.

pub trait TreePrinter {
    fn node_children(&self) -> Box<dyn Iterator<Item = Box<dyn TreePrinter>> + '_>;

    fn node(&self) -> Box<dyn Display>;

    fn print_tree_base(&self, prefix: &str, last: bool) -> String {
        let mut result = format!("{}{}{}\n", prefix, if last { "└── " } else { "├── " }, self.node());
        let prefix_child = if last { "    " } else { "│   " };
        let children: Vec<_> = self.node_children().collect();
        for (i, child) in children.iter().enumerate() {
            let last_child = i == children.len() - 1;
            result += &child.print_tree_base(&(prefix.to_owned() + prefix_child), last_child);
        }
        result
    }

    fn print_tree(&self) -> String {
        let len = self.node_children().count();
        let tree = self.node_children().enumerate().map(|(i, child)| {
            let last = len - 1 == i;
            child.print_tree_base("", last)
        }).collect::<String>();
        
        format!("{}\n{}", self.node(), tree)
    }
}

Output Example:

Company
├── HR
│   ├── Recruitment
│   └── Employee Relations
├── Engineering
│   ├── Software Development
│   │   ├── Backend Team
│   │   │   ├── API Development
│   │   │   └── Database Management
│   │   ├── Middleware Team
│   │   │   ├── Integration Solutions
│   │   │   └── Message Brokering
│   │   └── Frontend Team
│   │       ├── React Development
│   │       └── Vue Development
│   └── Quality Assurance
│       ├── Automated Testing
│       └── Manual Testing
└── Marketing
    ├── Digital
    └── Print

snowfoxsh avatar Mar 31 '24 20:03 snowfoxsh