-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathview.go
402 lines (345 loc) Β· 10.2 KB
/
view.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
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
package main
import (
"fmt"
"log"
"sort"
"strconv"
"strings"
"time"
"github.com/charmbracelet/lipgloss"
docker "github.com/fsouza/go-dockerclient"
"github.com/mattn/go-runewidth"
"github.com/muesli/reflow/wrap"
)
// ----------------------------- render utils -----------------------------
func buildEmptyBody(text, title string, width int) string {
emptyBody := text
padOuterComponent(&emptyBody, width)
emptyBody = strings.TrimSuffix(emptyBody, "\n")
return fmt.Sprintf("%s\n%s\n", title, emptyBody)
}
func padBodyHeight(s *string, itemCount int) {
if itemCount < minHeightPerView {
*s += strings.Repeat("\n", minHeightPerView-itemCount)
}
}
func padOuterComponent(s *string, windowWidth int) {
var outerPad, innerPad, longest int
// get width of longer help string (fullHelp)
split := strings.Split(*s, "\n")
for _, line := range split {
if lipgloss.Width(line) > longest {
longest = lipgloss.Width(line)
}
}
sWidth := longest
if windowWidth > 0 {
outerPad = (windowWidth - fullWidth) / 2
innerPad = (fullWidth - sWidth) / 2
}
var newS string
for _, line := range split {
newS += strings.Repeat(" ", outerPad+innerPad) + line + "\n"
}
*s = newS
}
func padItemName(name string, maxLen int) string {
nameWidth := lipgloss.Width(name)
if nameWidth < maxLen {
name = name + strings.Repeat(" ", maxLen-nameWidth)
}
name += "\n"
return name
}
func buildTitleView(m model) string {
s := "π³ Killer Whale" + " "
padOuterComponent(&s, m.width)
s = strings.TrimSuffix(s, "\n")
return s
}
// ----------------------------- log view -----------------------------
func buildLogView(m model) string {
var s string
s += m.logs
logStyle.MarginLeft((fullWidth - lipgloss.Width(s)) / 2)
logStyle.AlignHorizontal(lipgloss.Center)
return logStyle.Render(s)
}
// ----------------------------- volume view -----------------------------
func formatVolumeMountPoint(mp string) string {
s := wrap.String(mp, fixedBodyRWidth-8)
split := strings.Split(s, "\n")
if len(split) > 1 {
s = split[0] + "\n"
for _, line := range split[1:] {
s += strings.Repeat(" ", 10) + line + "\n"
}
}
s = strings.TrimSuffix(s, "\n")
return s
}
func formatVolumeInUse(b bool) string {
s := fmt.Sprintf("%v", b)
var style lipgloss.Style
if b {
style = inUseTextTrueStyle
} else {
style = inUseTextFalseStyle
}
return style.Render(s)
}
func formatVolumeName(name string) string {
s := wrap.String(name, fixedBodyRWidth-8)
split := strings.Split(s, "\n")
if len(split) > 1 {
s = split[0] + "\n"
for _, line := range split[1:] {
s += strings.Repeat(" ", 10) + line + "\n"
}
}
s = strings.TrimSuffix(s, "\n")
return s
}
func buildVolumeDescShort(volume Volume) string {
name := volume.name
createdAt := volume.createdAt
containers := volume.containers
mountPoint := volume.mountPoint
// calculate how many day since created
duration := time.Since(createdAt).Hours()
days := int(duration) / 24
// get first element from result
// TODO: display list of containers, might have >1 container per volume
var inUse bool
containerName := "null"
if len(containers) > 0 {
inUse = true
containerName = containers[0].Names[0][1:]
}
var desc string
desc += fmt.Sprintf("Created : %d days ago\n", days)
desc += fmt.Sprintf("Name : %s\n", formatVolumeName(name))
desc += fmt.Sprintf("In Use : %v\n", formatVolumeInUse(inUse))
desc += fmt.Sprintf("Use by : %s\n", containerName)
desc += fmt.Sprintf("Mount : %s\n", formatVolumeMountPoint(mountPoint))
return desc
}
func buildVolumeView(m model) (string, string) {
var bodyL, bodyR string
for i, choice := range m.volumes {
cursor := " "
check := " "
icon := "β "
if m.cursor == i {
cursor = "β―"
bodyR = buildVolumeDescShort(choice)
}
name := runewidth.Truncate(choice.name, maxVolumeNameWidth, "")
if len(choice.containers) > 0 {
icon = inUseIconTrueStyle.Render(icon)
} else {
icon = inUseIconFalseStyle.Render(icon)
}
name = icon + name
if _, ok := m.selected[i]; ok {
check = checkStyle.Render("β")
}
row := fmt.Sprintf("%s %s %s", cursor, check, name)
bodyL += row + "\n"
}
return bodyLStyle.Render(bodyL), bodyRStyle.Render(bodyR)
}
// ----------------------------- image view -----------------------------
func formatCmd(cmd []string) string {
s := wrap.String(strings.Join(cmd, " "), fixedBodyRWidth-10)
split := strings.Split(s, "\n")
if len(split) > 1 {
s = split[0] + "\n"
for _, line := range split[1:] {
s += strings.Repeat(" ", 10) + line + "\n"
}
}
s = strings.TrimSuffix(s, "\n")
return s
}
func formatImageVolumes(volumeMap map[string]struct{}) string {
var s string
for vol := range volumeMap {
s += fmt.Sprintf("%s\n", vol)
}
s = strings.TrimSuffix(s, "\n")
return s
}
func buildImageDescShort(id string) string {
client, err := docker.NewClientFromEnv()
if err != nil {
log.Fatal(err)
}
image, err := client.InspectImage(id)
if err != nil {
log.Fatal(err)
}
desc := fmt.Sprintf("ID : %v\n", runewidth.Truncate(image.ID, fixedBodyRWidth-8, "..."))
desc += fmt.Sprintf("Created : %s\n", image.Created.Format("2006-01-02 15:04:05"))
desc += fmt.Sprintf("Size : %s\n", convertSizeToHumanRedable(image.Size))
desc += fmt.Sprintf("Cmd : %v\n", formatCmd(image.Config.Cmd))
desc += fmt.Sprintf("Volumes : %v\n", formatImageVolumes(image.Config.Volumes))
return desc
}
func buildImageView(m model) (string, string) {
var bodyL, bodyR string
for i, choice := range m.images {
cursor := " " // default cursor
check := " "
if m.cursor == i {
cursor = "β―"
bodyR = buildImageDescShort(choice.id)
}
name := choice.name
if _, ok := m.selected[i]; ok {
check = checkStyle.Render("β")
}
name = padItemName(name, maxImageNameWidth)
row := fmt.Sprintf("%s %s %s", cursor, check, name)
bodyL += row
}
padBodyHeight(&bodyL, len(m.images)+2)
return bodyLStyle.Render(bodyL), bodyRStyle.Render(bodyR)
}
// ----------------------------- container view -----------------------------
func getSortedPort(portsMap map[docker.Port][]docker.PortBinding) []docker.Port {
l := []docker.Port{}
for k := range portsMap {
l = append(l, k)
}
sort.Slice(l, func(i, j int) bool {
// convert to int before compare
iInt, _ := strconv.Atoi(l[i].Port())
jInt, _ := strconv.Atoi(l[j].Port())
return iInt > jInt
})
return l
}
func formatPortsMapping(portsMap map[docker.Port][]docker.PortBinding) string {
s := "\n"
sortedPorts := getSortedPort(portsMap)
for _, containerPort := range sortedPorts {
// find matching host machine port
var portBindings []docker.PortBinding // host machine
for k, v := range portsMap {
if k.Port() == containerPort.Port() {
portBindings = v
}
}
var joinedPortBindingStr string
if len(portBindings) == 0 {
joinedPortBindingStr = "null"
}
for i, bindings := range portBindings {
IP, Port := bindings.HostIP, bindings.HostPort
joinedPortBindingStr += fmt.Sprintf("%s:%s", IP, Port)
// has more than 1 port binding per container port
if i > 0 {
joinedPortBindingStr += "\n"
}
}
containerPortStr := fmt.Sprintf("%5s/%s", containerPort.Port(), containerPort.Proto())
s += fmt.Sprintf(" %s -> %s\n", containerPortStr, joinedPortBindingStr)
}
// add column (container | hostmachine)
if len(sortedPorts) > 0 {
s = fmt.Sprintf(
"%s -> %s",
PortMapColStyle.Render("container"),
PortMapColStyle.Render("host machine"),
) + s
}
s = strings.TrimSuffix(s, "\n")
return s
}
func buildContainerDescShort(id string) string {
client, err := docker.NewClientFromEnv()
if err != nil {
log.Fatal(err)
}
container, err := client.InspectContainerWithOptions(docker.InspectContainerOptions{
ID: id,
})
if err != nil {
log.Fatal(err)
}
desc := fmt.Sprintf(
"ID : %v\n",
runewidth.Truncate(container.ID, fixedBodyRWidth-8, "..."),
)
desc += fmt.Sprintf("Image : %s\n", container.Config.Image)
desc += fmt.Sprintf("Cmd : %s\n", strings.Join(container.Config.Cmd, " "))
desc += fmt.Sprintf("State : %s\n", container.State.String())
desc += fmt.Sprintf("IP : %s\n", container.NetworkSettings.IPAddress)
desc += fmt.Sprintf("Ports : %v\n", formatPortsMapping(container.NetworkSettings.Ports))
return desc
}
func buildContainerView(m model) (string, string) {
var bodyL, bodyR string
for i, choice := range m.containers {
cursor := " " // default cursor
check := " "
if m.cursor == i {
cursor = "β―"
bodyR = buildContainerDescShort(choice.id)
}
isProcessing := checkProcess(choice.id, m.processes)
stateStyle := stateStyleMap[choice.state]
if isProcessing && m.blinkSwitch == on {
stateStyle = stateStyle.Copy().Foreground(pitchBlack)
}
state := stateStyle.Render("β")
// state := stateStyle.Render("β")
name := choice.name
name = runewidth.Truncate(name, maxContainerNameWidth, "...")
if _, ok := m.selected[i]; ok {
check = checkStyle.Render("β")
}
name = padItemName(name, maxContainerNameWidth)
row := fmt.Sprintf("%s %s %s %s", cursor, check, state, name)
bodyL += row
}
// pad body height
padBodyHeight(&bodyL, len(m.containers)+2)
return bodyLStyle.Render(bodyL), bodyRStyle.Render(bodyR)
}
// ----------------------------- main view -----------------------------
func (m model) View() string {
var final string
var bodyL, bodyR, body, bottom string
// body L
switch m.page {
case pageContainer:
bodyL, bodyR = buildContainerView(m)
case pageImage:
bodyL, bodyR = buildImageView(m)
case pageVolume:
bodyL, bodyR = buildVolumeView(m)
}
// title
title := buildTitleView(m)
title = titleStyle.Render(title)
// join left + right component
body = lipgloss.JoinHorizontal(lipgloss.Left, bodyL, bodyR)
body = bodyStyle.Render(body)
// bottom
bottom = buildLogView(m)
// help
help := m.help.View(m.keys)
padOuterComponent(&help, m.width)
// join title + body + log + help
final += lipgloss.JoinVertical(lipgloss.Top, body, bottom)
appStyle.MarginLeft((m.width - fullWidth) / 2)
// 0 containers/ image
if len(m.containers) == 0 && m.page == pageContainer {
return buildEmptyBody("\nNo containers found.", title, m.width)
} else if len(m.images) == 0 && m.page == pageImage {
return buildEmptyBody("\nNo images found.", title, m.width)
}
return title + "\n" + appStyle.Render(final) + "\n" + help
}