node

package
v0.0.0-...-4a23534 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 12, 2025 License: MIT Imports: 2 Imported by: 0

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

View Source
const DefaultHeight = 24

DefaultHeight is the default terminal height used for auto-layout.

View Source
const DefaultWidth = 80

DefaultWidth is the default terminal width used for auto-layout.

Variables

This section is empty.

Functions

func Layout

func Layout(n *Node, width, height int)

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

func Render

func Render(n *Node) string

Render produces ANSI-styled output from a node tree. Automatically calls Layout if not already calculated.

Example:

node := Style("flex gap-2",
    Style("bold", "Hello"),
    Style("italic", "World"),
)
output := Render(node)
fmt.Print(output)

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

func Style(classes string, children ...any) *Node

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) HasFlex

func (n *Node) HasFlex() bool

HasFlex returns true if this node has flex layout enabled.

func (*Node) IsContainer

func (n *Node) IsContainer() bool

IsContainer returns true if this node has children.

func (*Node) IsLeaf

func (n *Node) IsLeaf() bool

IsLeaf returns true if this node has no children.

func (*Node) Render

func (n *Node) Render() string

Render produces ANSI-styled output from this node tree. Automatically calls Layout with default 80x24 if not already calculated.

Example:

output := Style("bold text-red-500", "Hello").Render()
fmt.Print(output)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL