-
Notifications
You must be signed in to change notification settings - Fork 5
/
grid.go
405 lines (354 loc) · 8.54 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
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
403
404
405
package main
import (
"fmt"
"strconv"
"strings"
"sync"
"github.com/mattn/go-runewidth"
"github.com/nsf/termbox-go"
)
type TextAlign int
const (
AlignLeft TextAlign = iota
AlignRight
)
const (
titleOffset = 0
columnsOffset = 1
vHintsOffset = 2
dataOffset = 3
)
// CustomDrawFunc provides callback to draw a value
// Arguments are row index, column name and value and returns foreground and background attributes
type CustomDrawFunc func(int, string, string) (termbox.Attribute, termbox.Attribute)
type GridColumn struct {
Name string
Align TextAlign
Width int
}
// Format returns the formatted value for this column information
func (c *GridColumn) Format(s string, vars ...bool) string {
titleFormat := false
if len(vars) > 0 {
titleFormat = vars[0]
}
format := strconv.Itoa(c.Width) + "s"
if c.Align == AlignLeft {
format = "-" + format
}
format = "%" + format
ret := ""
if titleFormat {
ret = strings.Title(fmt.Sprintf(format, s))
}
ret = fmt.Sprintf(format, s)
if len(ret) > c.Width {
ret = ret[0:c.Width-3] + "..."
}
return ret
}
// ScrollableGrid represents the interface to arrange string data in tabular format
// Vertical and horizontal scrolling are provided by arrows keys
type ScrollableGrid struct {
Columns []GridColumn
VScroller bool
Title string
BP BufferProxy
visible bool
focused bool
dataStartY int
hScrollPos int
vScrollPos int
dataIndex int
bounds BufferRegion
dataBounds BufferRegion
data [][]string
customDrawFunc CustomDrawFunc
sync.RWMutex
}
func (s *ScrollableGrid) CurrentRow() []string {
s.RLock()
defer s.RUnlock()
if s.dataIndex >= 0 && s.dataIndex < len(s.data) {
return (s.data[s.dataIndex])[:]
}
return nil
}
func (s *ScrollableGrid) SetCustomDrawFunc(f CustomDrawFunc) {
s.customDrawFunc = f
}
func (s *ScrollableGrid) clearRow(y int, vars ...termbox.Attribute) {
bg := termbox.ColorDefault
if len(vars) > 0 {
bg = vars[0]
}
for i := s.dataBounds.X; i < s.dataBounds.X+s.dataBounds.W; i++ {
s.BP.SetCell(i, y, ' ', termbox.ColorDefault, bg)
}
}
func (s *ScrollableGrid) drawRow(cellY int, row []string, fg, bg termbox.Attribute, customizable bool, vars ...bool) {
titleFormat := false
if len(vars) > 0 {
titleFormat = true
}
totalLen := 0
dx := s.dataBounds.X
// always draw the first heading
for i := 0; i < len(row); i++ {
if i == 0 || i >= s.hScrollPos {
item := s.Columns[i].Format(row[i], titleFormat)
// text length
tw := len(item)
totalLen += tw
if totalLen > s.dataBounds.W {
break
}
if customizable && s.customDrawFunc != nil {
dataIndex := cellY - s.dataStartY
fg, bg := s.customDrawFunc(dataIndex, s.Columns[i].Name, row[i])
s.BP.WriteText(dx, cellY, fg, bg, item)
} else {
s.BP.WriteText(dx, cellY, fg, bg, item)
}
dx += tw
}
}
}
func (s *ScrollableGrid) drawBorder() {
fg := FGColor
bg := BGColor
if s.Focused() {
fg |= termbox.AttrBold
}
// top left
s.BP.SetCell(s.bounds.X, s.bounds.Y, '\u250c', fg, bg)
// top right
s.BP.SetCell(s.bounds.X+s.bounds.W, s.bounds.Y, '\u2510', fg, bg)
for i := 1; i < s.bounds.W; i++ {
// top line
s.BP.SetCell(s.bounds.X+i, s.bounds.Y, '\u2500', fg, bg)
// heading bottom
s.BP.SetCell(s.bounds.X+i, s.bounds.Y+columnsOffset+1, '\u2500', fg, bg)
// bottom line
s.BP.SetCell(s.bounds.X+i, s.bounds.Y+s.bounds.H-1, '\u2500', fg, bg)
}
for y := 1; y < s.bounds.H-1; y++ {
// left line
s.BP.SetCell(s.bounds.X, s.bounds.Y+y, '\u2502', fg, bg)
// right line
s.BP.SetCell(s.bounds.X+s.bounds.W, s.bounds.Y+y, '\u2502', fg, bg)
}
// left heading junction
s.BP.SetCell(s.bounds.X, s.bounds.Y+columnsOffset+1, '\u251c', fg, bg)
// right heading junction
s.BP.SetCell(s.bounds.X+s.bounds.W, s.bounds.Y+columnsOffset+1, '\u2524', fg, bg)
// bottom left
s.BP.SetCell(s.bounds.X, s.bounds.Y+s.bounds.H-1, '\u2514', fg, bg)
// bottom right
s.BP.SetCell(s.bounds.X+s.bounds.W, s.bounds.Y+s.bounds.H-1, '\u2518', fg, bg)
}
func (s *ScrollableGrid) drawHints() {
if !s.Focused() {
return
}
// horizontal scroller
s.BP.WriteText(s.bounds.X, s.bounds.Y+columnsOffset, FGColor|termbox.AttrBold, BGColor, "\u2190")
s.BP.WriteText(s.bounds.X+s.bounds.W, s.bounds.Y+columnsOffset, FGColor|termbox.AttrBold, BGColor, "\u2192")
// vertical scroller
if s.VScroller {
cx := (s.bounds.X + s.bounds.W) / 2
s.BP.WriteText(cx, s.dataBounds.Y-1, FGColor|termbox.AttrBold, BGColor, " \u2191 ")
s.BP.WriteText(cx, s.dataBounds.Y+s.dataBounds.H, FGColor|termbox.AttrBold, BGColor, " \u2193 ")
overlayHint := false
row := s.CurrentRow()
// first data cell is special
if row != nil && len(row) > 0 {
firstData := row[0]
overlayHint = len(firstData) > s.Columns[0].Width
}
if overlayHint {
s.BP.WriteText(s.bounds.X+1, s.dataBounds.Y+s.dataBounds.H, FGColor, termbox.AttrReverse, s.CurrentRow()[0])
}
}
}
func (s *ScrollableGrid) drawHeading() {
headers := []string{}
for _, c := range s.Columns {
headers = append(headers, c.Name)
}
highlightFGColor := FGColor
highlightBGColor := FGColor | termbox.AttrReverse
if !s.Focused() {
highlightBGColor = BGColor
}
s.clearRow(s.bounds.Y+columnsOffset, highlightBGColor)
s.drawRow(s.bounds.Y+columnsOffset, headers, highlightFGColor, highlightBGColor, false, true)
}
func (s *ScrollableGrid) drawData() {
dataLen := len(s.data)
if dataLen == 0 {
return
}
// start of data index
startDataIndex := s.vScrollPos
i := 0
for {
row := s.data[startDataIndex]
fg := FGColor
bg := BGColor
selectionIndex := s.dataIndex - s.vScrollPos
if selectionIndex == i && s.VScroller {
bg = BGSelectionColor
fg = FGSelectionColor
}
s.drawRow(s.dataBounds.Y+i, row, fg, bg, true)
startDataIndex++
i++
if i >= s.availableRowsSpace() || startDataIndex > dataLen-1 {
break
}
}
}
func (s *ScrollableGrid) drawTitle() {
cx := s.bounds.X + (s.bounds.W-runewidth.StringWidth(s.Title))/2
s.BP.WriteText(cx, s.bounds.Y+titleOffset, FGColor, FGColor, strings.ToUpper(s.Title))
}
func (s *ScrollableGrid) drawBuffer() {
s.RLock()
defer s.RUnlock()
s.drawHeading()
s.drawBorder()
s.drawTitle()
s.drawData()
s.drawHints()
}
func (s *ScrollableGrid) availableRowsSpace() int {
return s.dataBounds.H
}
func (s *ScrollableGrid) adjustScrollPos() {
dataLen := len(s.data)
if dataLen == 0 {
s.dataIndex = -1
s.vScrollPos = 0
return
}
// bound checking
if s.dataIndex < 0 {
s.dataIndex = 0
}
if s.dataIndex > dataLen-1 {
s.dataIndex = dataLen - 1
}
// scroll data when beyond viewport
delta := s.dataIndex - s.vScrollPos
if delta < 0 || delta > s.availableRowsSpace()-1 {
if delta > 0 {
delta = delta - s.availableRowsSpace() + 1
}
s.vScrollPos += delta
}
}
func (s *ScrollableGrid) Resize(bounds BufferRegion) {
s.bounds = bounds
s.dataBounds = BufferRegion{
bounds.X + 1,
bounds.Y + dataOffset,
bounds.W - 1,
bounds.H - dataOffset - 1,
}
s.adjustScrollPos()
s.Redraw()
}
func (s *ScrollableGrid) HandleEvent(ev termbox.Event) bool {
if !s.visible {
return false
}
switch ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyArrowLeft:
s.scrollLeft()
return true
case termbox.KeyArrowRight:
s.scrollRight()
return true
case termbox.KeyArrowUp:
s.scrollUp()
return true
case termbox.KeyArrowDown:
s.scrollDown()
return true
}
}
return false
}
func (s *ScrollableGrid) UpdateData(rows [][]string) {
s.Lock()
defer s.Unlock()
// clear data
s.data = [][]string{}
if len(rows) > 0 {
for _, row := range rows {
if len(row) == len(s.Columns) {
// copy the data
s.data = append(s.data, row[:])
}
}
}
s.adjustScrollPos()
}
func (s *ScrollableGrid) Redraw() {
if s.visible {
s.drawBuffer()
}
}
func (s *ScrollableGrid) SetFocus(v bool) {
s.focused = v
if s.visible {
s.Redraw()
}
}
func (s *ScrollableGrid) Focused() bool {
return s.focused
}
func (s *ScrollableGrid) SetVisible(v bool) {
s.visible = v
s.Redraw()
}
func (s *ScrollableGrid) Visible() bool {
return s.visible
}
func (s *ScrollableGrid) reset() {
s.hScrollPos = 1
s.vScrollPos = 0
s.dataIndex = 0
}
func (s *ScrollableGrid) scrollRight() {
s.hScrollPos++
max := len(s.Columns) - 1
if s.hScrollPos > max {
s.hScrollPos = max
}
s.Redraw()
}
func (s *ScrollableGrid) scrollLeft() {
s.hScrollPos--
if s.hScrollPos < 1 {
s.hScrollPos = 1
}
s.Redraw()
}
func (s *ScrollableGrid) scrollUp() {
if s.VScroller {
s.dataIndex--
s.adjustScrollPos()
s.Redraw()
}
}
func (s *ScrollableGrid) scrollDown() {
if s.VScroller {
s.dataIndex++
s.adjustScrollPos()
s.Redraw()
}
}