-
Notifications
You must be signed in to change notification settings - Fork 0
/
typeracer
executable file
·82 lines (77 loc) · 2.89 KB
/
typeracer
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
#!/usr/bin/env python3
import curses, time, wikiquote, random
def get_quote():
'''
Function to get quote from wikiquote api
uses the wikiquote module from pypi
Get's a random title name,
then detches quotes
returns a random quote from the collection
'''
title_name = random.choice(wikiquote.random_titles())
quotes_ = wikiquote.quotes(title_name)
while len(quotes_) == 0:
title_name = random.choice(wikiquote.random_titles())
quotes_ = wikiquote.quotes(title_name)
quote = random.choice(quotes_)
return quote
def main():
print('Fetching quote...')
question = get_quote()
stdscr = curses.initscr()
curses.noecho() #Do not print whatever is read
curses.cbreak() #If Ctrl + C is pressed, exit the program
stdscr.keypad(True)
curses.start_color() #Allow for color in text
UNTOUCH, CORRECT, INCORRECT = 1, 2, 3 #Constants
curses.init_pair(UNTOUCH, curses.COLOR_YELLOW, curses.COLOR_BLACK) #Color Pair for Untouched string
curses.init_pair(CORRECT, curses.COLOR_GREEN, curses.COLOR_BLACK) #Color Pair for Correct string
curses.init_pair(INCORRECT, curses.COLOR_RED, curses.COLOR_BLACK) #Color Pair for Incorrect string
number_words = len(list(question.split())) #Count number of words in quote
correct_index, current_index = -1, -1
no_errors = True
#Turn certain attributes on
stdscr.attron(curses.color_pair(UNTOUCH) | curses.A_STANDOUT)
stdscr.addstr(question)
stdscr.attroff(curses.color_pair(UNTOUCH))
#Wait until first key is pressed
#As soon as it is pressed, read it and buffer it back
#Then start the timer
curses.ungetch(stdscr.getch())
start = time.time()
while True:
#Main logic loop
c = stdscr.getch()
stdscr.move(0, 0)
if c == curses.KEY_BACKSPACE:
current_index = max(current_index - 1, -1)
correct_index = min(current_index, correct_index)
else:
current_index += 1
current_index = min(current_index, len(question) - 1)
if question[current_index] == chr(c) and no_errors:
correct_index += 1
elif question[current_index] != chr(c):
no_errors = False
stdscr.attron(curses.color_pair(CORRECT) | curses.A_UNDERLINE)
stdscr.addstr(question[ : correct_index + 1])
stdscr.attroff(curses.color_pair(CORRECT))
stdscr.attron(curses.color_pair(INCORRECT))
stdscr.addstr(question[max(correct_index + 1, 0) : current_index + 1])
stdscr.attroff(curses.color_pair(INCORRECT) | curses.A_UNDERLINE)
stdscr.attron(curses.color_pair(UNTOUCH))
stdscr.addstr(question[max(current_index + 1, 0) : ])
stdscr.attroff(curses.color_pair(UNTOUCH))
if correct_index == len(question) - 1:
break
if correct_index == current_index:
no_errors = True
#Self Explanatory
end = time.time()
stdscr.addstr("\nCongratulations! Execution Time is " + str(round((end - start), 3))\
+ " and speed is " + str(round(number_words * 60/(end - start))) + " WPM...")
stdscr.addstr("\nPress any key to continue...")
stdscr.getch()
curses.endwin()
if __name__ == '__main__':
main()