typetest

Typing speed benchmarker
git clone git@sinitax.com:sinitax/typetest
Log | Files | Refs | LICENSE | sfeed.txt

commit cd78494b61532c846e7ae2f4d9142330160a6560
parent bae29417270798f60d0348c22cd9fb21b5f3d8f8
Author: Louis Burda <quent.burda@gmail.com>
Date:   Tue, 22 Jun 2021 00:27:30 +0200

Improved gui behavior while typing

Diffstat:
Mmain.c | 32+++++++++++++++++++++-----------
1 file changed, 21 insertions(+), 11 deletions(-)

diff --git a/main.c b/main.c @@ -89,7 +89,7 @@ int type(const char *text, const char *preview) { char input[1024]; - int c, right, wrong, tlen; + int i, c, right, wrong, tlen; struct timespec start = { 0 }, end; float cps; @@ -97,15 +97,12 @@ type(const char *text, const char *preview) c = right = wrong = 0; do { printf("\r\x1b[2K"); /* goto beginning of row and clear */ - printf("\x1b[1A\x1b[2K"); /* up a row and clear */ - - if (c && !start.tv_sec) - clock_gettime(CLOCK_REALTIME, &start); + printf("\n\x1b[2K\x1b[1A"); /* down a row and clear */ switch (c) { case C_EOF: case C_EOT: - return 1; + goto quit; case C_DEL: if (wrong) wrong--; else if (right) right--; @@ -114,6 +111,8 @@ type(const char *text, const char *preview) case u'\0': break; default: + if (!start.tv_sec) + clock_gettime(CLOCK_REALTIME, &start); if (right + wrong == tlen) break; if (right + wrong == sizeof(input)) break; if (c == text[right + wrong] && !wrong) right++; @@ -124,7 +123,8 @@ type(const char *text, const char *preview) print_style(style_right); printf("%.*s", right, text); print_style(style_wrong); - printf("%.*s", wrong, text + right); + for (i = 0; i < wrong; i++) + putchar(text[right + i] == ' ' ? '_' : text[right + i]); print_style(style_reset); printf("%.*s", tlen - right - wrong, text + right + wrong); @@ -132,15 +132,22 @@ type(const char *text, const char *preview) printf("\n\r%s", preview); print_style(style_reset); + printf("\x1b[1A\r"); + if (right + wrong) + printf("\x1b[%iC", right + wrong); + if (!wrong && right + wrong == tlen) break; } while ((c = getchar()) > 0); +quit: + if (!start.tv_sec || typed + right + wrong < 20) return 1; + clock_gettime(CLOCK_REALTIME, &end); time_sub(&end, &start); - cps = tlen / (end.tv_sec + end.tv_nsec / 1e9f); + cps = (right + wrong) / (end.tv_sec + end.tv_nsec / 1e9f); if (!typed) gcps = cps; - else gcps = LINTERP(gcps, cps, typed * 1.f / (typed + tlen)); + else gcps = LINTERP(gcps, cps, typed * 1.f / (typed + right + wrong)); return 0; } @@ -194,14 +201,17 @@ main(int argc, const char **argv) if (getl(f, next, sizeof(next))) die("Empty input file\n"); - printf("\n"); for (eof = 0; !eof; ) { memcpy(cur, next, sizeof(cur)); eof |= getl(f, next, sizeof(next)); if (type(cur, eof ? "" : next)) break; } - printf("CPM: %0.2f\n\r", gcps * 60); + printf("\r\x1b[2K"); + if (gcps) { + printf("CPM: %0.2f\n\r", gcps * 60); + printf("WPM: %0.2f\n\r", gcps * 12); + } fclose(f); }