-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.go
105 lines (88 loc) · 1.87 KB
/
model.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
package main
import (
"time"
"github.com/charmbracelet/bubbles/help"
tea "github.com/charmbracelet/bubbletea"
docker "github.com/fsouza/go-dockerclient"
)
type Container struct {
name string
state string
id string
ancestor string
desc string
}
type Volume struct {
name string
mountPoint string
containers []docker.APIContainers
createdAt time.Time
}
type Image struct {
id string
name string
}
const (
pageContainer int = iota
pageImage
pageVolume
pageLog
)
type model struct {
containers []Container
images []Image
volumes []Volume
cursor int
selected map[int]struct{}
blinkSwitch int
// TODO: merge process into Container struct
processes map[string]string // map[containerID]desiredState
keys keyMap
help help.Model
logs string
page int
width int
height int
}
// fast tick rate doesn't seems to affect performance (average 20 container)
// TODO: change tickrate to 1s if theres no running docker action
const tickRate = 300 * time.Millisecond
// const tickRate = time.Second
type TickMsg struct {
Time time.Time
}
func doTick() tea.Cmd {
return tea.Tick(tickRate, func(t time.Time) tea.Msg {
return TickMsg{Time: t}
})
}
func (m model) Init() tea.Cmd {
return doTick()
}
func initialModel() model {
cursor := 0
// containers
containers := getContainers()
images := getImages()
volumes := getVolumes()
// descriptions of container at cursor
if len(containers) > 0 {
containers[cursor].desc = buildContainerDescShort(containers[cursor].id)
}
// help
h := help.New()
h.Width = fullWidth
// processes
processes := make(map[string]string)
return model{
cursor: 0,
containers: containers,
images: images,
volumes: volumes,
selected: make(map[int]struct{}),
processes: processes,
page: pageContainer,
keys: keys,
help: h,
}
}