/
container_menu.go
169 lines (143 loc) · 3.55 KB
/
container_menu.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
package appui
import (
"sync"
gizaktermui "github.com/gizak/termui"
"github.com/moncho/dry/docker"
drytermui "github.com/moncho/dry/ui/termui"
)
type ContainerAPI interface {
ContainerByID(string) *docker.Container
}
const cMenuWidth = 30
// ContainerMenuWidget shows the actions menu of a container
type ContainerMenuWidget struct {
containerAPI ContainerAPI
rows []*Row
cInfo *ContainerDetailsWidget
cID string
selectedIndex int
screen Screen
OnUnmount func() error
sync.RWMutex
mounted bool
}
// NewContainerMenuWidget creates a TasksWidget
func NewContainerMenuWidget(containerAPI ContainerAPI, s Screen) *ContainerMenuWidget {
w := ContainerMenuWidget{
containerAPI: containerAPI,
screen: s,
}
return &w
}
// Buffer returns the content of this widget as a termui.Buffer
func (s *ContainerMenuWidget) Buffer() gizaktermui.Buffer {
s.RLock()
defer s.RUnlock()
buf := gizaktermui.NewBuffer()
if !s.mounted {
return buf
}
y := s.screen.Bounds().Min.Y
s.prepareForRendering()
if s.cInfo != nil {
buf.Merge(s.cInfo.Buffer())
y += s.cInfo.GetHeight()
}
for i, row := range s.rows {
row.SetY(y)
y += row.GetHeight()
if i != s.selectedIndex {
row.NotHighlighted()
} else {
row.Highlighted()
}
buf.Merge(row.Buffer())
}
return buf
}
// Filter is a noop for this widget
func (s *ContainerMenuWidget) Filter(_ string) {
}
// ForContainer sets the container for which this widget is showing tasks
func (s *ContainerMenuWidget) ForContainer(cID string) {
s.Lock()
defer s.Unlock()
s.cID = cID
s.mounted = false
}
// Mount prepares this widget for rendering
func (s *ContainerMenuWidget) Mount() error {
s.Lock()
defer s.Unlock()
if s.mounted {
return nil
}
c := s.containerAPI.ContainerByID(s.cID)
if c != nil {
s.cInfo = NewContainerDetailsWidget(c, s.screen.Bounds().Min.Y)
}
rows := make([]*Row, len(docker.CommandDescriptions))
for i, command := range docker.CommandDescriptions {
r := &Row{
ParColumns: []*drytermui.ParColumn{drytermui.NewThemedParColumn(DryTheme, command)},
}
r.AddColumn(r.ParColumns[0])
r.Height = 1
r.Width = r.ParColumns[0].Width
rows[i] = r
}
s.rows = rows
s.align()
s.mounted = true
return nil
}
// Name returns this widget name
func (s *ContainerMenuWidget) Name() string {
return "ContainerMenuWidget"
}
// OnEvent runs the given command
func (s *ContainerMenuWidget) OnEvent(event EventCommand) error {
if s.RowCount() <= 0 || s.selectedIndex < 0 || s.selectedIndex >= s.RowCount() {
return invalidRow{selected: int(s.selectedIndex), max: s.RowCount()}
}
return event(s.cID + ":" + s.rows[s.selectedIndex].ParColumns[0].Text)
}
// RowCount returns the number of rows of this widget.
func (s *ContainerMenuWidget) RowCount() int {
return len(s.rows)
}
// Sort is a noop for this widget
func (s *ContainerMenuWidget) Sort() {
}
// Unmount this widget
func (s *ContainerMenuWidget) Unmount() error {
s.Lock()
defer s.Unlock()
s.mounted = false
if s.OnUnmount != nil {
return s.OnUnmount()
}
return nil
}
func (s *ContainerMenuWidget) align() {
width := s.screen.Bounds().Dx()
if s.cInfo != nil {
s.cInfo.SetWidth(width)
s.cInfo.SetX(s.screen.Bounds().Min.X)
}
rowsX := (width - cMenuWidth) / 2
for _, row := range s.rows {
row.SetX(rowsX)
row.SetWidth(cMenuWidth)
}
}
func (s *ContainerMenuWidget) prepareForRendering() {
s.screen.Cursor().Max(s.RowCount() - 1)
index := s.screen.Cursor().Position()
if index < 0 {
index = 0
} else if index > len(s.rows) {
index = len(s.rows) - 1
}
s.selectedIndex = index
}