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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
|
package ctfc
import (
"strings"
"gitlab.com/Syndamia/ctfc/go-src/csi"
"gitlab.com/Syndamia/ctfc/go-src/ui"
"gitlab.com/Syndamia/ctfc/go-src/utils"
)
const showHelp = "ShowHelp"
type window func(...string)
func StartupWindow(...string) {
csi.ClearScreen()
ui.NormalBox(true, loginNavTitle, registerNavTitle)
input := ui.InputField(startupWindowSpec)
nextWindow := handleInputActions(input, false,
inputAction{"L", loginWindow, nil},
inputAction{"R", registerWindow, nil},
)
if nextWindow == nil {
defer showError(invalidCommand, StartupWindow)
} else {
defer nextWindow()
}
}
func loginWindow(values ...string) {
// Takes form user input
var inputs []string
formWindow(loginNavTitle, StartupWindow,
[]formInput{
{usernameInName, inputBackSpec, nil},
{passwordInName, "", nil},
},
&inputs,
)
if !usernameExists(inputs[0]) {
defer showError(valueDoesNotExist(usernameInName), loginWindow)
}
// If login is successful, go to chats window, else show error
if logInUser(inputs[0], inputs[1]) {
defer chatsWindow()
} else {
defer showError(invalidCredentials, loginWindow, inputs[0])
}
}
func registerWindow(values ...string) {
var inputs []string
formWindow(registerNavTitle, StartupWindow,
[]formInput{
{usernameInName, usernameSpec, stringValidUsername},
{passwordInName, passwordSpec, stringValidPassword},
{nameInName, nameSpec, stringValidName},
},
&inputs,
)
if usernameExists(inputs[0]) {
defer showError(valueAlreadyTaken(usernameInName), registerWindow, values...)
return
}
createUser(inputs)
logInUser(inputs[0], inputs[1])
defer chatsWindow()
}
func chatsWindow(values ...string) {
csi.ClearScreen()
initPaginatedValues(0, &values)
ui.NormalBox(true, directMessagesNavTitle, accountNavTitle, logoutNavTitle)
allChats := getAllChats()
ui.EmptyLines(pageSize + 3) // empty line + message lines + page number line + empty line
customLinesBelow := utils.If(values[1] == showHelp).Int(3, 2)
lastLine := -1
go routinePaginatedSubwindow(&allChats,
func(s *[]string) { *s = getAllChats() },
&lastLine,
len(values[0]),
customLinesBelow,
true,
)
if values[1] == showHelp {
ui.TextField(chatsWindowHelpMsg)
}
input := ui.InputField(chatsSpec)
nextWindow := handleInputActions(input, true,
inputAction{"C", createChatWindow, nil},
inputAction{"H", chatsWindow, []string{values[0], showHelp}},
)
if nextWindow == nil {
defer handleComplexInputActions(input,
allChats,
" : ",
&values[0],
chatsWindow,
values,
chatWindow,
chatNameExists,
)
} else {
defer nextWindow()
}
lastLine = -2
}
func createChatWindow(values ...string) {
var inputs []string
formWindow(createChatNavTitle, chatsWindow,
[]formInput{
{chatNameInName, chatNameSpec, stringValidChatName},
{chatDescInName, chatDescSpec, stringValidChatDesc},
},
&inputs,
)
if chatNameExists(inputs[0]) {
defer showError(valueAlreadyTaken(chatNameInName), createChatWindow, values...)
return
}
createChat(inputs[0], inputs[1], loggedInUser.Username)
defer chatWindow(inputs[0])
}
func chatWindow(values ...string) {
csi.ClearScreen()
// We determine page number by length of a string
// This method should be faster than having to cast to int all the time
initPaginatedValues(1, &values)
currChat := getChat(values[0])
ui.NormalBox(true, chatNavTitle, directMessagesNavTitle, accountNavTitle, logoutNavTitle)
ui.TextFields(
currChat.Name+" : "+currChat.Description,
"Brought to you by "+currChat.Owner.Name+" ("+currChat.Owner.Username+")",
)
ui.EmptyLines(pageSize + 3) // empty line + messages lines + page number line + empty line
customLinesBelow := utils.If(values[2] == showHelp).Int(3, 2)
lastLine := -1
go routinePaginatedSubwindow(&currChat.Messages,
func(s *[]string) { *s = getChat(values[0]).Messages },
&lastLine,
len(values[1]),
customLinesBelow,
false,
)
if values[2] == showHelp {
ui.TextField(chatWindowHelpMsg)
values[2] = ""
}
input := ui.InputField(chatWindowSpec)
nextWindow := handleInputActions(input, true,
inputAction{"H", chatWindow, []string{values[0], values[1], showHelp}},
)
if nextWindow == nil {
switch input {
case ">":
// If possible, increment to the next page (adds a dot to the end of the string)
if len(values[1]) < totalPages(len(currChat.Messages)) {
values[1] += "."
}
case "<":
// If possible, decrement to the previous page (removes a dot from the string)
if len(values[1]) > 1 {
utils.StrShortenRight(&values[1], 1)
}
default: // Send a message
values[1] = "." // Make sure to be at the first page, when sending a message
// Since "C" is a command, to just enter the letter C you'll need to input "\C"
currChat.addMessage(strings.TrimPrefix(input, "\\"), loggedInUser.Username)
}
defer chatWindow(values...)
} else {
defer nextWindow()
}
lastLine = -2 // Practically stops execution of the paginated subwindow routine
}
func directMessagesWindow(values ...string) {
csi.ClearScreen()
initPaginatedValues(0, &values)
ui.NormalBox(true, chatNavTitle, accountNavTitle, logoutNavTitle)
allDirectMessageChats := getAllDirectMessageChats()
ui.EmptyLines(pageSize + 3) // empty line + message lines + page number line + empty line
customLinesBelow := utils.If(values[1] == showHelp).Int(3, 2)
lastLine := -1
go routinePaginatedSubwindow(&allDirectMessageChats,
func(s *[]string) { *s = (getAllDirectMessageChats()) },
&lastLine,
len(values[0]),
customLinesBelow,
true,
)
if values[1] == showHelp {
ui.TextField(directMessagesWindowHelpMsg)
}
input := ui.InputField(chatsSpec)
nextWindow := handleInputActions(input, true,
inputAction{"H", directMessagesWindow, []string{values[0], showHelp}},
)
if nextWindow == nil {
values[0] = values[0][:len(values[0])-1]
defer handleComplexInputActions(input,
allDirectMessageChats,
" (",
&values[0],
directMessagesWindow,
values,
directMessageWindow,
usernameExists,
)
} else {
defer nextWindow()
}
lastLine = -2
}
func directMessageWindow(values ...string) {
csi.ClearScreen()
// We determine page number by length of a string
// This method should be faster than having to cast to int all the time
initPaginatedValues(1, &values)
currDirectMessagesChat := getDirectMessageChat(values[0])
ui.NormalBox(true, chatNavTitle, directMessagesNavTitle, accountNavTitle, logoutNavTitle)
ui.TextField(currDirectMessagesChat.Author2.Name + " (" + currDirectMessagesChat.Author2.Username + ")")
ui.EmptyLines(pageSize + 3) // empty line + messages lines + page number line + empty line
customLinesBelow := utils.If(values[2] == showHelp).Int(3, 2)
lastLine := -1
go routinePaginatedSubwindow(&currDirectMessagesChat.Messages,
func(s *[]string) { *s = getDirectMessageChat(values[0]).Messages },
&lastLine,
len(values[1]),
customLinesBelow,
false,
)
if values[2] == showHelp {
ui.TextField(directMessageWindowHelpMsg)
values[2] = ""
}
input := ui.InputField(chatWindowSpec)
nextWindow := handleInputActions(input, true,
inputAction{"H", directMessageWindow, []string{values[0], values[1], showHelp}},
)
if nextWindow == nil {
switch input {
case ">":
// If possible, increment to the next page (adds a dot to the end of the string)
if len(values[1]) < totalPages(len(currDirectMessagesChat.Messages)) {
values[1] += "."
}
case "<":
// If possible, decrement to the previous page (removes a dot from the string)
if len(values[1]) > 1 {
utils.StrShortenRight(&values[1], 1)
}
default: // Send a message
values[1] = "." // Make sure to be at the first page, when sending a message
// Since "C" is a command, to just enter the letter C you'll need to input "\C"
currDirectMessagesChat.addMessage(strings.TrimPrefix(input, "\\"), loggedInUser.Username)
}
defer directMessageWindow(values...)
} else {
defer nextWindow()
}
lastLine = -2 // Practically stops execution of the paginated subwindow routine
}
func accountWindow(values ...string) {
csi.ClearScreen()
ui.NormalBox(true, chatNavTitle, directMessagesNavTitle, logoutNavTitle)
ui.TextField(usernameInName + " : " + loggedInUser.Username)
ui.NumberedFields(
passwordInName+" : "+hiddenValue,
nameInName+" : "+loggedInUser.Name,
)
ui.EmptyLine()
if len(values) > 0 {
if values[0] == showHelp {
ui.TextField(accountWindowHelpMsg)
}
}
input := ui.InputField(accountWindowSpec)
nextWindow := handleInputActions(input, true,
inputAction{"H", accountWindow, []string{showHelp}},
)
if nextWindow == nil {
defer validatedMultiForm(input, accountWindow,
multiFormProp{int(userPasswordProp), editPasswordNavTitle,
formInput{newPasswordInName, passwordSpec, stringValidPassword},
func(values []string) bool { return loggedInUser.UpdatePassword(values[0], values[1]) },
nil,
},
multiFormProp{int(userNameProp), editNameNavTitle,
formInput{newNameInName, nameSpec, stringValidName},
func(values []string) bool { return loggedInUser.UpdateName(values[0], values[1]) },
func(values []string) { loggedInUser.Name = values[1] },
},
)
} else {
defer nextWindow()
}
}
func logoutWindow(...string) {
csi.ClearScreen()
ui.NormalBox(true, "Are you sure you want to Log out of "+loggedInUser.Username+"?")
ui.EmptyLine()
input := ui.InputField(logoutWindowSpec)
if strings.ToLower(input) == "y" {
logoutUser()
defer StartupWindow()
} else {
defer chatsWindow()
}
}
|