Skip to content

Commit

Permalink
minimal requirements typetest
Browse files Browse the repository at this point in the history
  • Loading branch information
MasterMedo committed Jan 8, 2021
1 parent 33d7937 commit c62cbb2
Showing 1 changed file with 7 additions and 52 deletions.
59 changes: 7 additions & 52 deletions typetest
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ shortcuts:
"""


def main(duration, input, rows, shuffle, help,
output, mistyped, char_speeds, word_speeds):
def main(duration, input, rows, shuffle, help, output):
"""Reads words from `input` delimited by whitespace characters.
Takes keys from console forming a word every time a whitespace is sent and
compares typed words with test words. Updates the screen calling `draw`
Expand Down Expand Up @@ -64,9 +63,6 @@ def main(duration, input, rows, shuffle, help,
text = ''
colors = [color_normal]*len(words)

mistyped_words = []
char_times = []

with term.raw(), \
term.cbreak(), \
term.fullscreen(), \
Expand All @@ -82,8 +78,7 @@ def main(duration, input, rows, shuffle, help,
draw(term, rows, words, colors, word_i, text, wpm, time_passed)

char = term.inkey(timeout=0.1, esc_delay=0)
char_time = time()
time_passed = char_time - start if start else 0
time_passed = time() - start if start else 0

if not char:
continue
Expand All @@ -97,14 +92,15 @@ def main(duration, input, rows, shuffle, help,
elif char == '\x08' or char == '\x7f': # ctrl-h or backspace
text = text[:-1]

elif char == '\x12': # ctrl-r
correct_chars = total_chars = wpm = 0
elif char == '\x12' or char == '\x13': # ctrl-r or ctrl-s
correct_chars = total_chars = -1
wpm = 0
time_passed = actual_duration = start = 0
word_i = 0
text = ''
colors = [color_normal]*len(words)
mistyped_words = []
char_times = []
if char == '\x13': # ctrl-s
random.shuffle(words)

elif char == '\x15' or char == '\x17': # ctrl-u or ctrl-w
text = ''
Expand All @@ -119,21 +115,18 @@ def main(duration, input, rows, shuffle, help,
colors[word_i] = color_correct
else:
colors[word_i] = color_wrong
mistyped_words.append((word, text))

actual_duration = time_passed
wpm = min(int(correct_chars*12/actual_duration), 999)

text = ''
word_i += 1
char_times.append((char, char_time))

elif not char.isspace():
total_chars += 1
text += char
if word_i + 1 >= len(words) and words[-1] == text:
term.ungetch(' ')
char_times.append((char, char_time))

total_chars -= len(text)
accuracy = 100 * correct_chars // total_chars if total_chars else 0
Expand All @@ -147,35 +140,6 @@ def main(duration, input, rows, shuffle, help,
return

output.write(f'{timestamp},{wpm},{accuracy},{actual_duration},{duration}\n')
for a, b in mistyped_words:
mistyped.write(f'{a},{b},{timestamp}\n')

char_durations = []
for i in range(1, len(char_times)):
_, t0 = char_times[i-1]
c, t1 = char_times[i]
char_durations.append((c, t1-t0))

for char, t in char_durations:
char_speeds.write(f'{char},{t},{12/t},{timestamp}\n')

word = ''
word_i = 0
word_duration = 0
word_durations = []
for char, duration in char_durations:
if char.isspace():
if word == words[word_i]:
word_durations.append((word, word_duration))
word_i += 1
word = ''
word_duration = 0
else:
word += char
word_duration += duration

for word, t in word_durations:
word_speeds.write(f'{word},{t},{len(word)*12/t},{timestamp}\n')


def draw(term, rows, words, colors, word_i, text, wpm, time_passed):
Expand Down Expand Up @@ -239,15 +203,6 @@ def parse_args():
parser.add_argument('-o', '--output', type=FileType('a'),
default=os.path.dirname(__file__) + '/results',
help='file to store results in\n' + default)
parser.add_argument('-m', '--mistyped', type=FileType('a'),
default=os.path.dirname(__file__) + '/mistyped_words',
help='file to store mistyped words in\n' + default)
parser.add_argument('-c', '--char_speeds', type=FileType('a'),
default=os.path.dirname(__file__) + '/char_speeds',
help='file to store character speeds in\n' + default)
parser.add_argument('-w', '--word_speeds', type=FileType('a'),
default=os.path.dirname(__file__) + '/word_speeds',
help='file to store word speeds in\n' + default)
parser.add_argument('-s', '--shuffle', action='store_true',
help='shuffle words ' + default)
parser.add_argument('-r', '--rows', type=int, default=2,
Expand Down

0 comments on commit c62cbb2

Please sign in to comment.