forked from marcusolsson/tui-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.go
185 lines (156 loc) · 3.48 KB
/
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
package tui
import "image"
var _ Widget = &Table{}
// Table is a widget that lays out widgets in a table.
type Table struct {
selected int
onItemActivated func(*Table)
onSelectionChanged func(*Table)
*Grid
}
// NewTable returns a new Table.
func NewTable(cols, rows int) *Table {
return &Table{
Grid: NewGrid(cols, rows),
}
}
// Draw draws the table.
func (t *Table) Draw(p *Painter) {
s := t.Size()
if t.hasBorder {
border := 1
// Draw outmost border.
p.DrawRect(0, 0, s.X, s.Y)
// Draw column dividers.
var coloff int
for i := 0; i < t.cols-1; i++ {
x := t.colWidths[i] + coloff + border
p.DrawVerticalLine(x, 0, s.Y-1)
p.DrawRune(x, 0, '┬')
p.DrawRune(x, s.Y-1, '┴')
coloff = x
}
// Draw row dividers.
var rowoff int
for j := 0; j < t.rows-1; j++ {
y := t.rowHeights[j] + rowoff + border
p.DrawHorizontalLine(0, s.X-1, y)
p.DrawRune(0, y, '├')
p.DrawRune(s.X-1, y, '┤')
rowoff = y
}
// Polish the intersections.
rowoff = 0
for j := 0; j < t.rows-1; j++ {
y := t.rowHeights[j] + rowoff + border
coloff = 0
for i := 0; i < t.cols-1; i++ {
x := t.colWidths[i] + coloff + border
p.DrawRune(x, y, '┼')
coloff = x
}
rowoff = y
}
}
// Draw cell content.
for i := 0; i < t.cols; i++ {
for j := 0; j < t.rows; j++ {
style := "table.cell"
if j == t.selected {
style += ".selected"
}
p.WithStyle(style, func(p *Painter) {
pos := image.Point{i, j}
wp := t.mapCellToLocal(pos)
p.Translate(wp.X, wp.Y)
defer p.Restore()
if w, ok := t.cells[pos]; ok {
size := w.Size()
size.X = t.colWidths[i]
p.FillRect(0, 0, size.X, size.Y)
p.WithMask(image.Rectangle{
Min: image.Point{},
Max: size,
}, func(p *Painter) {
w.Draw(p)
})
}
})
}
}
}
// OnKeyEvent handles an event and propagates it to all children.
func (t *Table) OnKeyEvent(ev KeyEvent) {
if !t.IsFocused() {
return
}
switch ev.Key {
case KeyUp:
t.moveUp()
case KeyDown:
t.moveDown()
case KeyEnter:
if t.onItemActivated != nil {
t.onItemActivated(t)
}
}
switch ev.Rune {
case 'k':
t.moveUp()
case 'j':
t.moveDown()
}
}
func (t *Table) moveUp() {
if t.selected > 0 {
t.selected--
}
if t.onSelectionChanged != nil {
t.onSelectionChanged(t)
}
}
func (t *Table) moveDown() {
if t.selected < t.rows-1 {
t.selected++
}
if t.onSelectionChanged != nil {
t.onSelectionChanged(t)
}
}
// SetSelected changes the currently selected item.
func (t *Table) SetSelected(i int) {
t.selected = i
}
// Selected returns the index of the currently selected item.
func (t *Table) Selected() int {
return t.selected
}
// Select calls SetSelected and the OnSelectionChanged function.
func (t *Table) Select(i int) {
t.SetSelected(i)
if t.onSelectionChanged != nil {
t.onSelectionChanged(t)
}
}
// RemoveRow removes specific row from the table
func (t *Table) RemoveRow(index int) {
t.Grid.RemoveRow(index)
if t.selected == index {
t.selected = -1
} else if t.selected > index {
t.selected--
}
}
// RemoveRows removes all the rows added to the table.
func (t *Table) RemoveRows() {
t.Grid.RemoveRows()
t.selected = -1
}
// OnItemActivated sets the function that is called when an item was activated.
func (t *Table) OnItemActivated(fn func(*Table)) {
t.onItemActivated = fn
}
// OnSelectionChanged sets the function that is called when an item was selected.
func (t *Table) OnSelectionChanged(fn func(*Table)) {
t.onSelectionChanged = fn
}