diff --git a/README.md b/README.md index 439ca6a..2e4ff58 100644 --- a/README.md +++ b/README.md @@ -53,6 +53,7 @@ options. ## Keys - Pressing `escape` at any point restarts the test. +- `escape`, `enter`, or `space` will close the report page. If `-oneshot` is not set, the next test is loaded. - `C-c` exits the test. - `right` moves to the next test. - `left` moves to the previous test. diff --git a/src/tt.go b/src/tt.go index aba3ada..0fcdecb 100644 --- a/src/tt.go +++ b/src/tt.go @@ -86,7 +86,29 @@ func exit(rc int) { os.Exit(rc) } -func showReport(scr tcell.Screen, cpm, wpm int, accuracy float64, attribution string, mistakes []mistake) { +func showReport(scr tcell.Screen, cpm, wpm int, accuracy float64, attribution string, mistakes []mistake, titleStyle tcell.Style) { + //consumes last key presses to avoid space press + + scr.Clear() + scr.Show() + hold := make(chan tcell.Event) + go func(hold chan tcell.Event) { + discard := make(chan<- tcell.Event) + for { + select { + case discard <- scr.PollEvent(): + { + + } + case <-time.After(300 * time.Millisecond): + close(discard) + close(hold) + return + + } + } + }(hold) + <-hold mistakeStr := "" if attribution != "" { attribution = "\n\nAttribution: " + attribution @@ -104,13 +126,15 @@ func showReport(scr tcell.Screen, cpm, wpm int, accuracy float64, attribution st report := fmt.Sprintf("WPM: %d\nCPM: %d\nAccuracy: %.2f%%%s%s", wpm, cpm, accuracy, mistakeStr, attribution) - scr.Clear() drawStringAtCenter(scr, report, tcell.StyleDefault) + drawStringAsTitle(scr, "Press ESC, SPACE, or ENTER to continue.", titleStyle) scr.HideCursor() scr.Show() for { - if key, ok := scr.PollEvent().(*tcell.EventKey); ok && key.Key() == tcell.KeyEscape { + key, ok := scr.PollEvent().(*tcell.EventKey) + + if ok && (key.Key() == tcell.KeyEscape || key.Key() == tcell.KeyEnter || key.Rune() == 32) { return } else if ok && key.Key() == tcell.KeyCtrlC { exit(1) @@ -383,7 +407,7 @@ func main() { typer.ShowWpm = showWpm if timeout != -1 { - timeout *= 1E9 + timeout *= 1e9 } var tests [][]segment @@ -415,7 +439,7 @@ func main() { idx-- } case TyperComplete: - cpm := int(float64(ncorrect) / (float64(t) / 60E9)) + cpm := int(float64(ncorrect) / (float64(t) / 60e9)) wpm := cpm / 5 accuracy := float64(ncorrect) / float64(nerrs+ncorrect) * 100 @@ -425,7 +449,7 @@ func main() { if len(tests[idx]) == 1 { attribution = tests[idx][0].Attribution } - showReport(scr, cpm, wpm, accuracy, attribution, mistakes) + showReport(scr, cpm, wpm, accuracy, attribution, mistakes, typer.nextWordStyle) } if oneShotMode { exit(0) diff --git a/src/util.go b/src/util.go index 66f005b..9bad02a 100644 --- a/src/util.go +++ b/src/util.go @@ -147,6 +147,16 @@ func drawStringAtCenter(scr tcell.Screen, s string, style tcell.Style) { drawString(scr, x, y, s, -1, style) } +func drawStringAsTitle(scr tcell.Screen, s string, style tcell.Style) { + nc, nr := calcStringDimensions(s) + sw, sh := scr.Size() + + x := (sw - nc) / 2 + y := sh - (sh - nr) + + drawString(scr, x, y, s, -1, style) +} + func calcStringDimensions(s string) (nc, nr int) { if s == "" { return 0, 0