-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwidget_table.go
189 lines (143 loc) · 3.02 KB
/
widget_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
// Copyright 2015 Ghislain Guiot <[email protected]>. All rights reserved.
// Use of this source code is governed by a MIT license that can
// be found in the LICENSE file.
package termutil
import (
"github.com/nsf/termbox-go"
)
type WidgetTable struct {
Header *Header
Body *Body
}
func (wg *WidgetTable) Update(win *Window) []Row {
wg.Header.fillSpace()
var out []Row
out = append(out, wg.Header.buildRow(win.SizeX))
out = append(out, wg.Body.buildRows(wg.Header.Titles, win.SizeX, win.SizeY-1)...)
return out
}
func (wg *WidgetTable) EventFunc(ev termbox.Event) {
switch ev.Type {
case termbox.EventKey:
switch ev.Key {
case termbox.KeyArrowDown:
if wg.Body.Active < len(wg.Body.Data)-1 {
wg.Body.Active += 1
}
case termbox.KeyArrowUp:
if wg.Body.Active > 0 {
wg.Body.Active -= 1
}
}
}
}
type Header struct {
Titles []HeaderTitle
Fg termbox.Attribute
Bg termbox.Attribute
// Colors for the active tab
FgActive termbox.Attribute
BgActive termbox.Attribute
Active int
IsActive bool // Is a tab activated
}
func (h Header) buildRow(sizeX int) Row {
var fg termbox.Attribute
var bg termbox.Attribute
row := make(Row, sizeX)
for it, ir := 0, 0; it < len(h.Titles) && ir < sizeX; it++ {
size := h.Titles[it].Per * sizeX / 100
if h.IsActive && h.Active == it {
fg = h.FgActive
bg = h.BgActive
} else {
fg = h.Fg
bg = h.Bg
}
name := h.Titles[it].Name
for i := 0; i < size && ir < sizeX; i++ {
cell := Cell{
Fg: fg,
Bg: bg,
}
if i < len(name) {
cell.C = rune(name[i])
} else {
cell.C = ' '
}
row[ir] = cell
ir++
}
}
return row
}
// fillSpace replaces the automatic (0) percentages to fill the space
func (h *Header) fillSpace() {
var num int
var tot int
for _, title := range h.Titles {
tot += title.Per
if title.Per == 0 {
num += 1
}
}
if num == 0 {
return
}
num = (100 - tot) / num
for it := 0; it < len(h.Titles); it++ {
if h.Titles[it].Per == 0 {
h.Titles[it].Per = num
}
}
}
type HeaderTitle struct {
Name string
// Percentage of size
// When equal to 0, it takes the bigger space available
Per int
}
type Body struct {
Data [][]string
Active int
Fg termbox.Attribute
Bg termbox.Attribute
// Colors for the active row
FgActive termbox.Attribute
BgActive termbox.Attribute
}
func (b Body) buildRows(h []HeaderTitle, sizeX, sizeY int) []Row {
var fg termbox.Attribute
var bg termbox.Attribute
rows := make([]Row, sizeY)
for ib := 0; ib < len(b.Data) && ib < sizeY; ib++ {
d := b.Data[ib]
row := make(Row, sizeX)
if b.Active == ib {
fg = b.FgActive
bg = b.BgActive
} else {
fg = b.Fg
bg = b.Bg
}
for it, ir := 0, 0; it < len(d) && ir < sizeX; it++ {
size := h[it].Per * sizeX / 100
name := d[it]
for i := 0; i < size && ir < sizeX; i++ {
cell := Cell{
Fg: fg,
Bg: bg,
}
if i < len(name) {
cell.C = rune(name[i])
} else {
cell.C = ' '
}
row[ir] = cell
ir++
}
}
rows[ib] = row
}
return rows
}