GitHub - rendicott/gree: for building bash tree like visualizations with go

1 min read Original article ↗

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.

Release Notes

v0.0.8

  • Fix: Draw() output now works correctly with grep and pipes. Two bugs caused null bytes and negative widths when no TTY is present (e.g. gree | grep foo): unset map entries in rrow.str() emitted null runes (now replaced with spaces, trailing whitespace trimmed), and GetConsoleSize() returning 0 caused a negative width clamp (now guarded with terminalWidth > 0).