-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathui.go
More file actions
208 lines (191 loc) · 5.33 KB
/
ui.go
File metadata and controls
208 lines (191 loc) · 5.33 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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package main
import (
"fmt"
"strings"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
func cmdSelectContext(conf *KubeConfig) error {
contexts, err := conf.Contexts()
if err != nil {
return fmt.Errorf("parse contexts: %w", err)
}
if len(contexts) == 0 {
fmt.Println("no contexts defined")
return nil
}
clusters := GroupContextsByCluster(contexts)
selectedContext, err := conf.SelectedContext()
if err != nil {
return fmt.Errorf("get selected context: %w", err)
}
userSelectedContext, err := showSelectionUI(clusters, selectedContext)
if err != nil {
return fmt.Errorf("show selection ui: %w", err)
}
if len(userSelectedContext) > 0 {
if userSelectedContext == selectedContext {
// nothing to do
return nil
}
conf.SetSelectedContext(userSelectedContext)
if err := conf.Save(); err != nil {
return fmt.Errorf("write config: %w", err)
}
fmt.Println("switched to context", userSelectedContext)
}
return nil
}
func showSelectionUI(clusters []ClusterWithContexts, selectedContext string) (string, error) {
var contextCount int
for _, cluster := range clusters {
contextCount += len(cluster.Contexts)
}
var expandMode bool
if contextCount > 30 {
expandMode = true
}
var userSelectedContext string
app := tview.NewApplication()
rootNode := tview.NewTreeNode("Clusters").SetSelectable(false)
treeView := tview.NewTreeView().SetRoot(rootNode)
var firstContextNode *tview.TreeNode
for _, cluster := range clusters {
clusterNode := tview.NewTreeNode(cluster.Name).SetSelectable(expandMode)
if expandMode {
clusterNode.SetSelectedFunc(func() {
if clusterNode.IsExpanded() {
clusterNode.Collapse()
} else {
clusterNode.Expand()
}
})
}
clusterNode.SetColor(tcell.ColorGreen)
var containsSelection bool
for _, c := range cluster.Contexts {
var nodeName string
if expandMode {
nodeName = c.Context.Namespace
} else {
nodeName = c.Name
}
contextNode := tview.NewTreeNode(nodeName).SetSelectable(true)
if c.Name == selectedContext {
// this is the currently selected context
contextNode.SetColor(tcell.ColorYellow)
treeView.SetCurrentNode(contextNode)
containsSelection = true
} else {
contextNode.SetColor(tcell.ColorTurquoise)
}
// put iterator variable value in block-local variable so the lambda function
// does not access the wrong field afterwards
contextName := c.Name
contextNode.SetSelectedFunc(func() {
userSelectedContext = contextName
app.Stop()
})
if firstContextNode == nil {
// remember first visible node as fallback for default selection
firstContextNode = contextNode
}
if expandMode {
contextNode.SetReference(clusterNode)
}
clusterNode.AddChild(contextNode)
}
if expandMode {
if containsSelection {
clusterNode.SetColor(tcell.ColorYellow)
} else {
clusterNode.Collapse()
}
}
rootNode.AddChild(clusterNode)
}
if treeView.GetCurrentNode() == nil {
// no default selection? fall back to first visible node
treeView.SetCurrentNode(firstContextNode)
}
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
if event.Key() == tcell.KeyEscape {
app.Stop()
return nil
}
if event.Key() == tcell.KeyRight {
if expandMode {
if node := treeView.GetCurrentNode(); node != nil {
if len(node.GetChildren()) > 0 && !node.IsExpanded() {
// expand collapsed cluster node
treeView.GetCurrentNode().Expand()
} else {
// jump to last children of selected cluster node
if childs := node.GetChildren(); len(childs) > 0 {
treeView.SetCurrentNode(childs[len(childs)-1])
}
}
}
}
return nil
}
if event.Key() == tcell.KeyLeft {
if node := treeView.GetCurrentNode(); node != nil {
if len(node.GetChildren()) > 0 && node.IsExpanded() {
// collapse cluster node
treeView.GetCurrentNode().Collapse()
} else if ref := node.GetReference(); ref != nil {
// jump to parent cluster node of selected context
if ref, ok := ref.(*tview.TreeNode); ok {
treeView.SetCurrentNode(ref)
}
}
}
return nil
}
r := event.Rune()
if r >= 'A' && r <= 'Z' {
r -= 'A'
r += 'a'
}
if r >= 'a' && r <= 'z' {
if node := treeView.GetCurrentNode(); node != nil {
if len(node.GetChildren()) > 0 && node.IsExpanded() {
// select first child context node beginning with rune of selected cluster node
for _, child := range node.GetChildren() {
if strings.HasPrefix(child.GetText(), string(r)) {
treeView.SetCurrentNode(child)
break
}
}
} else if ref := node.GetReference(); ref != nil {
// select first child context node beginning with rune of parent cluster node of selected context node
if ref, ok := ref.(*tview.TreeNode); ok {
childs := ref.GetChildren()
var offset int
for i := range childs {
if childs[i] == treeView.GetCurrentNode() {
offset = i + 1
break
}
}
for i := 0; i < len(childs); i++ {
child := childs[(offset+i+len(childs))%len(childs)]
if strings.HasPrefix(child.GetText(), string(r)) {
treeView.SetCurrentNode(child)
break
}
}
}
}
}
return nil
}
return event
})
app.SetRoot(treeView, true)
if err := app.Run(); err != nil {
return "", err
}
return userSelectedContext, nil
}