Build a Tree View
TreeViewModel is the tree counterpart to ListViewModel. It flattens
expanded TreeItem nodes into visible items and renders them with the same line
number and viewport primitives used by lists.
from rich.text import Text
from lazy_cuh import LineNumberMode, LineNumberSpec, TreeGuideStyle, TreeItem, TreeViewModel, TreeViewSpec
tree = ( TreeItem( id="guild", label=Text("Guild"), children=(TreeItem(id="general", label=Text("general")),), ),)
view = TreeViewModel( roots=tree, cursor_index=1, spec=TreeViewSpec( width=60, line_numbers=LineNumberSpec(LineNumberMode.RELATIVE), guide_style=TreeGuideStyle.unicode_guides(), ),)
for line in view.rendered_lines(): print(line.plain)
view = view.toggle_current()view = view.expand_current()view = view.collapse_current()The model is intentionally pure. Textual tree widgets should adapt this output instead of owning tree state directly.
TreeGuideStyle controls guide glyphs, markers, and guide colors. It keeps tree
structure styling separate from item labels so guide color does not bleed into
row content.
Tree updates are immutable. toggle_current(), expand_current(), and
collapse_current() return a new TreeViewModel with updated roots. Lower-level
helpers such as set_tree_item_expanded() and toggle_tree_item_expanded() are
available when an app needs to update tree state outside a view model.