-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathgotree.go
More file actions
169 lines (142 loc) · 3.51 KB
/
gotree.go
File metadata and controls
169 lines (142 loc) · 3.51 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
// Package gotree create and print tree.
package gotree
import (
"strings"
)
const (
newLine = "\n"
emptySpace = " "
middleItem = "├── "
continueItem = "│ "
lastItem = "└── "
)
type (
tree struct {
text string
items []Tree
}
// Tree is tree interface
Tree interface {
Add(text string) Tree
AddTree(tree Tree)
Items() []Tree
Text() string
Print() string
}
printer struct {
}
// Printer is printer interface
Printer interface {
Print(Tree) string
}
)
// New returns a new GoTree.Tree.
// The text parameter accepts any string, including empty strings.
// Multi-line text is supported using newline characters.
func New(text string) Tree {
return &tree{
text: text,
items: make([]Tree, 0),
}
}
// Add adds a node to the tree with the given text.
// Returns the newly created child node to allow method chaining.
// Empty strings are valid and will create a node with empty text.
func (t *tree) Add(text string) Tree {
n := New(text)
t.items = append(t.items, n)
return n
}
// AddTree adds a tree as an item.
// If the provided tree is nil, this method does nothing (safe no-op).
func (t *tree) AddTree(tree Tree) {
if tree == nil {
return
}
t.items = append(t.items, tree)
}
// Text returns the node's value
func (t *tree) Text() string {
return t.text
}
// Items returns all items in the tree
func (t *tree) Items() []Tree {
return t.items
}
// Print returns an visual representation of the tree
func (t *tree) Print() string {
return newPrinter().Print(t)
}
func newPrinter() Printer {
return &printer{}
}
// Print prints a tree to a string
func (p *printer) Print(t Tree) string {
var builder strings.Builder
builder.Grow(len(t.Text()) + 100) // Root text + reasonable default
builder.WriteString(t.Text())
builder.WriteString(newLine)
builder.WriteString(p.printItems(t.Items(), []bool{}))
return builder.String()
}
func (p *printer) printText(text string, spaces []bool, last bool) string {
var builder strings.Builder
// Pre-allocate capacity for better performance
// Estimate: spaces * 4 chars + indicator + text + newline
builder.Grow(len(spaces)*4 + 4 + len(text) + 1)
// Build the prefix from spaces
for _, space := range spaces {
if space {
builder.WriteString(emptySpace)
} else {
builder.WriteString(continueItem)
}
}
prefix := builder.String()
indicator := middleItem
if last {
indicator = lastItem
}
// Reset builder for output
builder.Reset()
builder.Grow(len(prefix)*2 + len(text) + 10)
lines := strings.Split(text, "\n")
for i := range lines {
lineText := lines[i] // Fix variable shadowing
if i == 0 {
builder.WriteString(prefix)
builder.WriteString(indicator)
builder.WriteString(lineText)
builder.WriteString(newLine)
continue
}
if last {
indicator = emptySpace
} else {
indicator = continueItem
}
builder.WriteString(prefix)
builder.WriteString(indicator)
builder.WriteString(lineText)
builder.WriteString(newLine)
}
return builder.String()
}
func (p *printer) printItems(t []Tree, spaces []bool) string {
if len(t) == 0 {
return ""
}
var builder strings.Builder
// Estimate capacity: rough approximation based on tree size
// Each item typically needs ~50 chars (conservative estimate)
builder.Grow(len(t) * 50)
for i, f := range t {
last := i == len(t)-1
builder.WriteString(p.printText(f.Text(), spaces, last))
if len(f.Items()) > 0 {
spacesChild := append(spaces, last)
builder.WriteString(p.printItems(f.Items(), spacesChild))
}
}
return builder.String()
}