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
|
package termui
import (
"strings"
)
// Table tracks all the attributes of a Table instance
type Table struct {
*Block
Header []string
Rows [][]string
ColWidths []int
Cp []int // column position
Gap int // gap between columns
Cursor Color
UniqueCol int // the column used to identify the selected item
SelectedItem string // used to keep the cursor on the correct item if the data changes
SelectedRow int
TopRow int // used to indicate where in the table we are scrolled at
ColResizer func()
}
// NewTable returns a new Table instance
func NewTable() *Table {
t := &Table{
Block: NewBlock(),
Cursor: Theme.TableCursor,
SelectedRow: 0,
TopRow: 0,
UniqueCol: 0,
}
t.ColResizer = t.ColResize
return t
}
// ColResize is the default column resizer, but can be overriden.
func (t *Table) ColResize() {
// calculate gap size based on total width
t.Gap = 3
if t.X < 50 {
t.Gap = 1
} else if t.X < 75 {
t.Gap = 2
}
cur := 0
for _, w := range t.ColWidths {
cur += t.Gap
t.Cp = append(t.Cp, cur)
cur += w
}
}
// Buffer implements the Bufferer interface.
func (t *Table) Buffer() *Buffer {
buf := t.Block.Buffer()
// makes sure there isn't a gap at the bottom of the table view
if t.TopRow > len(t.Rows)-(t.Y-1) {
t.TopRow = len(t.Rows) - (t.Y - 1)
}
t.ColResizer()
// print header
// for i := 0; i < render; i++ {
for i, width := range t.ColWidths {
if width == 0 {
break
}
r := MaxString(t.Header[i], t.X-6)
buf.SetString(t.Cp[i], 1, r, t.Fg|AttrBold, t.Bg)
}
// prints each row
for rowNum := t.TopRow; rowNum < t.TopRow+t.Y-1 && rowNum < len(t.Rows); rowNum++ {
row := t.Rows[rowNum]
y := (rowNum + 2) - t.TopRow
// cursor
bg := t.Bg
if (t.SelectedItem == "" && rowNum == t.SelectedRow) || (t.SelectedItem != "" && t.SelectedItem == row[t.UniqueCol]) {
bg = t.Cursor
for _, width := range t.ColWidths {
if width == 0 {
break
}
buf.SetString(1, y, strings.Repeat(" ", t.X), t.Fg, bg)
}
t.SelectedItem = row[t.UniqueCol]
t.SelectedRow = rowNum
}
// prints each col of the row
for i, width := range t.ColWidths {
if width == 0 {
break
}
r := MaxString(row[i], t.X-6)
buf.SetString(t.Cp[i], y, r, t.Fg, bg)
}
}
return buf
}
////////////////////////////////////////////////////////////////////////////////
// Cursor movement
// calcPos is used to calculate the cursor position and where in the process list we are located.
func (t *Table) calcPos() {
t.SelectedItem = ""
if t.SelectedRow < 0 {
t.SelectedRow = 0
}
if t.SelectedRow < t.TopRow {
t.TopRow = t.SelectedRow
}
if t.SelectedRow > len(t.Rows)-1 {
t.SelectedRow = len(t.Rows) - 1
}
if t.SelectedRow > t.TopRow+(t.Y-2) {
t.TopRow = t.SelectedRow - (t.Y - 2)
}
}
func (t *Table) Up() {
t.SelectedRow -= 1
t.calcPos()
}
func (t *Table) Down() {
t.SelectedRow += 1
t.calcPos()
}
func (t *Table) Top() {
t.SelectedRow = 0
t.calcPos()
}
func (t *Table) Bottom() {
t.SelectedRow = len(t.Rows) - 1
t.calcPos()
}
func (t *Table) HalfPageUp() {
t.SelectedRow = t.SelectedRow - (t.Y-2)/2
t.calcPos()
}
func (t *Table) HalfPageDown() {
t.SelectedRow = t.SelectedRow + (t.Y-2)/2
t.calcPos()
}
func (t *Table) PageUp() {
t.SelectedRow -= (t.Y - 2)
t.calcPos()
}
func (t *Table) PageDown() {
t.SelectedRow += (t.Y - 2)
t.calcPos()
}
func (t *Table) Click(x, y int) {
x = x - t.XOffset
y = y - t.YOffset
if (x > 0 && x <= t.X) && (y > 0 && y <= t.Y) {
t.SelectedRow = (t.TopRow + y) - 2
t.calcPos()
}
}
|