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 ¶
- type DrawInput
- type Node
- func (n *Node) AddChild(nc *Node) *Node
- func (n *Node) Draw() (rendering string)
- func (n *Node) DrawOptions(di *DrawInput) (rendering string)
- func (n *Node) GetAllDescendents() (all []*Node)
- func (n *Node) GetChild(y int) (dc *Node)
- func (n *Node) GetDepth() int
- func (n *Node) GetGeneration(y int) []*Node
- func (n *Node) GetID() string
- func (n *Node) MaxDepth() (maxDepth int)
- func (n *Node) NewChild(contents string) *Node
- func (n *Node) NumChildren() int
- func (n *Node) SetColor(fatihcolor color.Attribute) *Node
- func (n *Node) SetColorMagenta() *Node
- func (n *Node) SetColorRed() *Node
- func (n *Node) SetColorYellow() *Node
- func (n *Node) SetContents(newContents string)
- func (n *Node) SetPaddingAll(padding string) (err error)
- func (n Node) String() string
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 ¶
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) Draw ¶
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
DrawOptions takes a DrawInput struct with desired parameters and returns the tree formatted string.
func (*Node) GetAllDescendents ¶ added in v0.0.4
GetAllDescendents gets all descendents of this node and returns a slice of pointers. Useful for updating them.
func (*Node) GetChild ¶
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
GetDepth returns this node's depth. Depths are updated as nodes are added.
func (*Node) GetGeneration ¶ added in v0.0.4
GetGeneration gets all the children of the y'th generation of this node
func (*Node) GetID ¶ added in v0.0.5
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
MaxDepth returns the maximum depth of descendents and child descendents
func (*Node) NewChild ¶
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
NumChildren returns the number of children this node has
func (*Node) SetColor ¶ added in v0.0.4
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
SetColorMagenta sets the color of the node to magenta
func (*Node) SetColorRed ¶ added in v0.0.4
SetColorRed sets the color of the node to red
func (*Node) SetColorYellow ¶ added in v0.0.4
SetColorGreen sets the color of the node to green
func (*Node) SetContents ¶ added in v0.0.4
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
SetPadding sets new padding for this node and all of it's descendents.