-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquestion.go
302 lines (280 loc) · 6.82 KB
/
question.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
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
// Copyright (C) 2015-2020 the Gprovision Authors. All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// SPDX-License-Identifier: BSD-3-Clause
//
package cfa
import (
"fmt"
"gprovision/pkg/log"
"time"
)
const (
maxTimeouts = 2
)
//Like menu, but confirmation required. Re-displays menu if choice is not confirmed.
func (l *Lcd) MenuWithConfirm(desc string, items []LcdTxt, menuTime, confirmTime time.Duration, keyPolling bool) (Choice, Answer) {
if l == nil {
time.Sleep(menuTime + confirmTime)
return CHOICE_NONE, ANSWER_NA
}
var confirm Answer
var choice Choice
timeoutCount := 0
for confirm != ANSWER_YES {
choice = l.Menu(items, menuTime, keyPolling)
confirm = l.ConfirmChoice(desc, items, choice, confirmTime)
if choice == CHOICE_NONE && confirm == ANSWER_NA {
timeoutCount++
log.Logf("MenuWithConfirm(%s,...): timeout", desc)
}
if timeoutCount >= maxTimeouts {
log.Logf("MenuWithConfirm(%s,...): max timeouts", desc)
break
}
}
return choice, confirm
}
type Answer int
const (
ANSWER_NA Answer = iota //no choice made - timeout
ANSWER_NO
ANSWER_YES
)
//From %s, you chose %s. Are you certain?
// >No< Yes
func (l *Lcd) ConfirmChoice(desc string, items []LcdTxt, choice Choice, timeout time.Duration) Answer {
if l == nil {
time.Sleep(timeout)
return ANSWER_NA
}
if int(choice) >= len(items) || choice < -2 {
//out of range
return ANSWER_NA
}
var item LcdTxt
switch choice {
case CHOICE_NONE:
item = LcdTxt("(timeout - go back)")
case CHOICE_CANCEL:
item = LcdTxt("to cancel (go back)")
default:
item = items[choice]
}
txt := LcdTxt(fmt.Sprintf("From %s, you chose %s. Are you certain?", desc, item))
return l.YesNo(txt, timeout)
}
func (l *Lcd) YesNo(txt LcdTxt, timeout time.Duration) Answer {
if l == nil {
time.Sleep(timeout)
return ANSWER_NA
}
q, err := l.NewQuestion(txt, []LcdTxt{LcdTxt("No"), LcdTxt("Yes")})
if err != nil {
log.Logf("question error: %s", err)
return ANSWER_NA
}
choice := q.Ask(timeout)
var ans Answer
switch choice {
case 0:
ans = ANSWER_NO
case 1:
ans = ANSWER_YES
default:
ans = ANSWER_NA
}
return ans
}
type Question struct {
l *Lcd
txt LcdTxt
buttons radioButtonSet
choice Choice
debug bool
syncTick chan<- time.Time
}
func (l *Lcd) NewQuestion(txt LcdTxt, opts []LcdTxt) (*Question, error) {
var err error
q := &Question{
l: l,
txt: txt,
}
//set up buttons, which must fit on one line
err = q.createButtonSet(opts)
if err != nil {
return nil, err
}
return q, nil
}
const askUpdateCycle = time.Second
func (q *Question) Ask(timeout time.Duration) Choice {
done := make(chan struct{})
update := NewTicker(askUpdateCycle)
go func() {
time.Sleep(timeout)
close(done)
}()
return q.ask(done, update)
}
func (q *Question) ask(done chan struct{}, update *Ticker) Choice {
if q.l == nil {
<-done
return CHOICE_NONE
}
q.l.mutex.Lock()
defer q.l.mutex.Unlock()
err := q.l.clear()
if err != nil && q.l.DbgGeneral {
log.Logf("Question error: %s", err)
}
q.l.setLegend(Legend_VRX, true)
defer q.l.setLegend(LegendNone, true)
q.l.setupKeyReporting(false, true)
area := q.l.dims
b := NewBlurb(q.l, q.txt, Coord{}, Coord{Row: area.Row - 1, Col: q.l.Width()})
err = q.l.write(Coord{Row: area.Row}, q.buttons.render(), true)
if err != nil && q.l.DbgGeneral {
log.Logf("Question error: %s", err)
}
//loop until timeout, allowing q.txt to scroll horiz if necessary
q.choice = CHOICE_NONE
for q.choice == CHOICE_NONE {
select {
case <-done:
return CHOICE_NONE
default:
}
_, err = b.draw(false)
if err != nil && q.l.DbgGeneral {
log.Logf("Question error: %s", err)
}
if q.buttons.prev != q.buttons.selection {
q.buttons.prev = q.buttons.selection
err = q.l.write(Coord{Row: area.Row}, q.buttons.render(), true)
if err != nil && q.l.DbgGeneral {
log.Logf("Question error: %s", err)
}
}
evt := q.l.waitForEvent(update.C)
if evt != KEY_NO_KEY {
q.handleEvent(evt)
}
if q.syncTick != nil {
q.syncTick <- time.Now()
}
}
return q.choice
}
//Handle up/down/enter/exit
func (q *Question) handleEvent(k KeyActivity) {
max := len(q.buttons.btns) - 1
switch k {
case KEY_LL_RELEASE:
fallthrough
case KEY_RIGHT_RELEASE:
if q.buttons.selection >= max {
//wrap around
q.buttons.selection = 0
q.l.setLegend(Legend_VRX, false)
} else {
q.buttons.selection++
if q.buttons.selection == max {
q.l.setLegend(LegendLV_X, false)
} else {
q.l.setLegend(LegendLVRX, false)
}
}
case KEY_UL_RELEASE:
fallthrough
case KEY_LEFT_RELEASE:
if q.buttons.selection <= 0 {
q.buttons.selection = max
q.l.setLegend(LegendLV_X, false)
} else {
q.buttons.selection--
if q.buttons.selection == 0 {
q.l.setLegend(Legend_VRX, false)
} else {
q.l.setLegend(LegendLVRX, false)
}
}
case KEY_UR_RELEASE:
fallthrough
case KEY_ENTER_RELEASE:
q.choice = Choice(q.buttons.selection)
case KEY_LR_RELEASE:
fallthrough
case KEY_EXIT_RELEASE:
q.choice = CHOICE_CANCEL
default:
if q.debug {
log.Logf("Question.handleEvent: ignoring key code 0x%02x", k)
}
}
}
//an item to be displayed, only one of which can be selected at a time
type radioButton struct {
txt LcdTxt
}
func (rb *radioButton) render(selected bool, st *styleSet) LcdTxt {
style := st.deselected
if selected {
style = st.selected
}
l := LcdTxt{style[0]}
l = append(l, rb.txt...)
l = append(l, style[1])
return l
}
//set of radio buttons. must fit on one row of display.
type radioButtonSet struct {
btns []*radioButton
styles *styleSet
selection int
prev int //used to track when buttons need redrawn
}
func (q *Question) createButtonSet(opts []LcdTxt) error {
//determine if buttons can all fit on a line
//if not, error
w := q.l.Width()
if w < rbsMinWidth(opts) {
return ERange
}
for _, opt := range opts {
q.buttons.btns = append(q.buttons.btns, &radioButton{txt: opt})
}
q.buttons.styles = &styleSet{selected: wrapArrows, deselected: wrapNone}
q.buttons.prev = -1
return nil
}
//calculates width, with one space on each end and two between items
func rbsMinWidth(opts []LcdTxt) byte {
var w byte
for _, opt := range opts {
w += byte(len(opt)) + 2
}
return w
}
func (rbs *radioButtonSet) render() LcdTxt {
var line LcdTxt
for i, rb := range rbs.btns {
selection := false
if rbs.selection == i {
selection = true
}
line = append(line, rb.render(selection, rbs.styles)...)
}
return line
}
type wrapStyle [2]byte
var (
wrapNone = wrapStyle{0x20, 0x20} //spaces
wrapBrackets = wrapStyle{0xfa, 0xfc} // [ and ] - not ascii
wrapArrows = wrapStyle{0x10, 0x11} // like > and < , but filled in
wrapCorners = wrapStyle{0x96, 0x97} // corner brackets (apparently used as quotes in Japanese, Chinese, Korean)
)
type styleSet struct {
selected, deselected wrapStyle
}