Documentation
¶
Overview ¶
Package tree draws hierarchical rows as an ASCII or Unicode tree.
Callers render their own row bodies and hand them to this package together with structural flags (the parent's stem, whether the row is the last child). The package supplies the connector glyphs, the dim styling from the current theme, and the gap stems that keep vertical rules unbroken when a list widget inserts blank lines between rows.
A tree is built from the top down: a trunk row the caller renders itself, then one Branch per child of the trunk, then Leaf rows under each branch using that branch's stem.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Connectors ¶
type Connectors struct {
// Trunk marks the root of the tree.
Trunk string
// Mid joins a child that still has siblings below it.
Mid string
// End joins the last child of a parent.
End string
// Vert continues the parent's rule past a Mid child.
Vert string
// Space blanks the parent's rule past an End child.
Space string
// Empty marks a parent that has no children at all.
Empty string
}
Connectors is the glyph set used to draw tree edges. Mid, End, Vert and Space must all have the same display width so that columns line up.
func DefaultConnectors ¶
func DefaultConnectors() Connectors
DefaultConnectors returns the connector set for the current glyph mode: a plain ASCII set when glyphs are off, box-drawing characters otherwise.
func (Connectors) Edge ¶
func (c Connectors) Edge(last bool) (connector, stem string)
Edge returns the connector that joins a child to its parent and the stem that continues underneath that child. The last child closes the rule with End and leaves blank space below it; every other child keeps the rule running.
func (Connectors) Indent ¶
func (c Connectors) Indent(stem string) int
Indent reports the display width a child's prefix occupies when it hangs off stem. Subtract it from the available width before rendering child bodies so they wrap inside the space the tree leaves them.
type Row ¶
type Row struct {
// Lines are the rendered lines of the row, connectors included.
Lines []string
// Key identifies the row to the caller, for example the URL a leaf opens.
// An empty Key marks a structural row that carries no payload.
Key string
// Selectable reports whether a cursor may land on the row.
Selectable bool
// GapStem is the stem to repeat in blank lines drawn after the row, so the
// tree's vertical rules survive the gap. It matches the field of the same
// name on list.Item.
GapStem string
// Payload carries the caller's domain value for the row; Item copies it
// through so list selection hands the value back without a side table.
Payload any
}
Row is one rendered node of a tree: its lines already carry the connector prefixes and are ready to print.
func Branch ¶
func Branch(c Connectors, last bool, body []string, key string) Row
Branch renders body as a direct child of the trunk: a Leaf with no parent stem. Its GapStem is the stem its own children must hang off, so pass the unstyled result of Edge to those Leaf calls.
func Leaf ¶
Leaf renders body as a child hanging off stem, the accumulated prefix of the parent's own connectors. The first body line is joined with the child's connector; later lines are indented with the continuation stem so a multi-line body stays under its connector. A non-empty key makes the row selectable.