forked from moncho/dry
-
Notifications
You must be signed in to change notification settings - Fork 0
/
images.go
322 lines (280 loc) · 7.36 KB
/
images.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
package appui
import (
"fmt"
"sort"
"strings"
"sync"
gizaktermui "github.com/gizak/termui"
"github.com/moncho/dry/docker"
"github.com/moncho/dry/ui"
"github.com/moncho/dry/ui/termui"
)
var defaultImageTableHeader = imageTableHeader()
var imageTableHeaders = []SortableColumnHeader{
{`REPOSITORY`, docker.SortImagesByRepo},
{`TAG`, docker.NoSortImages},
{`ID`, docker.SortImagesByID},
{`Created`, docker.SortImagesByCreationDate},
{`Size`, docker.SortImagesBySize},
}
//DockerImagesWidget knows how render a container list
type DockerImagesWidget struct {
dockerDaemon docker.ImageAPI
filteredRows []*ImageRow
totalRows []*ImageRow
filterPattern string
header *termui.TableHeader
selectedIndex int
x, y int
height, width int
startIndex, endIndex int
sortMode docker.SortMode
mounted bool
sync.RWMutex
}
//NewDockerImagesWidget creates a renderer for a container list
func NewDockerImagesWidget(dockerDaemon docker.ImageAPI, y int) *DockerImagesWidget {
w := DockerImagesWidget{
y: y,
dockerDaemon: dockerDaemon,
header: defaultImageTableHeader,
height: MainScreenAvailableHeight(),
sortMode: docker.SortImagesByRepo,
width: ui.ActiveScreen.Dimensions.Width}
RegisterWidget(docker.ImageSource, &w)
return &w
}
//Buffer returns the content of this widget as a termui.Buffer
func (s *DockerImagesWidget) Buffer() gizaktermui.Buffer {
s.Lock()
defer s.Unlock()
y := s.y
buf := gizaktermui.NewBuffer()
if s.mounted {
s.prepareForRendering()
var filter string
if s.filterPattern != "" {
filter = fmt.Sprintf(
"<b><blue> | Active filter: </><yellow>%s</></> ", s.filterPattern)
}
widgetHeader := WidgetHeader("Images", s.RowCount(), filter)
widgetHeader.Y = y
buf.Merge(widgetHeader.Buffer())
y += widgetHeader.GetHeight()
s.updateHeader()
s.header.SetY(y)
buf.Merge(s.header.Buffer())
y += s.header.GetHeight()
selected := s.selectedIndex - s.startIndex
for i, imageRow := range s.visibleRows() {
imageRow.SetY(y)
y += imageRow.GetHeight()
if i != selected {
imageRow.NotHighlighted()
} else {
imageRow.Highlighted()
}
buf.Merge(imageRow.Buffer())
}
}
return buf
}
//Filter filters the image list by the given filter
func (s *DockerImagesWidget) Filter(filter string) {
s.Lock()
defer s.Unlock()
s.filterPattern = filter
}
//Mount tells this widget to be ready for rendering
func (s *DockerImagesWidget) Mount() error {
s.Lock()
defer s.Unlock()
if !s.mounted {
images, err := s.dockerDaemon.Images()
if err != nil {
return err
}
imageRows := make([]*ImageRow, len(images))
for i, image := range images {
imageRows[i] = NewImageRow(image, s.header)
}
s.totalRows = imageRows
s.mounted = true
s.align()
}
return nil
}
//Name returns this widget name
func (s *DockerImagesWidget) Name() string {
return "DockerImagesWidget"
}
//OnEvent runs the given command
func (s *DockerImagesWidget) OnEvent(event EventCommand) error {
if s.RowCount() > 0 {
return event(s.filteredRows[s.selectedIndex].image.ID)
}
return nil
}
//RowCount returns the number of rows of this widget.
func (s *DockerImagesWidget) RowCount() int {
return len(s.filteredRows)
}
//Sort rotates to the next sort mode.
//SortImagesByRepo -> SortImagesByID -> SortImagesByCreationDate -> SortImagesBySize -> SortImagesByRepo
func (s *DockerImagesWidget) Sort() {
s.RLock()
defer s.RUnlock()
switch s.sortMode {
case docker.SortImagesByRepo:
s.sortMode = docker.SortImagesByID
case docker.SortImagesByID:
s.sortMode = docker.SortImagesByCreationDate
case docker.SortImagesByCreationDate:
s.sortMode = docker.SortImagesBySize
case docker.SortImagesBySize:
s.sortMode = docker.SortImagesByRepo
}
s.mounted = false
}
//Unmount tells this widget that it will not be rendering anymore
func (s *DockerImagesWidget) Unmount() error {
s.RLock()
defer s.RUnlock()
s.mounted = false
return nil
}
//Align aligns rows
func (s *DockerImagesWidget) align() {
x := s.x
width := s.width
s.header.SetWidth(width)
s.header.SetX(x)
for _, image := range s.totalRows {
image.SetX(x)
image.SetWidth(width)
}
}
func (s *DockerImagesWidget) filterRows() {
if s.filterPattern != "" {
var rows []*ImageRow
for _, row := range s.totalRows {
if RowFilters.ByPattern(s.filterPattern)(row) {
rows = append(rows, row)
}
}
s.filteredRows = rows
} else {
s.filteredRows = s.totalRows
}
}
func (s *DockerImagesWidget) calculateVisibleRows() {
count := s.RowCount()
//no screen
if s.height < 0 || count == 0 {
s.startIndex = 0
s.endIndex = 0
return
}
selected := s.selectedIndex
//everything fits
if count <= s.height {
s.startIndex = 0
s.endIndex = count
return
}
//at the the start
if selected == 0 {
s.startIndex = 0
s.endIndex = s.height
} else if selected >= count-1 { //at the end
s.startIndex = count - s.height
s.endIndex = count
} else if selected == s.endIndex { //scroll down by one
s.startIndex++
s.endIndex++
} else if selected <= s.startIndex { //scroll up by one
s.startIndex--
s.endIndex--
} else if selected > s.endIndex { // scroll
s.startIndex = selected - s.height
s.endIndex = selected
}
}
//prepareForRendering sets the internal state of this widget so it is ready for
//rendering (i.e. Buffer()).
func (s *DockerImagesWidget) prepareForRendering() {
s.sortRows()
s.filterRows()
index := ui.ActiveScreen.Cursor.Position()
if index < 0 {
index = 0
} else if index > s.RowCount() {
index = s.RowCount() - 1
}
s.selectedIndex = index
s.calculateVisibleRows()
}
func (s *DockerImagesWidget) updateHeader() {
sortMode := s.sortMode
for _, c := range s.header.Columns {
colTitle := c.Text
var header SortableColumnHeader
if strings.Contains(colTitle, DownArrow) {
colTitle = colTitle[DownArrowLength:]
}
for _, h := range imageTableHeaders {
if colTitle == h.Title {
header = h
break
}
}
if header.Mode == sortMode {
c.Text = DownArrow + colTitle
} else {
c.Text = colTitle
}
}
}
func (s *DockerImagesWidget) sortRows() {
rows := s.totalRows
mode := s.sortMode
if s.sortMode == docker.NoSortImages {
return
}
var sortAlg func(i, j int) bool
switch mode {
case docker.SortImagesByRepo:
sortAlg = func(i, j int) bool {
if rows[i].Repository.Text != rows[j].Repository.Text {
return rows[i].Repository.Text < rows[j].Repository.Text
}
return rows[i].Tag.Text < rows[j].Tag.Text
}
case docker.SortImagesByID:
sortAlg = func(i, j int) bool {
return rows[i].ID.Text < rows[j].ID.Text
}
case docker.SortImagesByCreationDate:
sortAlg = func(i, j int) bool {
return rows[i].CreatedSinceValue > rows[j].CreatedSinceValue
}
case docker.SortImagesBySize:
sortAlg = func(i, j int) bool {
return rows[i].SizeValue < rows[j].SizeValue
}
}
sort.SliceStable(rows, sortAlg)
}
func (s *DockerImagesWidget) visibleRows() []*ImageRow {
return s.filteredRows[s.startIndex:s.endIndex]
}
func imageTableHeader() *termui.TableHeader {
header := termui.NewHeader(DryTheme)
header.ColumnSpacing = DefaultColumnSpacing
header.AddColumn(imageTableHeaders[0].Title)
header.AddColumn(imageTableHeaders[1].Title)
header.AddFixedWidthColumn(imageTableHeaders[2].Title, 12)
header.AddFixedWidthColumn(imageTableHeaders[3].Title, 12)
header.AddColumn(imageTableHeaders[4].Title)
return header
}