forked from bcicen/ctop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrid.go
233 lines (201 loc) · 4.57 KB
/
grid.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
package main
import (
"fmt"
"github.com/bcicen/ctop/config"
"github.com/bcicen/ctop/cwidgets/compact"
"github.com/bcicen/ctop/widgets"
ui "github.com/gizak/termui"
)
var cGrid = compact.NewCompactGrid()
func maxRows() int {
return ui.TermHeight() - 2 - cGrid.Y
}
type Grid struct {
cursorID string // id of currently selected container
cSource ContainerSource
containers Containers // sorted slice of containers
header *widgets.CTopHeader
}
func NewGrid() *Grid {
cs := NewDockerContainerSource()
g := &Grid{
cSource: cs,
containers: cs.All(),
header: widgets.NewCTopHeader(),
}
return g
}
// Set an initial cursor position, if possible
func (g *Grid) cursorReset() {
if len(g.containers) > 0 {
g.cursorID = g.containers[0].id
g.containers[0].widgets.Highlight()
}
}
// Return current cursor index
func (g *Grid) cursorIdx() int {
for n, c := range g.containers {
if c.id == g.cursorID {
return n
}
}
return 0
}
func (g *Grid) cursorUp() {
idx := g.cursorIdx()
// decrement if possible
if idx <= 0 {
return
}
active := g.containers[idx]
next := g.containers[idx-1]
active.widgets.UnHighlight()
g.cursorID = next.id
next.widgets.Highlight()
ui.Render(cGrid)
}
func (g *Grid) cursorDown() {
idx := g.cursorIdx()
// increment if possible
if idx >= (len(g.containers) - 1) {
return
}
if idx >= maxRows()-1 {
return
}
active := g.containers[idx]
next := g.containers[idx+1]
active.widgets.UnHighlight()
g.cursorID = next.id
next.widgets.Highlight()
ui.Render(cGrid)
}
func (g *Grid) redrawRows() {
// reinit body rows
cGrid.Clear()
// build layout
y := 1
if config.GetSwitchVal("enableHeader") {
g.header.SetCount(len(g.containers))
g.header.SetFilter(config.GetVal("filterStr"))
g.header.Render()
y += g.header.Height()
}
cGrid.SetY(y)
var cursorVisible bool
max := maxRows()
for n, c := range g.containers.Filter() {
if n >= max {
break
}
cGrid.Rows = append(cGrid.Rows, c.widgets)
if c.id == g.cursorID {
cursorVisible = true
}
}
if !cursorVisible {
g.cursorReset()
}
ui.Clear()
if config.GetSwitchVal("enableHeader") {
g.header.Render()
}
cGrid.Align()
ui.Render(cGrid)
}
// Log current container and widget state
func (g *Grid) dumpContainer() {
c, _ := g.cSource.Get(g.cursorID)
msg := fmt.Sprintf("logging state for container: %s\n", c.ShortID())
msg += fmt.Sprintf("id = %s\nname = %s\nstate = %s\n", c.id, c.name, c.state)
msg += inspect(&c.metrics)
log.Infof(msg)
}
//func (g *Grid) ExpandView() {
//ui.Clear()
//ui.DefaultEvtStream.ResetHandlers()
//defer ui.DefaultEvtStream.ResetHandlers()
//container, _ := g.cSource.Get(g.cursorID)
//// copy current widgets to restore on exit view
//curWidgets := container.widgets
//container.Expand()
//ui.Render(container.widgets)
//ui.Handle("/timer/1s", func(ui.Event) {
//ui.Render(container.widgets)
//})
//ui.Handle("/sys/kbd/", func(ui.Event) {
//ui.StopLoop()
//})
//ui.Loop()
//container.widgets = curWidgets
//container.widgets.Reset()
//}
func logEvent(e ui.Event) {
var s string
s += fmt.Sprintf("Type: %s\n", e.Type)
s += fmt.Sprintf("Path: %s\n", e.Path)
s += fmt.Sprintf("From: %s\n", e.From)
s += fmt.Sprintf("To: %s", e.To)
log.Debugf("new event:\n%s", s)
}
func Display(g *Grid) bool {
var menu func()
cGrid.SetWidth(ui.TermWidth())
ui.DefaultEvtStream.Hook(logEvent)
// initial draw
g.redrawRows()
ui.Handle("/sys/kbd/<up>", func(ui.Event) {
g.cursorUp()
})
ui.Handle("/sys/kbd/<down>", func(ui.Event) {
g.cursorDown()
})
ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
ui.StopLoop()
})
ui.Handle("/sys/kbd/a", func(ui.Event) {
config.Toggle("allContainers")
g.redrawRows()
})
ui.Handle("/sys/kbd/D", func(ui.Event) {
g.dumpContainer()
})
ui.Handle("/sys/kbd/f", func(ui.Event) {
menu = FilterMenu
ui.StopLoop()
})
ui.Handle("/sys/kbd/h", func(ui.Event) {
menu = HelpMenu
ui.StopLoop()
})
ui.Handle("/sys/kbd/H", func(ui.Event) {
config.Toggle("enableHeader")
g.redrawRows()
})
ui.Handle("/sys/kbd/q", func(ui.Event) {
ui.StopLoop()
})
ui.Handle("/sys/kbd/r", func(e ui.Event) {
config.Toggle("sortReversed")
})
ui.Handle("/sys/kbd/s", func(ui.Event) {
menu = SortMenu
ui.StopLoop()
})
ui.Handle("/timer/1s", func(e ui.Event) {
g.containers = g.cSource.All() // refresh containers for current sort order
g.redrawRows()
})
ui.Handle("/sys/wnd/resize", func(e ui.Event) {
g.header.Align()
cGrid.SetWidth(ui.TermWidth())
log.Infof("resize: width=%v max-rows=%v", cGrid.Width, maxRows())
g.redrawRows()
})
ui.Loop()
if menu != nil {
menu()
return false
}
return true
}