-
Notifications
You must be signed in to change notification settings - Fork 0
/
typingTestBuffer.go
462 lines (405 loc) · 9.74 KB
/
typingTestBuffer.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
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
package buffers
import (
"fmt"
"time"
"github.com/Pallinder/go-randomdata"
"github.com/gdamore/tcell/v2"
"github.com/soleimanim/gotype/db"
"github.com/soleimanim/gotype/logger"
"github.com/soleimanim/gotype/screen"
"github.com/soleimanim/gotype/styles"
)
const TYPING_BUFFER_IDENTIFIER = 1
type TestMode int
const (
TestMode25Words TestMode = 0
TestMode50Words TestMode = 1
TestMode75Words TestMode = 2
TestMode100Words TestMode = 3
TestMode15Seconds TestMode = 4
TestMode30Seconds TestMode = 5
TestMode45Seconds TestMode = 6
TestMode60Seconds TestMode = 7
)
type TestBufferMenuItem struct {
Label string
Key tcell.Key
Action func(*TypingTestBuffer)
}
var menuItems []TestBufferMenuItem = []TestBufferMenuItem{
{
Label: "New Test:",
Key: tcell.KeyF1,
Action: func(b *TypingTestBuffer) {
b.window.RemoveBuffer(TYPING_BUFFER_IDENTIFIER)
buff := NewTypingTestBuffer(b.Position, b.Size, b.Mode, b.Repository)
b.window.AppendBuffer(&buff)
},
},
{
Label: "Repeat:",
Key: tcell.KeyF2,
Action: func(b *TypingTestBuffer) {
b.reset()
},
},
}
type TypingEvent struct {
Time time.Time
Key *tcell.EventKey
}
type TypingTestBuffer struct {
Size screen.BufferSize
Position screen.BufferPosition
Mode TestMode
Repository db.Repository[db.TypingTestModel]
y int
screen tcell.Screen
window *screen.Window
input string
testText string
recordedEvents []TypingEvent
isReplaying bool
speed float32
accuracy float32
mistakesCount int
isFinished bool
}
func GetTypingTestBufferPositionAndSize(s tcell.Screen) (screen.BufferPosition, screen.BufferSize) {
w, h := s.Size()
return screen.BufferPosition{
X: w / 4,
Y: 0,
}, screen.BufferSize{
Width: w * 3 / 4,
Height: h / 2,
}
}
func NewTypingTestBuffer(position screen.BufferPosition, size screen.BufferSize, mode TestMode, repository db.Repository[db.TypingTestModel]) TypingTestBuffer {
wordsCount := 25
switch mode {
case TestMode25Words:
wordsCount = 25
case TestMode50Words:
wordsCount = 50
case TestMode75Words:
wordsCount = 75
case TestMode100Words:
wordsCount = 100
}
testText := randomdata.Noun()
for range wordsCount - 1 {
testText += " " + randomdata.Noun()
}
b := TypingTestBuffer{
Size: size,
Position: position,
Mode: mode,
Repository: repository,
y: 5,
screen: nil,
input: "",
testText: testText,
}
return b
}
func (b *TypingTestBuffer) Draw() {
screen.DrawBox(b.Position, b.Size, b.screen, screen.BoxTitle{
Title: "Typing Test",
Alignment: screen.TextAlignmentLeft,
})
b.y = b.Position.Y + 1
// Show typed words count or remaining time based on the mode
// TODO: Add remaining time when time modes implemented
typedWords := b.countWords(b.input)
totalWords := b.countWords(b.testText)
startX := b.Position.X + 2
if len(b.recordedEvents) > 0 {
screen.DrawText(b.screen, fmt.Sprintf("%d / %d", typedWords, totalWords), &startX, &b.y, styles.Style(tcell.ColorOrange, tcell.ColorReset))
}
b.y += 1
startX = b.Position.X + 2
for i, r := range b.testText {
style := styles.TextPlaceHolderStyle
if len(b.input) > i {
if b.input[i] == b.testText[i] {
style = styles.TextPrimaryStyle
} else {
style = styles.TextErrorStyle
}
}
b.screen.SetContent(startX, b.y, r, nil, style)
if i == len(b.input) {
b.screen.ShowCursor(startX, b.y)
}
startX += 1
if r == ' ' && len(b.testText) > i+2 {
remainingText := b.testText[i+1:]
for j, rr := range remainingText {
if rr == ' ' {
if startX+j+2 > b.Position.X+b.Size.Width {
b.y += 1
startX = b.Position.X + 2
}
break
}
}
}
}
b.drawMenu()
if b.isFinished || b.isReplaying {
b.screen.HideCursor()
}
if b.isReplaying {
y := b.y
y += 2
x := 0
screen.DrawText(b.screen, "Replaying...", &x, &y, styles.ForegroundStyle(tcell.ColorLightGray))
screen.DrawText(b.screen, " Press ", &x, &y, styles.ForegroundStyle(tcell.ColorLightGray))
screen.DrawText(b.screen, " X ", &x, &y, styles.Style(tcell.ColorRed, tcell.ColorLightPink))
screen.DrawText(b.screen, " to cancel replaying. ", &x, &y, styles.ForegroundStyle(tcell.ColorLightGray))
}
}
func (b *TypingTestBuffer) GetID() int {
return TYPING_BUFFER_IDENTIFIER
}
func (b *TypingTestBuffer) SetScreen(screen tcell.Screen) {
b.screen = screen
}
func (b *TypingTestBuffer) SetWindow(w *screen.Window) {
b.window = w
}
func (b *TypingTestBuffer) HandleKeyEvent(ev *tcell.EventKey) {
for _, m := range menuItems {
if m.Key == ev.Key() {
m.Action(b)
return
}
}
if b.isReplaying {
if ev.Rune() == 'x' || ev.Rune() == 'X' {
b.isReplaying = false
b.showTestResult()
}
return
}
if b.isFinished {
return
}
b.recordEvent(ev)
b.applyKeyEvent(ev)
b.updateTestInfo()
b.updateStatusLine()
if len(b.input) >= len(b.testText) {
b.isFinished = true
b.onFinished()
b.showTestResult()
return
}
}
func (b *TypingTestBuffer) Replay() {
b.isReplaying = true
b.input = ""
b.y = 5
b.window.Draw()
go func() {
for i, ev := range b.recordedEvents {
if !b.isReplaying {
break
}
if i != 0 {
prevTime := b.recordedEvents[i-1].Time
currentTime := b.recordedEvents[i].Time
wait := currentTime.Sub(prevTime)
time.Sleep(wait)
}
b.applyKeyEvent(ev.Key)
b.window.Draw()
}
b.isReplaying = false
b.showTestResult()
}()
}
func (b *TypingTestBuffer) applyKeyEvent(ev *tcell.EventKey) {
if ev.Key() == tcell.KeyBackspace || ev.Key() == tcell.KeyBackspace2 {
if len(b.input) > 0 {
b.input = b.input[:len(b.input)-1]
}
return
}
if r := ev.Rune(); r != 0 {
// character
b.input = b.input + string(r)
// check mistakes
index := len(b.input) - 1
if !b.isReplaying && b.input[index] != b.testText[index] {
b.mistakesCount += 1
}
}
}
func (b *TypingTestBuffer) recordEvent(ev *tcell.EventKey) {
b.recordedEvents = append(b.recordedEvents, TypingEvent{
Time: time.Now(),
Key: ev,
})
}
func (b TypingTestBuffer) countWords(s string) int {
if len(s) == 0 {
return 0
}
words := 1
for _, r := range s {
if r == ' ' {
words += 1
}
}
return words
}
func (b TypingTestBuffer) updateStatusLine() {
buffer := b.window.GetBufferByID(STATUS_LINE_BUFFER_ID)
statusBuffer, ok := buffer.(*StatusLineBuffer)
if !ok {
return
}
statusBuffer.Speed = b.speed
statusBuffer.Accuracy = b.accuracy
}
func (b *TypingTestBuffer) updateTestInfo() {
// calculate speed
if len(b.recordedEvents) > 0 {
words := float32(b.countWords(b.input))
// all := (b.countWords(b.testText))
startTime := b.recordedEvents[0].Time
duration := time.Since(startTime).Seconds()
b.speed = 60 * words / float32(duration)
if b.mistakesCount > len(b.input) {
b.accuracy = 0
} else {
b.accuracy = float32(len(b.testText)-b.mistakesCount) / float32(len(b.testText)) * 100.0
}
}
}
func (b *TypingTestBuffer) showTestResult() {
dialog := DialogBuffer{
Title: "Typing Test Result",
Description: []StyledText{
{
Text: "Speed: ",
Style: styles.BorderStyle,
},
{
Text: fmt.Sprintf(" %.2f ", b.speed),
Style: styles.TextHighlightStyle1,
},
{
Text: "\tAccuracy: ",
Style: styles.BorderStyle,
},
{
Text: fmt.Sprintf(" %.2f ", b.accuracy),
Style: styles.TextHighlightStyle2,
},
},
Buttons: []DialogButton{
{
Label: " New Test ⏎ ",
Key: tcell.KeyEnter,
Style: styles.Style(tcell.ColorSkyblue, tcell.ColorWhite),
Action: func() bool {
b.window.RemoveBuffer(TYPING_BUFFER_IDENTIFIER)
newBuffer := NewTypingTestBuffer(b.Position, b.Size, b.Mode, b.Repository)
b.window.AppendBuffer(&newBuffer)
return true
},
},
{
Label: " Replay ^R ",
Key: tcell.KeyCtrlR,
Style: styles.Style(tcell.ColorWhite, tcell.ColorSkyblue),
Action: func() bool {
b.Replay()
return true
},
},
},
TitleStyle: styles.ForegroundStyle(tcell.ColorBlack).Bold(true),
}
b.window.AppendBuffer(&dialog)
}
func (b *TypingTestBuffer) onFinished() {
logger.Println("TypingTestBuffer OnFinished called, saving result to database")
model := db.TypingTestModel{
Speed: b.speed,
Accuracy: b.accuracy,
WordsCount: b.getWordsCount(b.Mode),
MistakesCount: uint(b.mistakesCount),
}
err := b.Repository.Create(&model)
if err != nil {
logger.Println("Error saving typing test result to database", err)
return
}
rb := b.window.GetBufferByID(RECENT_TESTS_BUFFER_ID)
if rb != nil {
buffer, ok := rb.(*RecentTestsBuffer)
if ok {
buffer.Update()
}
}
sb := b.window.GetBufferByID(STATS_BUFFER_ID)
if sb != nil {
buffer, ok := sb.(*StatsBuffer)
if ok {
buffer.Update()
}
}
}
func (b TypingTestBuffer) getWordsCount(mode TestMode) uint {
switch mode {
case TestMode25Words:
return 25
case TestMode50Words:
return 50
case TestMode75Words:
return 75
case TestMode100Words:
return 100
}
return 25
}
func (b TypingTestBuffer) drawMenu() {
y := b.Size.Height - 3
x := b.Position.X + 1
w := b.Size.Width - 2
lineX := x
style := styles.BorderStyle.Foreground(tcell.ColorLightGray)
for range w {
b.screen.SetContent(lineX, y, '─', nil, style)
lineX++
}
spacing := 5
startX := x
y += 1
menuCount := len(menuItems)
for i, m := range menuItems {
screen.DrawText(b.screen, m.Label, &startX, &y, styles.ForegroundStyle(tcell.ColorRoyalBlue))
startX++
screen.DrawText(b.screen, tcell.KeyNames[m.Key], &startX, &y, styles.ForegroundStyle(tcell.ColorPurple))
if i != menuCount-1 {
b.screen.SetContent(startX+spacing/2, y, '|', nil, styles.BorderStyle)
}
startX += spacing
}
}
func (b *TypingTestBuffer) reset() {
b.input = ""
b.mistakesCount = 0
b.recordedEvents = []TypingEvent{}
b.isReplaying = false
b.speed = 0
b.accuracy = 0
b.isFinished = false
b.speed = 0
b.accuracy = 0
b.isFinished = false
}