summaryrefslogtreecommitdiff
path: root/vendor/github.com/authzed/zed/internal/printers/treeprinter.go
blob: 55c3830b5211e6db4e14be8f46ff78c049cb5dad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package printers

import (
	"strings"

	"github.com/xlab/treeprint"

	"github.com/authzed/zed/internal/console"
)

type TreePrinter struct {
	tree treeprint.Tree
}

func NewTreePrinter() *TreePrinter {
	return &TreePrinter{}
}

func (tp *TreePrinter) Child(val string) *TreePrinter {
	if tp.tree == nil {
		tp.tree = treeprint.NewWithRoot(val)
		return tp
	}
	return &TreePrinter{tree: tp.tree.AddBranch(val)}
}

func (tp *TreePrinter) Print() {
	console.Println(tp.String())
}

func (tp *TreePrinter) Indented() string {
	var sb strings.Builder
	lines := strings.Split(tp.String(), "\n")
	for _, line := range lines {
		sb.WriteString("  " + line + "\n")
	}

	return sb.String()
}

func (tp *TreePrinter) String() string {
	return tp.tree.String()
}