/
selectable_table.go
320 lines (283 loc) · 8.49 KB
/
selectable_table.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
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2022 Wrangle Ltd
package widgets
import (
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
// SelectableTable add following behaviors on top of VirtualTable:
// - Allow selecting cell and row (by selecting row number cell)
// - Define default selected style
type SelectableTable struct {
*VirtualTable
// This function is called to get the underlying table cell at any position
getCells func(row, column int) []*TableCell
// The currently selected row and column.
selectedRow, selectedColumn, selectedSubColumn int
// Minimum row and column index which can be selected.
minSelectedRow, minSelectedCol int
// An optional function which gets called when the user presses Enter on a
// selected cell. If entire rows selected, the column value is undefined.
// Likewise for entire columns.
selected func(row, column, subColumn int)
// An optional function which gets called when selection change
selectionChanged func()
inputHandler func(event *tcell.EventKey, setFocus func(p tview.Primitive)) bool
}
func NewSelectableTable() *SelectableTable {
t := &SelectableTable{
VirtualTable: NewVirtualTable(),
selectionChanged: func() {},
}
t.VirtualTable.SetGetCellsFunc(t.getStyledCells)
return t
}
func (t *SelectableTable) GetSelection() (row int, column int, subColumn int) {
return t.selectedRow, t.selectedColumn, t.selectedSubColumn
}
// SetGetCellsFunc set function to get table cell at a position
func (t *SelectableTable) SetGetCellsFunc(getCells func(row, column int) []*TableCell) *SelectableTable {
t.getCells = getCells
return t
}
// SetSelectedFunc sets a handler which is called whenever the user presses the
// Enter key on a selected cell/row/column. The handler receives the position of
// the selection and its cell contents. If entire rows are selected, the column
// index is undefined. Likewise for entire columns.
func (t *SelectableTable) SetSelectedFunc(handler func(row, column, subColumn int)) *SelectableTable {
t.selected = handler
return t
}
func (t *SelectableTable) SetSelectionChangedFunc(handler func()) *SelectableTable {
t.selectionChanged = handler
return t
}
// SetMinSelection tells table to prevent selection from going below certain index
func (t *SelectableTable) SetMinSelection(row, column int) *SelectableTable {
t.minSelectedRow, t.minSelectedCol = row, column
return t
}
// Select sets the selected cell. Depending on the selection settings
// specified via SetSelectable(), this may be an entire row or column, or even
// ignored completely. The "selection changed" event is fired if such a callback
// is available (even if the selection ends up being the same as before and even
// if cells are not selectable).
func (t *SelectableTable) Select(row, column, subCol int) *SelectableTable {
t.selectedRow, t.selectedColumn, t.selectedSubColumn = row, column, subCol
return t
}
func (t *SelectableTable) Draw(screen tcell.Screen) {
t.Box.DrawForSubclass(screen, t)
_, _, _, height := t.Box.GetInnerRect()
// Clamp offsets.
if t.selectedRow >= t.fixedRows && t.selectedRow < t.fixedRows+t.rowOffset {
t.rowOffset = t.selectedRow - t.fixedRows
}
if t.borders {
if 2*(t.selectedRow+1-t.rowOffset) >= height {
t.rowOffset = t.selectedRow + 1 - height/2
}
} else {
if t.selectedRow+1-t.rowOffset >= height {
t.rowOffset = t.selectedRow + 1 - height
}
}
if t.selectedColumn < t.columnOffset {
t.columnOffset = t.selectedColumn
}
t.VirtualTable.KeepColumnInView(t.selectedColumn).
Draw(screen)
}
func (t *SelectableTable) getStyledCells(row, column int) []*TableCell {
if row < 0 || column < 0 || row >= t.rowCount || column >= t.columnCount {
return nil
}
cells := t.getCells(row, column)
if cells == nil {
return nil
}
for i, cell := range cells {
if row == t.selectedRow && (t.selectedColumn == 0 ||
(t.selectedColumn == column && (t.selectedSubColumn == i || len(cells) == 1))) {
cell.SetFlipped(true).SetTransparency(false)
} else {
cell.SetFlipped(false).SetTransparency(true)
}
}
return cells
}
func (t *SelectableTable) WrapInputHandler(f func(event *tcell.EventKey, setFocus func(p tview.Primitive)) bool) func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
t.inputHandler = f
return t.InputHandler()
}
func (t *SelectableTable) InputHandler() func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
return t.Box.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p tview.Primitive)) {
if t.inputHandler != nil {
if t.inputHandler(event, setFocus) {
return
}
}
key := event.Key()
rowCount, columnCount := t.VirtualTable.GetShape()
var (
updateSubCol = func(num int) {
t.selectedSubColumn = num
cells := t.getCells(t.selectedRow, t.selectedColumn)
if len(cells) == 1 {
t.selectedSubColumn = 0
}
}
home = func() {
t.selectedRow = t.minSelectedRow
t.selectedColumn = t.minSelectedCol
t.selectedSubColumn = 0
t.selectionChanged()
}
end = func() {
t.selectedRow = rowCount - 1
t.selectedColumn = columnCount - 1
t.selectedSubColumn = 0
t.selectionChanged()
}
down = func() {
t.selectedRow++
if t.selectedRow >= rowCount {
t.selectedRow = rowCount - 1
}
updateSubCol(t.selectedSubColumn)
t.selectionChanged()
}
up = func() {
t.selectedRow--
if t.selectedRow < t.minSelectedRow {
t.selectedRow = t.minSelectedRow
}
updateSubCol(t.selectedSubColumn)
t.selectionChanged()
}
left = func() {
if t.selectedSubColumn > 0 {
t.selectedSubColumn--
} else {
t.selectedColumn--
if t.selectedColumn < t.minSelectedCol {
t.selectedColumn = t.minSelectedCol
}
updateSubCol(1)
}
t.selectionChanged()
}
right = func() {
cells := t.getCells(t.selectedRow, t.selectedColumn)
if t.selectedSubColumn < len(cells)-1 {
t.selectedSubColumn++
} else {
t.selectedSubColumn = 0
t.selectedColumn++
if t.selectedColumn >= columnCount {
t.selectedColumn = columnCount - 1
}
}
t.selectionChanged()
}
pageDown = func() {
offsetAmount := t.visibleRows - t.fixedRows
if offsetAmount < 0 {
offsetAmount = 0
}
t.selectedRow += offsetAmount
if t.selectedRow >= rowCount {
t.selectedRow = rowCount - 1
}
updateSubCol(t.selectedSubColumn)
t.selectionChanged()
}
pageUp = func() {
offsetAmount := t.visibleRows - t.fixedRows
if offsetAmount < 0 {
offsetAmount = 0
}
t.selectedRow -= offsetAmount
if t.selectedRow < t.minSelectedRow {
t.selectedRow = t.minSelectedRow
}
updateSubCol(t.selectedSubColumn)
t.selectionChanged()
}
)
switch key {
case tcell.KeyRune:
switch event.Rune() {
case 'g':
home()
case 'G':
end()
case 'j':
down()
case 'k':
up()
case 'h':
left()
case 'l':
right()
}
case tcell.KeyHome:
home()
case tcell.KeyEnd:
end()
case tcell.KeyUp:
up()
case tcell.KeyDown:
down()
case tcell.KeyLeft:
left()
case tcell.KeyRight:
right()
case tcell.KeyPgDn, tcell.KeyCtrlF:
pageDown()
case tcell.KeyPgUp, tcell.KeyCtrlB:
pageUp()
case tcell.KeyEnter:
if t.selected != nil {
t.selected(t.selectedRow, t.selectedColumn, t.selectedSubColumn)
}
}
})
}
// MouseHandler returns the mouse handler for this primitive.
func (t *SelectableTable) MouseHandler() func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
return t.WrapMouseHandler(func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
x, y := event.Position()
if !t.InRect(x, y) {
return false, nil
}
switch action {
case tview.MouseLeftClick:
rowCount, columnCount := t.VirtualTable.GetShape()
selectEvent := true
row, column, subCol := t.cellAt(x, y)
if row >= 0 && row < rowCount && column >= 0 && column < columnCount {
cells := t.getStyledCells(row, column)
var cell *TableCell
if len(cells) == 1 {
cell = cells[0]
} else {
cell = cells[subCol]
}
if cell != nil && cell.Clicked != nil {
if noSelect := cell.Clicked(); noSelect {
selectEvent = false
}
}
}
if selectEvent && row >= t.minSelectedRow && column >= t.minSelectedCol {
t.Select(row, column, subCol)
}
setFocus(t)
consumed = true
default:
return t.VirtualTable.MouseHandler()(action, event, setFocus)
}
return
})
}