-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.go
245 lines (206 loc) · 6.5 KB
/
ui.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
package main
import (
"fmt"
"log"
"strings"
"time"
ui "github.com/gizak/termui/v3"
"github.com/gizak/termui/v3/widgets"
)
// RunUI updates the GUI
func RunUI(cfg Config, inStatusCh chan inputStats, outputStatusChannel chan outputStats, cfgSource string) {
// Let the goroutines initialize before starting GUI
time.Sleep(50 * time.Millisecond)
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
y := 0
height := 5
width := 120
halfWidth := width / 2
p := widgets.NewParagraph()
p.Title = applicationName()
p.Text = fmt.Sprintf("PRESS q TO QUIT.\nConfig from: %s\n", cfgSource)
p.SetRect(0, y, width, height)
p.TextStyle.Fg = ui.ColorWhite
p.BorderStyle.Fg = ui.ColorCyan
y += height
height = 10
inSrcHeight := height
if cfg.RetransmitEnabled() {
inSrcHeight = height * 2
}
inpSrcStatus := widgets.NewParagraph()
inpSrcStatus.Title = "GPS/GPS Compass in"
if cfg.InputEnabled() {
inpSrcStatus.Text = "Waiting for data"
} else {
inpSrcStatus.Text = "Input not enabled"
}
inpSrcStatus.SetRect(0, y, halfWidth, y+inSrcHeight)
inpSrcStatus.TextStyle.Fg = ui.ColorGreen
inpSrcStatus.BorderStyle.Fg = ui.ColorCyan
inpArrow := widgets.NewParagraph()
inpArrow.Border = false
inpArrow.Text = "=>"
inpArrow.SetRect(halfWidth, y, halfWidth+5, y+height)
inpDestStatus := widgets.NewParagraph()
inpDestStatus.Title = "GPS/GPS Compass out to UGPS"
inpDestStatus.SetRect(halfWidth+5, y, width, y+height)
inpDestStatus.TextStyle.Fg = ui.ColorGreen
inpDestStatus.BorderStyle.Fg = ui.ColorCyan
inpRetransmitStatus := widgets.NewParagraph()
if cfg.RetransmitEnabled() {
inpRetransmitStatus.Title = "Retransmit Input"
inpRetransmitStatus.SetRect(halfWidth+5, y+height, width, y+inSrcHeight)
inpRetransmitStatus.TextStyle.Fg = ui.ColorGreen
inpRetransmitStatus.BorderStyle.Fg = ui.ColorCyan
}
//y += height
y += inSrcHeight
height = 10
outSrcStatus := widgets.NewParagraph()
outSrcStatus.Title = "Locator Position in from UGPS"
outSrcStatus.Text = "Waiting for data"
if !cfg.OutputEnabled() {
outSrcStatus.Text = "Output not enabled"
}
outSrcStatus.SetRect(0, y, halfWidth, y+height)
outSrcStatus.TextStyle.Fg = ui.ColorGreen
outSrcStatus.BorderStyle.Fg = ui.ColorCyan
outArrow := widgets.NewParagraph()
outArrow.Border = false
outArrow.Text = "=>"
outArrow.SetRect(halfWidth, y, halfWidth+5, y+height)
outDestStatus := widgets.NewParagraph()
outDestStatus.Title = "Locator Position out to NMEA"
outDestStatus.Text = "Waiting for data"
if !cfg.OutputEnabled() {
outDestStatus.Text = "Output not enabled"
}
outDestStatus.SetRect(halfWidth+5, y, width, y+height)
outDestStatus.TextStyle.Fg = ui.ColorGreen
outDestStatus.BorderStyle.Fg = ui.ColorCyan
y += height
height = 15
dbgText := widgets.NewList()
dbgText.Title = "Debug"
dbgText.Rows = dbgMsg
dbgText.WrapText = true
dbgText.SetRect(0, y, width, y+height)
dbgText.BorderStyle.Fg = ui.ColorCyan
hideDebug := widgets.NewParagraph()
hideDebug.Text = ""
hideDebug.SetRect(0, y, width, y+height)
hideDebug.Border = false
draw := func() {
ui.Render(p, inpSrcStatus, inpArrow, inpDestStatus, outSrcStatus, outArrow, outDestStatus, inpRetransmitStatus)
if debug {
dbgText.Rows = dbgMsg
ui.Render(dbgText)
} else {
ui.Render(hideDebug)
}
}
// Initial draw before any events have occurred
draw()
uiEvents := ui.PollEvents()
for {
select {
case inStats := <-inStatusCh:
inpSrcStatus.TextStyle.Fg = ui.ColorGreen
inpSrcStatus.Text = fmt.Sprintf("Source: %s\n\n", cfg.Input.Device) +
"Supported NMEA sentences received:\n" +
fmt.Sprintf(" * Topside Position : %s\n", inStats.src.posDesc) +
fmt.Sprintf(" * Topside Heading : %s\n", inStats.src.headDesc) +
fmt.Sprintf(" * Parse error: %d\n\n", inStats.src.unparsableCount) +
inStats.src.errorMsg
if inStats.src.errorMsg != "" {
inpSrcStatus.TextStyle.Fg = ui.ColorRed
}
inpDestStatus.TextStyle.Fg = ui.ColorGreen
inpDestStatus.Text = fmt.Sprintf("Destination: %s\n\n", cfg.BaseURL) +
fmt.Sprintf("Sent successfully to\n Underwater GPS: %d\n\n", inStats.dst.sendOk) +
inStats.dst.errorMsg
if inStats.dst.errorMsg != "" {
inpDestStatus.TextStyle.Fg = ui.ColorRed
}
inpRetransmitStatus.Text = fmt.Sprintf("Destination: %s\n\n", cfg.Input.Retransmit) +
fmt.Sprintf("Count: %d\n%s", inStats.retransmit.count, inStats.retransmit.errorMsg)
inpRetransmitStatus.TextStyle.Fg = ui.ColorGreen
if inStats.retransmit.errorMsg != "" {
inpRetransmitStatus.TextStyle.Fg = ui.ColorRed
}
draw()
case outStats := <-outputStatusChannel:
outSrcStatus.Text = fmt.Sprintf("Source: %s\n\n", cfg.BaseURL) +
fmt.Sprintf("Positions from Underwater GPS:\n %d\n", outStats.src.getCount)
outSrcStatus.TextStyle.Fg = ui.ColorGreen
if outStats.src.errMsg != "" {
outSrcStatus.TextStyle.Fg = ui.ColorRed
outSrcStatus.Text += fmt.Sprintf("\n\n%v (%d)", outStats.src.errMsg, outStats.src.getErr)
}
outDestStatus.Text = fmt.Sprintf("Destination: %s\n\n", cfg.Output.Device) +
"Sent:\n" +
fmt.Sprintf(" * Locator/ROV Position : %s: %d\n", strings.ToUpper(cfg.Output.PositionSentence), outStats.dst.sendOk)
outDestStatus.TextStyle.Fg = ui.ColorGreen
if outStats.dst.errMsg != "" {
outDestStatus.TextStyle.Fg = ui.ColorRed
outDestStatus.Text += fmt.Sprintf("\n\n%s", outStats.dst.errMsg)
}
draw()
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
return
case "d":
dbgMsg = nil
dbgText.Rows = dbgMsg
debug = !debug
draw()
}
}
}
}
// RunUIError shows a dialog box with an error message
func RunUIError(message string) {
// Let the goroutines initialize before starting GUI
time.Sleep(50 * time.Millisecond)
if err := ui.Init(); err != nil {
log.Fatalf("failed to initialize termui: %v", err)
}
defer ui.Close()
y := 0
height := 7
width := 80
p := widgets.NewParagraph()
p.Title = applicationName()
p.Text = "PRESS q TO QUIT."
p.SetRect(0, y, width, height)
y += height
p.TextStyle.Fg = ui.ColorWhite
p.BorderStyle.Fg = ui.ColorCyan
errorPara := widgets.NewParagraph()
errorPara.Title = "Error occurred"
errorPara.Text = message
height = 10
errorPara.SetRect(0, y, width, y+height)
errorPara.TextStyle.Fg = ui.ColorRed
errorPara.BorderStyle.Fg = ui.ColorCyan
draw := func() {
ui.Render(p, errorPara)
}
// Intial draw before any events have occured
draw()
uiEvents := ui.PollEvents()
for {
select {
case e := <-uiEvents:
switch e.ID {
case "q", "<C-c>":
return
}
}
}
}