treerender

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: May 17, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package treerender renders generic ASCII operator trees with optional wrapping.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ContinuationIndent

type ContinuationIndent int

ContinuationIndent selects how wrapped continuation lines align to the tree rail.

const (
	// ContinuationIndentTree keeps wrapped lines aligned only under the tree prefix.
	ContinuationIndentTree ContinuationIndent = iota
	// ContinuationIndentAnchor hangs continuation lines after a node-local prefix.
	ContinuationIndentAnchor
)

type Node

type Node struct {
	// Text is the node label rendered after the tree prefix.
	Text string
	// Children are the child nodes rendered below this node.
	Children []*Node
}

Node is one vertex in a logical tree rendered as ASCII edges.

type PrefixMetrics

type PrefixMetrics struct {
	// contains filtered or unexported fields
}

PrefixMetrics caches grapheme-aware display widths for a Style so callers that need prefix width at many depths (e.g. plantree wrapping) avoid recomputing tabwrap.StringWidth on every node.

func NewPrefixMetrics

func NewPrefixMetrics(style Style) PrefixMetrics

NewPrefixMetrics precomputes widths for style once; use PrefixMetrics.MaxWidthForDepth per level.

func (PrefixMetrics) MaxWidthForDepth

func (p PrefixMetrics) MaxWidthForDepth(depth int) int

MaxWidthForDepth returns the maximum display width of the prefix added by RenderTree for a node at the given depth. This includes the tree edges and the separator after the edge.

type RenderOptions

type RenderOptions[T any] struct {
	// GetContinuationAnchor returns the node-local prefix used for hanging continuation lines.
	// It is required when [ContinuationIndentAnchor] is selected and [WrapWidth] is positive.
	GetContinuationAnchor func(*T) string
	// WrapWidth sets the maximum total rendered line width. A non-positive value disables wrapping.
	WrapWidth int
	// WrapCondition controls display-width calculation and truncation for wrapped text.
	WrapCondition *tabwrap.Condition
	// ContinuationIndent selects how wrapped continuation lines align.
	ContinuationIndent ContinuationIndent
}

RenderOptions configures the optional wrapping behavior of RenderTreeWithOptions.

type Row

type Row struct {
	// TreePart is everything rendered before NodeText on each visual line: the ASCII tree drawing
	// plus any continuation padding added by the renderer (for example, hanging-indent spacing).
	// It is joined with newlines using the same line structure as strings.Split(NodeText, "\n").
	TreePart string
	// NodeText is the rendered node label, possibly split across visual lines.
	NodeText string
}

Row is one rendered tree row: a tree prefix per visual line plus node text lines.

func Render

func Render(root *Node, style Style) []Row

Render renders a Node tree. It is equivalent to RenderTree on the same structure.

func RenderTree

func RenderTree[T any](root *T, style Style, getText func(*T) string, getChildren func(*T) []*T) []Row

RenderTree walks an existing tree without copying it into Node, using accessors for *T values. A *Node root infers T as Node, so accessors receive *Node rather than **Node.

func RenderTreeWithOptions

func RenderTreeWithOptions[T any](
	root *T,
	style Style,
	getText func(*T) string,
	getChildren func(*T) []*T,
	opts RenderOptions[T],
) ([]Row, error)

RenderTreeWithOptions renders a tree with optional wrapping and continuation-indent behavior. It returns an error if opts contains an invalid ContinuationIndent, or if hanging-indent wrapping is requested without RenderOptions.GetContinuationAnchor.

func (Row) Text

func (r Row) Text() string

Text returns the full rendered row text, with the tree prefix prepended to each node text line. If a manually constructed row has mismatched tree and node line counts, all lines are preserved.

func (Row) TreePartLines

func (r Row) TreePartLines() []string

TreePartLines splits Row.TreePart into one prefix per visual line. Rows produced by this package align these prefixes with the lines in Row.NodeText.

type Style

type Style struct {
	// EdgeLink is the ancestor rail glyph used for rows that have following siblings.
	EdgeLink string
	// EdgeMid is the edge glyph used for non-last children.
	EdgeMid string
	// EdgeEnd is the edge glyph used for last children.
	EdgeEnd string
	// EdgeSeparator is inserted between an edge glyph and node text.
	EdgeSeparator string
	// IndentSize is the number of spaces between ancestor rails.
	IndentSize int
}

Style configures ASCII edge glyphs and indentation between rails.

func CompactStyle

func CompactStyle() Style

CompactStyle returns a compact tree style with minimal edge glyphs.

func DefaultStyle

func DefaultStyle() Style

DefaultStyle returns the default "+-" / "|" tree drawing style.

Jump to

Keyboard shortcuts

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