tree

package
v0.26.0 Latest Latest
Warning

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

Go to latest
Published: Nov 22, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type LeafPathPartsRenderFunc

type LeafPathPartsRenderFunc[T any] func(leaf T) []string

LeafPathPartsRenderFunc is a function that takes a leaf and returns a slice of strings. The strings are all the path parts from the root to the leaf. Each string is the rendered version of a path part.

func NewStringSplitRenderFunc

func NewStringSplitRenderFunc(sep string) LeafPathPartsRenderFunc[string]

NewStringSplitRenderFunc returns a LeafPathPartsRenderFunc that splits a string by the given separator.

For example, if you use a separator of "/", the render function will split the leaf string by "/" and return the resulting slice.

type Tree

type Tree[T any] struct {
	// contains filtered or unexported fields
}

func New

func New[T any](leaves []T, renderFunc LeafPathPartsRenderFunc[T]) (*Tree[T], error)

New returns a new Tree instance that's ready to be rendered.

It takes two parameters: leaves and renderFunc. The resulting tree will have one leaf per element in the leaves slice, and each leaf's ancestors are inferred from the slice returned by the renderFunc. When the tree is rendered, the renderFunc is called once per leaf, and it's expected to return a slice of strings, each of which are rendered versions of the leaf's ancestors and the leaf itself.

For example, let's say we use a leaves slice of []string{"ABC", "ABD", "BCD"} and a renderFunc implementation that returns a slice of the individual characters from the leaf string. The resulting tree would look like this:

	 A
	 └─ B
		  C
		  D
	 B
	 └─ C
          D

func (Tree[T]) Render

func (t Tree[T]) Render() string

Render returns a string rendering of the tree.

Jump to

Keyboard shortcuts

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