-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree.go
111 lines (100 loc) · 2.51 KB
/
tree.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
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
package main
import (
"strings"
"github.com/cfstras/pcm/types"
)
func treePrint(target *[]string, index map[int]types.Node, pathToIndexMap map[string]int,
node *types.Container, width int) {
if node == nil {
return
}
treeDescend(target, index, pathToIndexMap, "", "/", node, width)
return
}
func treeDescend(target *[]string, index map[int]types.Node, pathToIndexMap map[string]int,
prefix string, pathPrefix string, node *types.Container, width int) {
if !node.Expanded {
return
}
for i := range node.Containers {
nextCont := &node.Containers[i]
nextPathPrefix := pathPrefix + nextCont.Name + "/"
var nodeSym string
var newPrefix string
var expand string
if i == len(node.Containers)-1 {
if len(node.Connections) > 0 {
nodeSym = "┡"
newPrefix = "│ "
} else {
nodeSym = "┗"
newPrefix = " "
}
} else {
if len(node.Containers) > 0 {
nodeSym = "┣"
newPrefix = "┃ "
} else if len(node.Containers) == 0 {
nodeSym = "┗"
newPrefix = " "
} else {
nodeSym = "┣"
newPrefix = "┃ "
}
}
if nextCont.Expanded {
if len(nextCont.Containers) > 0 {
expand = "━┓ ▼ "
} else {
expand = "━┑ ▼ "
}
} else {
expand = "━┅ ▶ "
}
index[len(*target)] = nextCont
if pathToIndexMap != nil {
pathToIndexMap[nextCont.Path()] = len(*target)
}
str := prefix + nodeSym + expand + nextCont.Name
nextCont.TreeView = str
spaces := width - len([]rune(str)) - len([]rune(nextCont.StatusInfo))
if spaces > 0 {
str += strings.Repeat(" ", spaces)
}
str += nextCont.StatusInfo
*target = append(*target, str)
treeDescend(target, index, pathToIndexMap, prefix+newPrefix,
nextPathPrefix, nextCont, width)
}
for i := range node.Connections {
conn := &node.Connections[i]
var nodeSym string
if i == len(node.Connections)-1 {
nodeSym = "└"
} else if len(node.Connections) != 0 {
nodeSym = "├"
} else {
nodeSym = "┌"
}
index[len(*target)] = conn
if pathToIndexMap != nil {
pathToIndexMap[conn.Path()] = len(*target)
}
str := prefix + nodeSym + "─ " + conn.Name
conn.TreeView = str
spaces := width - len([]rune(str)) - len([]rune(conn.StatusInfo))
if spaces > 0 {
str += strings.Repeat(" ", spaces)
}
str += conn.StatusInfo
*target = append(*target, str)
}
}
// get all keys of the map as a slice
func listWords(conns map[string]*types.Connection) []string {
words := make([]string, 0, len(conns))
for k := range conns {
words = append(words, k)
}
return words
}