This repository has been archived by the owner on Dec 9, 2023. It is now read-only.
/
termui.go
273 lines (236 loc) · 5.5 KB
/
termui.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
package platform
import (
"fmt"
"time"
"github.com/Phantas0s/termui"
)
type termUI struct {
body *termui.Grid
widgets []termui.GridBufferer
col []*termui.Row
row []*termui.Row
}
// NewTermUI returns a new Terminal Interface object with a given output mode.
func NewTermUI(d bool) (*termUI, error) {
if err := termui.Init(); err != nil {
return nil, err
}
termUI := termUI{
row: []*termui.Row{},
}
termui.Handle("/sys/wnd/resize", func(e termui.Event) {
termUI.Align()
termUI.Render()
})
termUI.Clean()
return &termUI, nil
}
// AddCol to the termui grid system.
func (t *termUI) AddCol(size int) {
t.col = append(t.col, termui.NewCol(size, 0, t.widgets...))
t.widgets = []termui.GridBufferer{}
}
// AddRow to the termui grid system.
func (t *termUI) AddRow() {
t.body.AddRows(termui.NewRow(t.col...))
t.Align()
}
func (t *termUI) Align() {
t.body.Width = termui.TermWidth()
t.body.Align()
}
// TextBox widget type.
func (t *termUI) TextBox(
data string,
textColor uint16,
borderColor uint16,
title string,
tc uint16,
height int,
multiline bool,
bold bool,
) {
textBox := termui.NewPar(data)
if title != "" {
textBox.BorderLabel = title
}
textBox.TextFgColor = termui.Attribute(textColor)
if bold {
textBox.TextFgColor = termui.Attribute(textColor) | termui.AttrBold
}
textBox.BorderFg = termui.Attribute(borderColor)
textBox.BorderLabelFg = termui.Attribute(tc)
textBox.Height = height
textBox.Multiline = multiline
t.widgets = append(t.widgets, textBox)
}
func (t *termUI) Gauge(
data float64,
textColor uint16,
barColor uint16,
borderColor uint16,
title string,
tc uint16,
height int,
) {
gauge := termui.NewGauge()
gauge.BarColor = termui.Attribute(barColor)
gauge.BorderFg = termui.Attribute(borderColor)
gauge.BorderLabelFg = termui.Attribute(tc)
gauge.PercentColor = termui.Attribute(textColor)
gauge.BorderLabel = title
gauge.Percent = data
gauge.Height = height
t.widgets = append(t.widgets, gauge)
}
// Title is a special TextBox widget type.
// TODO Should be replace with regular TextBox
func (t *termUI) Title(
title string,
textColor uint16,
borderColor uint16,
bold bool,
height int,
size int,
) {
pro := termui.NewPar(title)
pro.TextFgColor = termui.Attribute(textColor)
if bold {
pro.TextFgColor = termui.Attribute(textColor) | termui.AttrBold
}
pro.BorderFg = termui.Attribute(borderColor)
pro.Height = height
t.body.AddRows(termui.NewCol(size, 0, pro))
}
// BarChar widget type.
func (t *termUI) BarChart(
data []int,
dimensions []string,
title string,
tc uint16,
bd uint16,
fg uint16,
nc uint16,
enc uint16,
height int,
gap int,
barWidth int,
barColor uint16,
) {
bc := termui.NewBarChart()
bc.BorderLabel = title
bc.Data = data
bc.BorderLabelFg = termui.Attribute(tc)
bc.DataLabels = dimensions
bc.Height = height
bc.TextColor = termui.Attribute(fg)
bc.BorderFg = termui.Attribute(bd)
bc.BarWidth = barWidth
bc.BarColor = termui.Attribute(barColor)
bc.BarGap = gap
bc.NumColor = termui.Attribute(nc)
bc.EmptyNumColor = termui.Attribute(enc)
bc.Buffer()
t.widgets = append(t.widgets, bc)
}
// StackedBarChar widget type.
func (t *termUI) StackedBarChart(
data [8][]int,
dimensions []string,
title string,
tc uint16,
colors []uint16,
bd uint16,
fg uint16,
nc uint16,
height int,
gap int,
barWidth int,
) {
bc := termui.NewMBarChart()
bc.BorderLabel = title
bc.Data = data
bc.BorderLabelFg = termui.Attribute(tc)
bc.BarWidth = barWidth
bc.Height = height
bc.BarGap = gap
bc.DataLabels = dimensions
bc.TextColor = termui.Attribute(fg)
bc.BorderFg = termui.Attribute(bd)
bc.BarColor = [8]termui.Attribute{}
for k, v := range colors {
bc.BarColor[k] = termui.Attribute(v)
}
bc.NumColor = [8]termui.Attribute{termui.Attribute(nc), termui.Attribute(nc)}
t.widgets = append(t.widgets, bc)
}
// Table widget type.
func (t *termUI) Table(
data [][]string,
title string,
tc uint16,
bd uint16,
fg uint16,
) {
ta := termui.NewTable()
ta.Rows = data
ta.BorderLabel = title
ta.FgColor = termui.Attribute(fg)
ta.BorderLabelFg = termui.Attribute(tc)
ta.BorderFg = termui.Attribute(bd)
ta.SetSize()
t.widgets = append(t.widgets, ta)
}
// KQuit set a key to quit the application.
func (*termUI) KQuit(key string) {
termui.Handle(fmt.Sprintf("/sys/kbd/%s", key), func(termui.Event) {
termui.StopLoop()
})
}
// Hot reload
func (t *termUI) KHotReload(key string, c chan<- time.Time) {
termui.Handle(fmt.Sprintf("/sys/kbd/%s", key), func(e termui.Event) {
go func() {
// TODO wrap that into a function and pass it till here
c <- time.Now()
}()
})
}
// Key to edit a dashboard config.
// Need to stop the hot reload while editing the file.
// Automatically reload the dashboad after the edit is done.
func (t *termUI) KEdit(key string, editDashboard func()) {
termui.Handle(fmt.Sprintf("/sys/kbd/%s", key), func(e termui.Event) {
editDashboard()
})
}
// Loop termui to receive events.
func (t *termUI) Loop() {
termui.Loop()
}
// Render termui and delete the instance of the widgets rendered.
func (t *termUI) Render() {
termui.Render(t.body)
// delete every widget for the rows / cols rendered.
t.removeWidgets()
}
func (t *termUI) removeWidgets() {
t.row = []*termui.Row{}
t.col = []*termui.Row{}
}
// Clean and create a new empty grid.
func (t *termUI) Clean() {
t.body = termui.NewGrid()
t.body.X = 0
t.body.Y = 0
t.body.BgColor = termui.ThemeAttr("bg")
t.body.Width = termui.TermWidth()
}
// Close termui.
func (termUI) Close() {
termui.Close()
}
func (t *termUI) HotReload() {
t.Clean()
termui.Clear()
}