gree

package module
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2024 License: MIT Imports: 9 Imported by: 0

README

gree Go Reference

Build nodes with children and display them like the classic tree command shows dirs

Example

package main

import (
	"fmt"

	"github.com/rendicott/gree"
)

func main() {
	a := gree.NewNode("root")
	a.NewChild("child1").NewChild("grandchild1")
	a.NewChild("child2").NewChild("grandchild2")
	a.NewChild("child3").NewChild("grandchild3")
	a.NewChild("child4")
	a.NewChild("child5")
	// add child to 1st grandchild of 2nd child of root
	a.GetChild(1).GetChild(0).NewChild("whoops")

	// integrate a new tree
	b := gree.NewNode("extended")
	b.NewChild("puppy1").NewChild("grandpuppy1")
	b.NewChild("puppy2").NewChild("grandpuppy2")

	a.GetChild(2).AddChild(b)

	fmt.Println(a.Draw())
}

Outputs:

root
├── child1
│   └── grandchild1
├── child2
│   └── grandchild2
│       └── whoops
├── child3
│   ├── grandchild3
│   └── extended
│       ├── puppy1
│       │   └── grandpuppy1
│       └── puppy2
│           └── grandpuppy2
├── child4
└── child5

More examples can be found in ./examples.

Documentation

Overview

Package gree provides a Node struct to which children can be retrieved and added. Calling the Draw() method on a Node returns the 'tree' like string representation of the Node and its children

Example:

 func main() {
	 a := gree.NewNode("root")
	 a.NewChild("child1")
	 a.NewChild("child2").NewChild("grandchild1")
	 fmt.Println(a.Draw())
 }

Displays

root
├── child1
└── child2
    └── grandchild1

The package provides many convenient methods for retrieving children by generation, getting descendent depth, setting display padding, and setting colors.

The package also exposes the DrawOptions method for more fine grained control over the display.

Any node from which the Draw*() methods are called will be considered the root node for display purposes.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DrawInput added in v0.0.4

type DrawInput struct {
	Border  bool   // whether or not to draw a border
	Debug   bool   // whether or not to add debug info to output
	Padding string // rendered padding for this and child nodes
}

DrawInput holds input options for the DrawOptions method

type Node

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

Node contains methods for adding/retrieving children and rendering a tree drawing.

func NewNode

func NewNode(contents string) *Node

NewNode returns a new node with contents of the passed string. Please do not use color formatted strings and instead use the provided SetColor* methods.

func (*Node) AddChild

func (n *Node) AddChild(nc *Node) *Node

AddChild adds the given Node to the children of the current Node

func (*Node) Draw

func (n *Node) Draw() (rendering string)

Draw sets default input options and returns a string of the rendered tree for this Node as if this node is root

func (*Node) DrawOptions added in v0.0.4

func (n *Node) DrawOptions(di *DrawInput) (rendering string)

DrawOptions takes a DrawInput struct with desired parameters and returns the tree formatted string.

func (*Node) GetAllDescendents added in v0.0.4

func (n *Node) GetAllDescendents() (all []*Node)

GetAllDescendents gets all descendents of this node and returns a slice of pointers. Useful for updating them.

func (*Node) GetChild

func (n *Node) GetChild(y int) (dc *Node)

GetChild returns a pointer to the y'th child of the Node. If the y'th child does not exist a nil pointer is returned.

func (*Node) GetDepth added in v0.0.4

func (n *Node) GetDepth() int

GetDepth returns this node's depth. Depths are updated as nodes are added.

func (*Node) GetGeneration added in v0.0.4

func (n *Node) GetGeneration(y int) []*Node

GetGeneration gets all the children of the y'th generation of this node

func (*Node) GetID added in v0.0.5

func (n *Node) GetID() string

GetID returns the UUID of the node. Useful for identifying unique nodes when many have the same contents.

func (*Node) MaxDepth added in v0.0.4

func (n *Node) MaxDepth() (maxDepth int)

MaxDepth returns the maximum depth of descendents and child descendents

func (*Node) NewChild

func (n *Node) NewChild(contents string) *Node

NewChild adds a child with contents of the passed string to this Node's children. It returns the pointer to the new Node. This can be discarded or used for chaining methods in literals (e.g., a.NewChild("foo").NewChild("bar"))

Please do not use color formatted strings and instead use the provided SetColor* methods.

func (*Node) NumChildren added in v0.0.4

func (n *Node) NumChildren() int

NumChildren returns the number of children this node has

func (*Node) SetColor added in v0.0.4

func (n *Node) SetColor(fatihcolor color.Attribute) *Node

SetColor sets the color of the node to the passed fatih/color attribute Requires that the caller import fatih/color and reference their color.Attribute

func (*Node) SetColorMagenta added in v0.0.4

func (n *Node) SetColorMagenta() *Node

SetColorMagenta sets the color of the node to magenta

func (*Node) SetColorRed added in v0.0.4

func (n *Node) SetColorRed() *Node

SetColorRed sets the color of the node to red

func (*Node) SetColorYellow added in v0.0.4

func (n *Node) SetColorYellow() *Node

SetColorGreen sets the color of the node to green

func (*Node) SetContents added in v0.0.4

func (n *Node) SetContents(newContents string)

SetContents sets new contents for this node. Please do not use color formatted strings and instead use the provided SetColor* methods.

func (*Node) SetPaddingAll added in v0.0.4

func (n *Node) SetPaddingAll(padding string) (err error)

SetPadding sets new padding for this node and all of it's descendents.

func (Node) String

func (n Node) String() string

String returns a string, satisfying the Stringer interface

Jump to

Keyboard shortcuts

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