Documentation
¶
Overview ¶
Package node provides a tree-based structure for terminal UI composition. Nodes can be composed declaratively and rendered in a single pass.
Index ¶
Constants ¶
const DefaultHeight = 24
DefaultHeight is the default terminal height used for auto-layout.
const DefaultWidth = 80
DefaultWidth is the default terminal width used for auto-layout.
Variables ¶
This section is empty.
Functions ¶
func Layout ¶
Layout calculates positions and dimensions for all nodes in the tree. Call this before Render to compute X, Y, W, H for each node.
Parameters:
- n: root node of the tree
- width: available width in characters
- height: available height in lines
Example:
node := Style("flex gap-2",
Style("bold", "Hello"),
Style("italic", "World"),
)
Layout(node, 80, 24)
// node.Children[0].X, node.Children[0].Y are now set
Types ¶
type Node ¶
type Node struct {
// Classes contains Tailwind-like class string (e.g., "flex gap-2 bold")
Classes string
// Text is the content for leaf nodes. Empty for containers.
Text string
// Children contains child nodes for container elements.
Children []*Node
// Style is the parsed representation of Classes.
// Populated during Layout.
Style style.Style
// Layout properties (calculated by Layout function)
X, Y int // Position relative to parent's content area
W, H int // Dimensions including padding and border
}
Node represents a styled element in the render tree. Can be a leaf node (with Text) or a container (with Children).
func Style ¶
Style creates a new Node with the given classes and children. Children can be strings (converted to text nodes) or *Node.
Example:
// Leaf node with text
node := Style("bold text-red-500", "Hello")
// Container with children
node := Style("flex gap-2",
Style("bg-green-600", "Item 1"),
Style("bg-blue-600", "Item 2"),
)
// Mixed content
node := Style("flex",
"Plain text",
Style("bold", "Styled"),
)
func (*Node) IsContainer ¶
IsContainer returns true if this node has children.