-
Notifications
You must be signed in to change notification settings - Fork 0
/
ui.go
executable file
·286 lines (254 loc) · 6.32 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
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
package main
// Deals with the UI for the program. It probably will stay a console-based program though
// because I personally feel that coders should be familiar with a terminal.
import (
"strconv"
"os"
"fmt"
)
func wr(s...interface{}) {
for _,v := range s {
fmt.Print(v)
}
fmt.Println()
}
type MenuStack []UIScreen
func (m MenuStack) Cur() UIScreen {
return m[len(m)-1]
}
func (m*MenuStack) Push(u UIScreen) {
*m = append(*m, u)
}
func (m*MenuStack) Pop() {
*m = (*m)[:len(*m)-1]
}
type Action struct {
Name string
Act func(o Out) (UIScreen, error)
}
type UIScreen interface {
Choices(o Out) []Action
}
var DontClear = dontClear{}
type dontClear struct{}
func (dontClear) Error() string {
return ""
}
var PopParent = popParent{}
type popParent struct{}
func (popParent) Choices(o Out) []Action { return nil }
type NotImplemented string
func (n NotImplemented) Choices(o Out) []Action {
o(string(n), " is not yet implemented, sorry")
return nil
}
type DoubleCheck struct {
msg string
thing func(Out) error
}
func (d DoubleCheck) Choices(o Out) []Action {
o("Are you sure you want to ", d.msg, "?")
return []Action{
{"Yes, of course", func(o Out) (UIScreen, error) {
return PopParent, d.thing(o)}},
}
}
func StartUI(input func()string) {
var menus MenuStack
var last string
if err := Load(); err != nil {
wr("There was an error with loading user data: ", err)
wr("If this is your first time running the program, ignore this!")
wr("Otherwise, existing data will be overwritten on next save")
fmt.Print("Press enter...")
input()
}
if U.DoneTutorial {
menus.Push(MainUI{})
} else {
menus.Push(TutUI{})
U.DoneTutorial = true
}
var lastErr error
for len(menus) > 0{
if lastErr != nil {
if lastErr != DontClear {
wr(lastErr)
}
lastErr = nil
} else {
ClearScreen()
}
chs := menus.Cur().Choices(wr)
if len(chs) == 0 {
if chs == nil {
fmt.Print("\n\nPress enter...")
input()
}
menus.Pop()
continue
}
wr()
for i,v := range chs {
wr(i, ": ", v.Name)
}
if len(menus) > 1 {
wr(len(chs), ": Back")
}
wr()
fmt.Print("Do what? ")
line := input()
if line == "" {
if last == "" {
continue
}
line = last
} else {
last = line
}
if ind, err := strconv.Atoi(line); err != nil && line != ".." {
lastErr = fmt.Errorf("not a valid choice: %v", line)
} else if ind < 0 || ind > len(chs) {
lastErr = fmt.Errorf("index out of bounds: %v", ind)
} else if ind == len(chs) || line == ".." {
menus.Pop()
} else {
var next UIScreen
next, lastErr = chs[ind].Act(wr)
// Save lastErr until the next loop iteration
if next == PopParent {
menus.Pop()
} else if next != nil {
menus.Push(next)
}
}
}
}
type TutUI struct{}
func (TutUI) Choices(o Out) []Action {
o(`Welcome to Kevin's Learning Thing!
This program helps you learn the Go programming language by throwing you
straight into it and asking questions.
Many problems have hints you can unlock and some even have links to online
information. When in doubt, Google it! A search engine can be a programmer's
most helpful resource.
The goal is to solve as many problems as you can, learning Go as you go!
`)
U.DoneTutorial = true
// Make the workspace directory
os.Mkdir(Workspace, 0)
Save()
return []Action {
{ "Press '0' and then 'Enter' or 'Return' to continue",
func(Out) (UIScreen, error) { return MainUI{}, nil } },
}
}
func Choice(u UIScreen) func(Out) (UIScreen, error) {
return func(Out) (UIScreen, error) {
return u, nil
}
}
type MainUI struct {}
func (MainUI) Choices(o Out) []Action {
o("Welcome to Kevin's Learning Thing!")
return []Action {
{ "Problems", Choice(ProblemList{})},
{ "Stats", Choice(Stats{})},
{ "Settings", Choice(NotImplemented("Settings"))},
}
}
// Todo: pagination
type ProblemList struct {}
func (ProblemList) Choices(o Out) []Action {
o("Choose a problem!")
ret := make([]Action, len(Probs))
for i,v := range Probs {
i := i
var str = v.Name
if U.IsSolved(i) {
str += " (SOLVED)"
}
ret[i] = Action{str, Choice(ProblemMenu{i})}
}
return ret
}
type ProblemMenu struct {
pid int
}
func (p ProblemMenu) Choices(o Out) []Action {
pid := p.pid
pr := Probs[pid]
o(`Problem "`, pr.Name, `"`)
ret := []Action{
{"Open problem", func(o Out) (UIScreen, error){
return nil, Edit(pid)}},
{"Run all tests", func(o Out) (UIScreen, error){
if err := Test(o, pid); err != nil {
return nil, err
}
return ProblemSolved{pid}, nil}},
}
// Only let them start over if they haven't solved it already
// (I don't want to deal with removing the solved status)
if U.IsSolved(pid) {
o("\nNote: You've solved this one already!\n")
} else {
ret = append(ret, Action{"Start problem over", Choice(DoubleCheck{
"delete your work and start this problem over from scratch",
func(Out) error {
return WriteOut(pid)
}})})
}
if len(pr.Help) > 0 {
ret = append(ret, Action{"Show help topic",
func(o Out) (UIScreen, error){
return nil, ShowHelp(pid)
}})
}
if len(pr.Hint) > 0 {
if U.IsHintUnlocked(pid) {
ret = append(ret, Action{"Show hint", Choice(ShowHint{pid})})
} else {
ret = append(ret, Action{"Unlock hint", Choice(DoubleCheck{
fmt.Sprintf("spend %v points (out of %v remaining points) to unlock the hint", HintCost, U.Points),
func(Out) error {
return U.UnlockHint(pid)
}})})
}
}
return ret
}
type ProblemSolved struct {
pid int
}
func (p ProblemSolved) Choices(o Out) []Action {
pid := p.pid
pr := Probs[pid]
o(`Problem "`, pr.Name, `" correctly solved!`)
U.MarkSolved(pid)
o(" Current points: ", U.Points)
return nil
}
type ShowHint struct {
pid int
}
func (h ShowHint) Choices(o Out) []Action {
o(Probs[h.pid].Hint)
return nil
}
type Stats struct{}
func (Stats) Choices(o Out) []Action {
o("User stats:")
o("\tPoints: ", U.Points)
o("\nProblem Name (Difficulty): SolvedStatus")
for i,v := range Probs {
var solv string
if U.IsSolved(i) {
solv = "Solved"
} else {
solv = "Unsolved"
}
o("\t", v.Name, " (", v.Difficulty, "): ", solv)
}
return nil
}