Repository navigation
Expand file tree
/
Copy pathtimer.py
More file actions
149 lines (114 loc) · 4.59 KB
/
Copy pathtimer.py
File metadata and controls
149 lines (114 loc) · 4.59 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import curses
import threading
import time
from scramble import scramble
from pynput import keyboard
stop_thread = False
stop_timer = False
scramble_alg = False
timer_time = 0
def turn_timer_green(time, stdscr, timer, decimals, height, width):
global stop_thread
for i in range(0, 5):
if stop_thread: break
time.sleep(0.1)
stdscr.clear()
stdscr.addstr(int(height / 2) - 1, int((width - len(format_timer(timer_time, decimals))) / 2), format_timer(timer_time, decimals), curses.color_pair(4))
stdscr.refresh()
def listen_for_space(key):
if key == keyboard.Key.space:
return False
if key == keyboard.Key.f2 or key == keyboard.Key.f3 or key == keyboard.Key.f4:
global stop_timer
stop_timer = True
return False
def format_timer(timer_time, decimals, padding=True):
if timer_time / 60 > 1: # Show minutes
timer_value = f"{int(timer_time / 60)}:{'' if timer_time % 60 > 10 else '0'}{(timer_time % 60):.{decimals}f}"
return f" {timer_value} " if padding else timer_value
else: # Don't show minutes
timer_value = f"{timer_time:.{decimals}f}"
return f" {timer_value} " if padding else timer_value
def timer(stdscr, args):
cover = args["cover"]
decimals = args["decimals"]
database = args["database"]
height, width = stdscr.getmaxyx()
global scramble_alg
global stop_timer
global timer_time
stop_timer = False
while True:
timer = 0
decimals = 3
stdscr.clear()
stdscr.addstr(int(height / 2) - 1, int((width - len(format_timer(timer_time, decimals))) / 2), format_timer(timer_time, decimals), curses.color_pair(1))
if not scramble_alg:
scramble_alg = scramble(20)
stdscr.addstr(int(height / 2) + 1, int((width - len(scramble_alg)) / 2), scramble_alg)
solves = [float(solve[2]) for solve in args["database"].read() if not solve[5]]
if len(solves) >= 5:
stdscr.addstr(height - 1, 0, f"Ao5: {format_timer(sum(solves[-5:]) / 5, decimals, padding=False)}")
if len(solves) > 0:
session_best = min(solves)
stdscr.addstr(
height - 1,
width - 1 - len(f"Session best: {format_timer(session_best, decimals, padding=False)}"),
f"Session best: {format_timer(session_best, decimals, padding=False)}")
stdscr.refresh()
with keyboard.Listener(
on_press=listen_for_space) as listener:
listener.join()
if stop_timer: break
stdscr.clear()
stdscr.addstr(int(height / 2) - 1, int((width - len(format_timer(timer_time, decimals))) / 2), format_timer(timer_time, decimals), curses.color_pair(3))
stdscr.refresh()
thread = threading.Thread(target=turn_timer_green, args=(time, stdscr, timer_time, decimals, height, width))
thread.start()
with keyboard.Listener(
on_release=listen_for_space) as listener:
listener.join()
if stop_timer: break
if thread.is_alive():
stdscr.addstr(int(height / 2) - 1, int((width - len(format_timer(timer_time, decimals))) / 2), format_timer(timer_time, decimals), curses.color_pair(1))
stdscr.refresh()
global stop_thread
stop_thread = True
thread.join()
stop_thread = False
continue
start = time.time()
listener = keyboard.Listener(
on_press=listen_for_space)
listener.start()
if stop_timer: break
if cover:
stdscr.clear()
stdscr.addstr(int(height / 2) - 1, int((width - len("Solve")) / 2), "Solve")
stdscr.refresh()
else:
stdscr.clear()
while listener.is_alive():
timer_time = time.time() - start
stdscr.addstr(
int(height / 2) - 1,
int((width - len(format_timer(timer_time, decimals))) / 2),
format_timer(timer_time, decimals),
curses.color_pair(1))
stdscr.refresh()
listener.join()
timer_time = time.time() - start
database.write(f"{timer_time:.3f}", scramble_alg)
scramble_alg = False
def main():
from parser import parse_args
args = parse_args()
if args.scramble:
print(scramble(20))
else:
try:
curses.wrapper(timer, args.cover, args.decimals)
except KeyboardInterrupt:
curses.endwin()
print("Ctrl-c recieved, exiting...")
if __name__ == "__main__": main()