fliptree

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2024 License: GPL-3.0 Imports: 6 Imported by: 0

Documentation

Overview

Package fliptree renders a tree of nodes as text in reverse: children first, then parent.

For example: given main -> {feat1 -> feat1.1, feat2 -> feat2.1} The tree would look like this:

  ┌── feat2.1
┌─┴ feat2
│ ┌── feat1.1
├─┴ feat1
main

Index

Constants

This section is empty.

Variables

View Source
var DefaultNodeMarker = lipgloss.NewStyle().SetString("□")

DefaultNodeMarker is the marker used for each node in the tree.

Functions

func Write

func Write[T any](w io.Writer, g Graph[T], opts Options[T]) error

Write renders the tree of nodes in g.

Types

type CycleError

type CycleError struct {
	// Nodes that form the cycle.
	Nodes []int
}

CycleError is returned when a cycle is detected in the tree.

func (*CycleError) Error

func (e *CycleError) Error() string

type Graph

type Graph[T any] struct {
	// Values specifies the value for each node in the graph.
	// All nodes must have a value.
	Values []T

	// Edges returns the nodes that are directly reachable
	// from the given node as indexes into the Values slice.
	Edges func(T) []int

	// Roots are indexes of nodes in the Values slice
	// at which we start rendering the tree.
	//
	// Each root will be rendered as a separate tree.
	Roots []int

	// View returns the text for the node.
	// The view for a node may be multiline, and may have its own styling.
	View func(T) string
}

Graph defines a directed graph.

type Options

type Options[T any] struct {
	Style *Style[T]
}

Options configure the rendering of the tree.

type Style

type Style[T any] struct {
	// Joint defines how the connecting joints between nodes are rendered.
	Joint lipgloss.Style

	// NodeMarker returns the style of the marker
	// placed next to each node based on the node's value.
	//
	// By default, all nodes are marked with [DefaultNodeMarker].
	NodeMarker func(T) lipgloss.Style
}

Style configures the visual appearance of the tree.

func DefaultStyle

func DefaultStyle[T any]() *Style[T]

DefaultStyle returns the default style for rendering trees.

Jump to

Keyboard shortcuts

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