tree

package
v1.228.0-test.28 Latest Latest
Warning

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

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

Documentation

Overview

Package tree computes the box-drawing gutters ("│", "├── ", "└── ") that connect the rows of a rendered tree, and checks that a rendered tree is fully connected.

Renderers tend to compute these gutters ad hoc at each place they emit a row (the node's own row, rows of content that belong to the node, spacer rows) and drift apart: the classic failure is a node whose content rows sit between its connector and its children's connectors with no rail through them, leaving the children floating. This package owns that geometry in one place, as pure functions of a node's position, so a renderer only has to describe where a row sits and the invariant can be tested directly against generated trees (see Violations).

Index

Constants

View Source
const (
	// ConnectorMore is the connector of a node that has later siblings.
	ConnectorMore = "├── "
	// ConnectorLast is the connector of a node that is the last of its siblings.
	ConnectorLast = "└── "
	// Rail is one level of gutter with rows still to come at that level.
	Rail = "│   "
	// Blank is one level of gutter with nothing further at that level; it has the same
	// width as Rail so deeper columns stay aligned.
	Blank = "    "

	// Width is the display width of every gutter segment above.
	Width = 4
)

Variables

This section is empty.

Functions

func Connector

func Connector(p Path) string

Connector returns the gutter for the node's own row: a rail or blank for each ancestor level, then the node's connector.

func ContentGutter

func ContentGutter(p Path, hasChildren bool) string

ContentGutter returns the gutter for rows of content that belong to the node and sit between the node's row and its children's rows (attribute diffs, descriptions, ...): a rail or blank for every level including the node's own, then one more level that carries a rail down to the children when the node has any, so the first child's connector is never left floating below the content.

func SpacerFromConnectorRow

func SpacerFromConnectorRow(row string) string

SpacerFromConnectorRow derives the spacer row that belongs in place of a rendered tree row whose connector introduces a spacer placeholder (a blank line between sibling blocks).

The row is the rendered row with ANSI styling stripped. Everything before the connector -- the ancestor rails -- is kept as is; the connector itself becomes a rail when it is "├" (rows still follow at that level) and nothing when it is "└"; the connector's arm and the placeholder text are dropped. This is SpacerGutter for renderers that emit spacers as tree nodes (lipgloss/tree) rather than tracking a Path themselves. A row without a connector has no tree position and yields an empty spacer.

func SpacerGutter

func SpacerGutter(p Path) string

SpacerGutter returns the gutter for an otherwise empty spacer row emitted after the node's whole block (its content and children), before its next sibling: the rails that still have rows to come, with trailing padding trimmed. A node that is the last of its siblings has no rail of its own to carry.

func Violations

func Violations(rows []string) []string

Violations checks that a rendered tree is connected and returns one message per break.

The rows are the rendered lines with any ANSI styling already stripped; any fixed leading column (an action symbol, indentation) is fine as long as it is the same width on every row. The invariant: every box-drawing character must have, on the row directly above it, either a box-drawing character in the same column (the rail it continues) or a connector one arm's width to the left (the parent it hangs from; both this package's four-column segments and lipgloss/tree's three-column enumerator are recognized). The one exception is a root-level connector -- one in the tree's leftmost box-drawing column -- which may sit under a row with no tree characters at all, such as a header. Nothing deeper may: a nested connector or a rail under a bare row is exactly a broken rail.

Types

type Path

type Path []bool

Path is a node's position in a tree, one entry per level from the root's children down to the node itself: Path[i] reports whether the node's ancestor at depth i (the node itself for the final entry) is the last among its siblings.

Jump to

Keyboard shortcuts

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