-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpage.go
236 lines (197 loc) · 6.01 KB
/
page.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
package minigo
const (
NoOp = iota - 50
DisconnectOp
QuitOp
EnvoiOp
SommaireOp
AnnulationOp
RetourOp
SuiteOp
RepetitionOp
GuideOp
CorrectionOp
)
type InitFunc func(mntl *Minitel, inputs *Form, initData map[string]string) int
type KeyboardFunc func(mntl *Minitel, inputs *Form, key rune)
type NavigationFunc func(mntl *Minitel, inputs *Form) (map[string]string, int)
type ConnexionFinFunc func(mntl *Minitel) int
type Page struct {
mntl *Minitel
name string
initData map[string]string
form *Form
initFunc InitFunc
charFunc KeyboardFunc
connexionFinFunc ConnexionFinFunc
envoiFunc NavigationFunc
sommaireFunc NavigationFunc
annulationFunc NavigationFunc
retourFunc NavigationFunc
repetitionFunc NavigationFunc
guideFunc NavigationFunc
correctionFunc NavigationFunc
suiteFunc NavigationFunc
hautFunc NavigationFunc
basFunc NavigationFunc
droiteFunc NavigationFunc
gaucheFunc NavigationFunc
}
func NewPage(name string, mntl *Minitel, initData map[string]string) *Page {
return &Page{
mntl: mntl,
name: name,
initData: initData,
initFunc: func(mntl *Minitel, inputs *Form, initData map[string]string) int { return NoOp },
charFunc: func(mntl *Minitel, inputs *Form, key rune) {},
connexionFinFunc: defaultConnexionFinHandlerFunc,
envoiFunc: defaultNavigationHandlerFunc,
sommaireFunc: defaultNavigationHandlerFunc,
annulationFunc: defaultNavigationHandlerFunc,
retourFunc: defaultNavigationHandlerFunc,
repetitionFunc: defaultNavigationHandlerFunc,
guideFunc: defaultNavigationHandlerFunc,
correctionFunc: defaultNavigationHandlerFunc,
suiteFunc: defaultNavigationHandlerFunc,
hautFunc: defaultNavigationHandlerFunc,
basFunc: defaultNavigationHandlerFunc,
droiteFunc: defaultNavigationHandlerFunc,
gaucheFunc: defaultNavigationHandlerFunc,
}
}
func defaultConnexionFinHandlerFunc(mntl *Minitel) int {
mntl.CleanScreen()
mntl.PrintLeftAt(1, "→ Déconnexion demandée, à bientôt !")
return DisconnectOp
}
func defaultNavigationHandlerFunc(mntl *Minitel, input *Form) (map[string]string, int) {
return nil, NoOp
}
func (p *Page) SetInitFunc(f InitFunc) {
p.initFunc = f
}
func (p *Page) SetCharFunc(f KeyboardFunc) {
p.charFunc = f
}
func (p *Page) SetEnvoiFunc(f NavigationFunc) {
p.envoiFunc = f
}
func (p *Page) SetSommaireFunc(f NavigationFunc) {
p.sommaireFunc = f
}
func (p *Page) SetAnnulationFunc(f NavigationFunc) {
p.annulationFunc = f
}
func (p *Page) SetRetourFunc(f NavigationFunc) {
p.retourFunc = f
}
func (p *Page) SetRepetitionFunc(f NavigationFunc) {
p.repetitionFunc = f
}
func (p *Page) SetGuideFunc(f NavigationFunc) {
p.guideFunc = f
}
func (p *Page) SetCorrectionFunc(f NavigationFunc) {
p.correctionFunc = f
}
func (p *Page) SetSuiteFunc(f NavigationFunc) {
p.suiteFunc = f
}
func (p *Page) SetHautFunc(f NavigationFunc) {
p.hautFunc = f
}
func (p *Page) SetBasFunc(f NavigationFunc) {
p.basFunc = f
}
func (p *Page) SetDroiteFunc(f NavigationFunc) {
p.droiteFunc = f
}
func (p *Page) SetGaucheFunc(f NavigationFunc) {
p.gaucheFunc = f
}
func (p *Page) SetConnexionFinFunc(f ConnexionFinFunc) {
p.connexionFinFunc = f
}
func (p *Page) Run() (map[string]string, int) {
p.form = &Form{}
if op := p.initFunc(p.mntl, p.form, p.initData); op != NoOp {
return nil, op
}
for p.mntl.IsConnected() {
key := <-p.mntl.Messages
switch key {
case Envoi:
if out, op := p.envoiFunc(p.mntl, p.form); op != NoOp {
infoLog.Printf("page: key Envoi: quit %s page, with op=%d\n", p.name, op)
return out, op
}
case Sommaire:
if out, op := p.sommaireFunc(p.mntl, p.form); op != NoOp {
infoLog.Printf("page: key Sommaire: quit %s page, with op=%d\n", p.name, op)
return out, op
}
case Annulation:
if out, op := p.annulationFunc(p.mntl, p.form); op != NoOp {
infoLog.Printf("page: key Annulation: quit %s page, with op=%d\n", p.name, op)
return out, op
}
case Retour:
if out, op := p.retourFunc(p.mntl, p.form); op != NoOp {
infoLog.Printf("page: key Retour: quit %s page, with op=%d\n", p.name, op)
return out, op
}
case Repetition:
if out, op := p.repetitionFunc(p.mntl, p.form); op != NoOp {
infoLog.Printf("page: key Repetition: quit %s page, with op=%d\n", p.name, op)
return out, op
}
case Guide:
if out, op := p.guideFunc(p.mntl, p.form); op != NoOp {
infoLog.Printf("page: key Guide: quit %s page, with op=%d\n", p.name, op)
return out, op
}
case Correction:
if out, op := p.correctionFunc(p.mntl, p.form); op != NoOp {
infoLog.Printf("page: key Correction: quit %s page, with op=%d\n", p.name, op)
return out, op
}
case Suite:
if out, op := p.suiteFunc(p.mntl, p.form); op != NoOp {
infoLog.Printf("page: key Suite: quit %s page, with op=%d\n", p.name, op)
return out, op
}
case ToucheFlecheHaut:
if out, op := p.hautFunc(p.mntl, p.form); op != NoOp {
infoLog.Printf("page: key Haut: quit %s page, with op=%d\n", p.name, op)
return out, op
}
case ToucheFlecheBas:
if out, op := p.basFunc(p.mntl, p.form); op != NoOp {
infoLog.Printf("page: key Bas: quit %s page, with op=%d\n", p.name, op)
return out, op
}
case ToucheFlecheDroite:
if out, op := p.droiteFunc(p.mntl, p.form); op != NoOp {
infoLog.Printf("page: key Droite: quit %s page, with op=%d\n", p.name, op)
return out, op
}
case ToucheFlecheGauche:
if out, op := p.gaucheFunc(p.mntl, p.form); op != NoOp {
infoLog.Printf("page: key Gauche: quit %s page, with op=%d\n", p.name, op)
return out, op
}
case ConnexionFin:
if op := p.connexionFinFunc(p.mntl); op != NoOp {
infoLog.Printf("page: key ConnexionFin: quit page %s, with op=%d\n", p.name, op)
return nil, op
}
default:
if ValidRune(key) {
p.charFunc(p.mntl, p.form, key)
} else {
errorLog.Printf("page: invalid rune=%x\n", key)
}
}
}
return nil, DisconnectOp
}