Skip to content
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

Nested TreeItem addition #47

Open
gclarkjr5 opened this issue Nov 12, 2024 · 2 comments
Open

Nested TreeItem addition #47

gclarkjr5 opened this issue Nov 12, 2024 · 2 comments

Comments

@gclarkjr5
Copy link

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
Copy link
Owner

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?

@gclarkjr5
Copy link
Author

Thanks for the response! I actually got something working based on your feedback. I also took some ideas I saw in the MQTT implementation of this tree-widget that you did. I'm now creating the persistent tree data structure using the ego-tree crate. Then using that, I create my tui-tree-widget items anytime children of a directory are requested. Maybe its a bit inefficient at the moment to completely re-create the tui-tree-widget anytime a change happens in the ego-tree data structure, but for the time being it seems to be fast enough, and works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants