gree

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2023 License: MIT Imports: 1 Imported by: 0

README

gree

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

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")
	a.Draw()
 }

Displays

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

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.

func (*Node) AddChild

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

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

func (Node) Draw

func (n Node) Draw() string

Draw returns a string of the rendered tree for this Node as if this node is root

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) 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"))

func (Node) String

func (n Node) String() string

String() satisfies the Stringer interface

Jump to

Keyboard shortcuts

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