forked from ProfessorChill/CLI-StopWatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__main__.py
99 lines (85 loc) · 3.04 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
87
88
89
90
91
92
93
94
95
96
97
98
99
""" Just a simple CLI Stopwatch that supports multiple watches """
import sys
import threading
import curses
import curses.ascii
from time import sleep
class StopWatch(threading.Thread):
""" The stopwatch class which handles the counting of all watches """
def __init__(self):
super().__init__(daemon=True)
self.seconds = []
self.minutes = []
self.hours = []
self.watches = 0
self.add()
self.paused = set()
def add(self):
""" Append a stopwatch that is set to 0 """
for time in self.seconds, self.minutes, self.hours:
time.append(0)
self.watches += 1
def remove(self, watch_index):
""" Remove a stopwatch at given index """
for time in self.seconds, self.minutes, self.hours:
time.pop(watch_index)
self.watches -= 1
def run(self):
while self.watches:
sleep(1)
for watch in range(0, self.watches):
if watch not in self.paused:
self.seconds[watch] = self.seconds[watch] + 1
if self.seconds[watch] == 60:
self.minutes[watch] = self.minutes[watch] + 1
self.seconds[watch] = 0
if self.minutes[watch] == 60:
self.hours[watch] = self.hours[watch] + 1
self.minutes[watch] = 0
def display(screen, stop_watch, selected):
""" Used to display the watches """
for watch in range(0, stop_watch.watches):
line = '{:02d} Seconds | {:02d} Minutes | {:02d} Hours'\
.format(stop_watch.seconds[watch],
stop_watch.minutes[watch],
stop_watch.hours[watch])
if watch in stop_watch.paused:
line += " - P"
screen.addstr(watch, 0, line, curses.A_REVERSE if watch == selected else 0)
def run(screen):
""" The main run loop """
curses.echo()
stop_watch = StopWatch()
stop_watch.start()
selected = 0
while stop_watch.watches:
sleep(1/20)
screen.clear()
display(screen, stop_watch, selected)
screen.refresh()
char = screen.getch()
if char == curses.ascii.ESC:
stop_watch.watches = 0
elif char == curses.KEY_UP and selected > 0:
selected -= 1
elif char == curses.KEY_DOWN and selected + 1 < stop_watch.watches:
selected += 1
elif char == curses.KEY_LEFT:
stop_watch.remove(selected)
if selected > 0:
selected -= 1
if selected in stop_watch.paused:
stop_watch.paused.remove(selected)
elif char == curses.KEY_RIGHT:
stop_watch.add()
elif char == ord('p'):
stop_watch.paused ^= {selected} # add/remove
sys.exit()
if __name__ == '__main__':
standard_screen = curses.initscr()
curses.noecho()
curses.cbreak()
curses.curs_set(0) # no cursor
standard_screen.keypad(True)
standard_screen.nodelay(True)
curses.wrapper(run)