Skip to content

Tree

KeyAction
/ kMove up
/ jMove down
/ lExpand node or move into first child
/ hCollapse node or move to parent
Home / gJump to first node
End / GJump to last node
PgDnPage down
PgUpPage up
Ctrl+DHalf-page down
Ctrl+UHalf-page up
EnterSelect node (fires onSelect)
SpaceToggle selection (multi-select) or toggle expand (single)
oToggle expand/collapse
OExpand all nodes
CCollapse all nodes

Enable with SetMultiSelect(true). In multi-select mode, Space toggles the focused node’s selection and o toggles expand/collapse (both are available simultaneously). Retrieve all selected nodes with GetSelectedNodes().

onHighlight fires on every cursor move; onSelect fires only on Enter. Use onHighlight to drive a detail panel in a master-detail layout.

Tree preview

Tree is a collapsible tree view component.

Constructor func NewTree() *Tree

Usage

root := &components.TreeNode{ID: "root", Label: "Project", Expanded: true}
root.AddChild(&components.TreeNode{ID: "src", Label: "src/", Icon: ""})
root.AddChild(&components.TreeNode{ID: "readme", Label: "README.md"})
tree := components.NewTree().
SetRoot(root).
SetShowLines(true).
SetShowIcons(true).
SetIndentSize(2).
SetMultiSelect(false)
tree.SetOnSelect(func(n *components.TreeNode) {
fmt.Println("selected:", n.Label)
})
fmt.Println(tree != nil)

Methods

Method Signature Description
ClearSelection func (t *Tree) ClearSelection() *Tree ClearSelection clears multi-selection.
CollapseAll func (t *Tree) CollapseAll() *Tree CollapseAll collapses all nodes.
ExpandAll func (t *Tree) ExpandAll() *Tree ExpandAll expands all nodes.
ExpandTo func (t *Tree) ExpandTo(depth int) *Tree ExpandTo expands nodes to the specified depth.
Filter func (t *Tree) Filter(query string) *Tree Filter filters the tree by a query string.
GetSelected func (t *Tree) GetSelected() *TreeNode GetSelected returns the currently highlighted node.
GetSelectedIndex func (t *Tree) GetSelectedIndex() int GetSelectedIndex returns the current cursor index in the flat node list.
GetSelectedNodes func (t *Tree) GetSelectedNodes() []*TreeNode GetSelectedNodes returns all multi-selected nodes.
SetIndentSize func (t *Tree) SetIndentSize(size int) *Tree SetIndentSize sets the indentation per level.
SetLazyLoader func (t *Tree) SetLazyLoader(fn func(node *TreeNode) []*TreeNode) *Tree SetLazyLoader sets a function to load children on demand.
SetMultiSelect func (t *Tree) SetMultiSelect(enable bool) *Tree SetMultiSelect enables/disables multi-selection.
SetOnCollapse func (t *Tree) SetOnCollapse(fn func(node *TreeNode)) *Tree SetOnCollapse sets the callback for when a node is collapsed.
SetOnExpand func (t *Tree) SetOnExpand(fn func(node *TreeNode)) *Tree SetOnExpand sets the callback for when a node is expanded.
SetOnHighlight func (t *Tree) SetOnHighlight(fn func(node *TreeNode)) *Tree SetOnHighlight sets the callback for when the highlighted node changes (j/k navigation).
SetOnSelect func (t *Tree) SetOnSelect(fn func(node *TreeNode)) *Tree SetOnSelect sets the callback for when a node is selected (Enter).
SetRoot func (t *Tree) SetRoot(root *TreeNode) *Tree SetRoot sets the root node of the tree.
SetSelectedIndex func (t *Tree) SetSelectedIndex(index int) *Tree SetSelectedIndex sets the cursor to the given index in the flat node list. The index is clamped to valid bounds. onHighlight fires only when the index actually changes. The scroll offset is adjusted so the new selection is visible on the next draw.
SetShowIcons func (t *Tree) SetShowIcons(show bool) *Tree SetShowIcons enables/disables node icons.
SetShowLines func (t *Tree) SetShowLines(show bool) *Tree SetShowLines enables/disables tree line drawing.

Types

type TreeNode struct TreeNode is a single node in a Tree. Set Expanded to control initial open/closed state. Color 0 uses the default foreground. Data holds arbitrary caller data returned by onSelect and onHighlight callbacks.
FieldTypeDescription
ID string
Label string
Icon string
Color tcell.Color optional label color (zero value = default)
Children []*TreeNode
Data any
Expanded bool