-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.go
44 lines (36 loc) · 924 Bytes
/
debug.go
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
package ui
import (
"fmt"
"strings"
)
// PrintWidgetTree prints a widget tree to console.
func PrintWidgetTree(root Widget) {
fmt.Printf("--- Widget Tree of %s ---\n", root)
for _, row := range WidgetTree(root) {
fmt.Println(row)
}
}
// WidgetTree returns a string representing the tree of widgets starting
// at a given widget.
func WidgetTree(root Widget) []string {
var crawl func(int, Widget) []string
crawl = func(depth int, node Widget) []string {
var (
prefix = strings.Repeat(" ", depth)
size = node.Size()
width = size.W
height = size.H
fixedSize = node.FixedSize()
lines = []string{
fmt.Sprintf("%s%s P:%s S:%dx%d (fixedSize: %+v)",
prefix, node.ID(), node.Point(), width, height, fixedSize,
),
}
)
for _, child := range node.Children() {
lines = append(lines, crawl(depth+1, child)...)
}
return lines
}
return crawl(0, root)
}