-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
86 lines (64 loc) · 2.16 KB
/
main.py
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
import curses
from curses import wrapper
from getSnippet import getSnippet
from opening import *
from getSnippet import getSnippet
import time
def display(stdscr, target, current, w, wpm=0,):
stdscr.addstr(5, 4, target)
stdscr.addstr(1, w//2-5, f"WPM: {wpm}")
for i, char in enumerate(current):
correct_char = target[i]
color = curses.color_pair(1)
if correct_char != char:
color = curses.color_pair(2)
stdscr.addstr(5, i+4, char, color)
# code per minute counter. choice is programming language
def cpm_test(stdscr, choice, width):
target_text = getSnippet(choice)
while len(target_text) > width:
target_text = getSnippet(choice)
current_text = []
wpm = 0
start_time = time.time()
stdscr.nodelay(True)
while True:
time_end = max(time.time() - start_time, 1)
wpm = round((len(current_text) / (time_end/60))/5)
stdscr.erase()
display(stdscr, target_text, current_text, width, wpm)
stdscr.refresh()
joined_text = "".join(current_text)
if joined_text == (target_text):
stdscr.nodelay(False)
break
try:
key = stdscr.getkey()
except:
continue
if len(key) == 1 and ord(key) == 27:
break
# Handling deleting characters
if key in ("KEY_BACKSPACE"):
if len(current_text) > 0:
current_text.pop()
elif len(current_text) < len(target_text):
current_text.append(key)
# main method
def main(stdscr):
height, width = stdscr.getmaxyx()
curses.init_pair(1, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_WHITE, curses.COLOR_BLACK)
name = getName(stdscr, width, height)
lang = choose_language(stdscr)
opening(stdscr, name)
while True:
cpm_test(stdscr, lang, width)
stdscr.erase()
stdscr.addstr(
5, 5, "Finished! Press ESC key to Exit.\nPress any key to continue...")
key1 = stdscr.getkey()
if ord(key1) == 27:
break
wrapper(main)