EdJoPaTo / tui-rs-tree-widget

Tree Widget for ratatui
https://crates.io/crates/tui-tree-widget
MIT License
80 stars 26 forks source link

Nested TreeItem addition #47

Open gclarkjr5 opened 2 weeks ago

gclarkjr5 commented 2 weeks ago

Hi! I'm using this crate as a means to build a File System-like viewer. However, I'm having some trouble and maybe am overcomplicating things. I currently have a vector of TreeItems at the top most level, and upon request, like hitting Enter on a particular TreeItem, I will add children to it. I have this working well at 1 layer (i.e i can add children to a parent), but when trying to create another layer of children to an existing child is when things get tricky. I'm currently attempting to recursively build a nested data structure of TreeItems since the value of a selected node is an array of identifiers leading to that node. Is this the recommended approach or am I missing some existing functionality that would make creating this more straight forward?

An example of what I am trying to create is

[
  "one": [   //clicking on this key will yield the children ["two", "twenty"]
    "two":  [    // clicking on this key will yield children ["three"]
      "three"
    ]
    "twenty"
  ]
  "ten",
]

Appreciate any tips on this!

EdJoPaTo commented 2 weeks ago

Tree structures are surprisingly complicated with Rust. So I fully understand why it feels so hard. I started working on #34 for cases like this, but my whole progress with it got kinda stuck because of rendering glitches with ratatui > 0.26.

In general I would approach a separate data structure and generating the TreeItems on a render (= don't keep them until the next render). In your case the file system already is the separate data structure. So get the open items, and recursively go through the separate data structure, create the children first and then the TreeItem containing them. That seems like a useful approach for me so far. Maybe it helps?