tree

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 7, 2021 License: MIT Imports: 5 Imported by: 1

README

Pretty Print Trees in Go

Go Reference

Installation

go get github.com/need-being/go-tree

Basic Usage

The following code

package main

import "github.com/need-being/go-tree"

func main() {
	root := tree.New("root")

	str := root.Add("strings")
	str.Add("foo")
	str.Add("bar")

	num := root.Add("numbers")
	floats := num.Add("floats")
	floats.Add(0.5)
	integer := num.Add("integers")
	integer.Add(0)
	integer.Add(42)

	slices := root.Add("slices")
	slices.Add([]string{"hello", "world"})
	slices.Add([]interface{}{42, 0.5, "QED"})

	tree.Print(root)
}

outputs

root
├── strings
│   ├── foo
│   └── bar
├── numbers
│   ├── floats
│   │   └── 0.5
│   └── integers
│       ├── 0
│       └── 42
└── slices
    ├── [hello world]
    └── [42 0.5 QED]

Advanced Usage

The following code

package main

import (
	"fmt"
	"os"

	"github.com/need-being/go-tree"
)

func main() {
	root := tree.New("root")

	root.AddPathString("strings/foo")
	root.AddPathString("strings/bar")

	root.AddPath("numbers", "floats", 0.5)
	root.AddPath("numbers", "integers", 0)
	root.AddPath("numbers", "integers", 42)

	root.Add("QED")

	printer := tree.NewPrinter(os.Stdout, func(n *tree.Node) string {
		if n.Virtual {
			return fmt.Sprintf("%v (virtual)", n.Value)
		}
		return fmt.Sprint(n.Value)
	})
	printer.Print(root)
}

outputs

root
├── strings (virtual)
│   ├── foo
│   └── bar
├── numbers (virtual)
│   ├── floats (virtual)
│   │   └── 0.5
│   └── integers (virtual)
│       ├── 0
│       └── 42
└── QED

Documentation

Index

Constants

View Source
const (
	EdgeEmpty = "    "
	EdgePipe  = "│   "
	EdgeItem  = "├── "
	EdgeLast  = "└── "
)

Box-drawing symbols

Variables

View Source
var DefaultPrinter = NewPrinter(os.Stdout, nil)

DefaultPrinter prints the tree to the stdout with default settings.

Functions

func Print

func Print(root *Node) error

Print prints the tree using the default printer.

Types

type FormaterFunc

type FormaterFunc func(*Node) string

FormaterFunc formats a single node.

type Node

type Node struct {
	Value   interface{}
	Nodes   []*Node
	Virtual bool
}

Node represents a tree node.

func New

func New(value interface{}) *Node

New creates a new tree / root node.

func (*Node) Add

func (n *Node) Add(value interface{}) *Node

Add adds a leaf node

func (*Node) AddPath

func (n *Node) AddPath(values ...interface{}) *Node

AddPath adds a chain of nodes

func (*Node) AddPathString

func (n *Node) AddPathString(path string) *Node

func (*Node) Find

func (n *Node) Find(value interface{}) *Node

Find finds the child node with the target value. Nil if not found.

type Printer

type Printer interface {
	Print(*Node) error
}

Printer prints the tree.

func NewPrinter

func NewPrinter(writer io.Writer, format FormaterFunc) Printer

type PrinterFunc

type PrinterFunc func(*Node) error

PrinterFunc type is an adapter to allow the use of ordinary functions as a printer.

func (PrinterFunc) Print

func (f PrinterFunc) Print(root *Node) error

Jump to

Keyboard shortcuts

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