This repository has been archived by the owner on Sep 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
views.go
318 lines (265 loc) Β· 6.83 KB
/
views.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package main
import (
"fmt"
"strings"
"time"
"github.com/jroimartin/gocui"
"github.com/willf/pad"
)
// View: Overlay
func viewOverlay(g *gocui.Gui, lMaxX int, lMaxY int) error {
if v, err := g.SetView("overlay", 0, 0, lMaxX, lMaxY); err != nil {
if err != gocui.ErrUnknownView {
return err
}
// Settings
v.Frame = false
}
return nil
}
// View: Title bar
func viewTitle(g *gocui.Gui, lMaxX int, lMaxY int) error {
if v, err := g.SetView("title", -1, -1, lMaxX, 1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
// Settings
v.Frame = false
v.BgColor = gocui.ColorDefault | gocui.AttrReverse
v.FgColor = gocui.ColorDefault | gocui.AttrReverse
// Content
fmt.Fprintln(v, versionTitle(lMaxX))
}
return nil
}
// View: Debug
func viewDebug(g *gocui.Gui, lMaxX int, lMaxY int) error {
if v, err := g.SetView("debug", 2, 2, lMaxX-4, lMaxY-2); err != nil {
if err != gocui.ErrUnknownView {
return err
}
// Settings
v.Title = " Debug "
v.Autoscroll = true
}
return nil
}
// View: Namespace
func viewNamespaces(g *gocui.Gui, lMaxX int, lMaxY int) error {
w := lMaxX / 2
h := lMaxY / 4
minX := (lMaxX / 2) - (w / 2)
minY := (lMaxY / 2) - (h / 2)
maxX := minX + w
maxY := minY + h
// Main view
if v, err := g.SetView("namespaces", minX, minY, maxX, maxY); err != nil {
if err != gocui.ErrUnknownView {
return err
}
// Configure view
v.Title = " Namespaces "
v.Frame = true
v.Highlight = true
v.SelBgColor = gocui.ColorGreen
v.SelFgColor = gocui.ColorBlack
// Display list
go viewNamespacesRefreshList(g)
}
return nil
}
// Actualize list in namespaces view
func viewNamespacesRefreshList(g *gocui.Gui) {
g.Update(func(g *gocui.Gui) error {
debug(g, "View namespaces: Actualize")
v, err := g.View("namespaces")
if err != nil {
return err
}
namespaces, err := getNamespaces()
if err != nil {
displayError(g, err)
return nil
}
hideError(g)
var ns []string
v.Clear()
if len(namespaces.Items) > 0 {
debug(g, fmt.Sprintf("View namespaces: %d namespaces found", len(namespaces.Items)))
for _, namespace := range namespaces.Items {
fmt.Fprintln(v, namespace.GetName())
ns = append(ns, namespace.GetName())
}
} else {
debug(g, "View namespaces: Namespaces not found")
}
setViewCursorToLine(g, v, ns, NAMESPACE)
return nil
})
}
// View: Logs
func viewLogs(g *gocui.Gui, lMaxX int, lMaxY int) error {
if v, err := g.SetView("logs", 2, 2, lMaxX-4, lMaxY-2); err != nil {
if err != gocui.ErrUnknownView {
return err
}
// Settings
v.Title = " Logs "
v.Autoscroll = true
}
// Containers view
minX := int(lMaxX/5) * 4
minY := 2
maxX := lMaxX - 4
maxY := int(lMaxY / 5)
if v, err := g.SetView("logs-containers", minX, minY, maxX, maxY); err != nil {
if err != gocui.ErrUnknownView {
return err
}
// Settings
v.Frame = true
v.BgColor = gocui.ColorBlack
v.Highlight = true
}
return nil
}
// View: Pods
func viewPods(g *gocui.Gui, lMaxX int, lMaxY int) error {
if v, err := g.SetView("pods", -1, 1, lMaxX, lMaxY-1); err != nil {
if err != gocui.ErrUnknownView {
return err
}
// Settings
v.Frame = false
v.Highlight = true
v.SelBgColor = gocui.ColorGreen
v.SelFgColor = gocui.ColorBlack
v.SetCursor(0, 2)
// Set as current view
g.SetCurrentView(v.Name())
// Content
go viewPodsShowWithAutoRefresh(g)
}
return nil
}
// Auto refresh view pods
func viewPodsShowWithAutoRefresh(g *gocui.Gui) {
c := getConfig()
t := time.NewTicker(time.Duration(c.frequency) * time.Second)
go viewPodsRefreshList(g)
for {
select {
case <-t.C:
debug(g, fmt.Sprintf("View pods: Refreshing (%ds)", c.frequency))
go viewPodsRefreshList(g)
}
}
}
// Actualize list in pods view
func viewPodsRefreshList(g *gocui.Gui) {
g.Update(func(g *gocui.Gui) error {
lMaxX, _ := g.Size()
debug(g, "View pods: Actualize")
v, err := g.View("pods")
if err != nil {
return err
}
pods, err := getPods()
if err != nil {
displayError(g, err)
return nil
}
hideError(g)
v.Clear()
// Content: Add column
//viewPodsAddLine(v, lMaxX, "NAME", "CPU", "MEMORY", "READY", "STATUS", "RESTARTS", "AGE") // TODO CPU + Memory #20
viewPodsAddLine(v, lMaxX, "NAME", "READY", "STATUS", "RESTARTS", "AGE")
fmt.Fprintln(v, strings.Repeat("β", lMaxX))
if len(pods.Items) > 0 {
debug(g, fmt.Sprintf("View pods: %d pods found", len(pods.Items)))
for _, pod := range pods.Items {
n := pod.GetName()
//c := "?" // TODO CPU + Memory #20
//m := "?" // TODO CPU + Memory #20
s := columnHelperStatus(pod.Status)
r := columnHelperRestarts(pod.Status.ContainerStatuses)
a := columnHelperAge(pod.CreationTimestamp)
cr := columnHelperReady(pod.Status.ContainerStatuses)
viewPodsAddLine(v, lMaxX, n, cr, s, r, a)
//viewPodsAddLine(v, lMaxX, n, c, m, cr, s, r, a) // TODO CPU + Memory #20
}
// Reset cursor when empty line
if l, err := getViewLine(g, v); err != nil || l == "" {
v.SetCursor(0, 2)
}
} else {
v.SetCursor(0, 2)
debug(g, "View pods: Pods not found")
}
return nil
})
}
// Add line to view pods
//func viewPodsAddLine(v *gocui.View, maxX int, name, cpu, memory, ready, status, restarts, age string) { // TODO CPU + Memory #20
func viewPodsAddLine(v *gocui.View, maxX int, name, ready, status, restarts, age string) {
wN := maxX - 34 // 54 // TODO CPU + Memory #20
if wN < 45 {
wN = 45
}
line := pad.Right(name, wN, " ") +
//pad.Right(cpu, 10, " ") + // TODO CPU + Memory #20
//pad.Right(memory, 10, " ") + // TODO CPU + Memory #20
pad.Right(ready, 10, " ") +
pad.Right(status, 10, " ") +
pad.Right(restarts, 10, " ") +
pad.Right(age, 4, " ")
fmt.Fprintln(v, line)
}
// View: Status bar
func viewStatusBar(g *gocui.Gui, lMaxX int, lMaxY int) error {
if v, err := g.SetView("status", -1, lMaxY-2, lMaxX, lMaxY); err != nil {
if err != gocui.ErrUnknownView {
return err
}
// Settings
v.Frame = false
v.BgColor = gocui.ColorBlack
v.FgColor = gocui.ColorWhite
// Content
changeStatusContext(g, "D")
}
return nil
}
// Change status context
func changeStatusContext(g *gocui.Gui, c string) error {
lMaxX, _ := g.Size()
v, err := g.View("status")
if err != nil {
return err
}
v.Clear()
i := lMaxX + 4
b := ""
switch c {
case "D":
i = 150 + i
b = b + frameText("β") + " Up "
b = b + frameText("β") + " Down "
b = b + frameText("D") + " Delete "
b = b + frameText("L") + " Show Logs "
b = b + frameText("CTRL+N") + " Namespace "
case "SE":
i = i + 100
b = b + frameText("β") + " Up "
b = b + frameText("β") + " Down "
b = b + frameText("Enter") + " Select "
case "SL":
i = i + 100
b = b + frameText("β") + " Up "
b = b + frameText("β") + " Down "
b = b + frameText("L") + " Hide Logs "
}
b = b + frameText("CTRL+C") + " Exit"
fmt.Fprintln(v, pad.Left(b, i, " "))
return nil
}