2023-04-14 14:27:45 +02:00
|
|
|
#!/bin/env python3
|
2023-04-18 20:30:49 +02:00
|
|
|
import beepy # pip install beepy
|
|
|
|
from multiprocessing import Process
|
|
|
|
import argparse
|
2023-04-16 17:30:32 +02:00
|
|
|
import time
|
2023-04-14 14:27:45 +02:00
|
|
|
import datetime
|
|
|
|
import sys
|
2023-04-16 17:30:32 +02:00
|
|
|
import time
|
2023-04-14 14:27:45 +02:00
|
|
|
|
|
|
|
|
|
|
|
class stopwatch:
|
2023-04-18 20:30:49 +02:00
|
|
|
|
|
|
|
beep_at: int
|
|
|
|
beep_at_time: datetime.datetime
|
|
|
|
has_beeped: bool = False
|
|
|
|
start_time: datetime.datetime
|
|
|
|
enable_sound: bool = False
|
|
|
|
|
|
|
|
def __init__(self, beep_at, enable_sound) -> None:
|
2023-04-20 10:22:54 +02:00
|
|
|
print('='*120)
|
2023-04-14 14:27:45 +02:00
|
|
|
self.start_time = datetime.datetime.now().replace(microsecond=0)
|
2023-04-18 20:30:49 +02:00
|
|
|
if enable_sound:
|
|
|
|
print("Warning:\tonce the beep time is reached, a sound will play.")
|
|
|
|
self.enable_sound = enable_sound
|
|
|
|
if not beep_at <= 0:
|
|
|
|
self.beep_at = beep_at
|
|
|
|
self.beep_at_time = self.start_time + datetime.timedelta(minutes=beep_at)
|
|
|
|
print("Start time:\t%s\nWill beep at:\t%s\nBeeping Time:\t%sm" % (self.start_time, self.beep_at_time, beep_at))
|
|
|
|
else:
|
|
|
|
self.beep_at = 0
|
2023-04-20 10:22:54 +02:00
|
|
|
self.beep_at_time = datetime.datetime.now()
|
2023-04-18 20:30:49 +02:00
|
|
|
print("Start time:\t%s" % self.start_time)
|
|
|
|
print('='*120)
|
|
|
|
|
2023-04-14 14:27:45 +02:00
|
|
|
|
|
|
|
def display(self) -> None:
|
2023-04-18 20:30:49 +02:00
|
|
|
text2 = ""
|
2023-04-20 10:22:54 +02:00
|
|
|
text3 = ""
|
2023-04-14 14:27:45 +02:00
|
|
|
while True:
|
|
|
|
now = datetime.datetime.now().replace(microsecond=0)
|
|
|
|
elapsed = (now - self.start_time)
|
|
|
|
text0 = ("\rcurrent:\t%s" % now)
|
2023-04-20 10:22:54 +02:00
|
|
|
text1 = ("elapsed: %s" % elapsed)
|
|
|
|
if self.beep_at > 0 and not self.has_beeped:
|
|
|
|
remaining = self.beep_at_time - now
|
|
|
|
text2 = ("remaining: %s" % remaining)
|
2023-04-18 20:30:49 +02:00
|
|
|
if elapsed.seconds / 60 >= self.beep_at and not self.has_beeped and not self.beep_at == 0:
|
|
|
|
text0 += '\a'
|
2023-04-20 10:22:54 +02:00
|
|
|
text3 = "🔔Beep!🔔"
|
2023-04-18 20:30:49 +02:00
|
|
|
self.has_beeped = True
|
|
|
|
if self.enable_sound:
|
|
|
|
p = Process(target=beepy.beep, kwargs={"sound": "success"})
|
|
|
|
p.start()
|
2023-04-20 10:22:54 +02:00
|
|
|
sys.stdout.write('\r' + text0+"\t\t"+text1+"\t\t"+text2+"\t\t"+text3 + "\t")
|
2023-04-14 14:27:45 +02:00
|
|
|
sys.stdout.flush()
|
2023-04-18 20:30:49 +02:00
|
|
|
time.sleep(0.1)
|
2023-04-14 14:27:45 +02:00
|
|
|
|
|
|
|
def main():
|
2023-04-18 20:30:49 +02:00
|
|
|
parser = argparse.ArgumentParser(prog="stopwatch", description='Simple CLI stopwatch.')
|
|
|
|
parser.add_argument('-b', '--beep', metavar='N', type=float, nargs='+',
|
|
|
|
help='beep after x minutes', default=[0])
|
|
|
|
parser.add_argument('-s', '--sound',
|
|
|
|
action='store_true')
|
|
|
|
args = parser.parse_args()
|
2023-04-14 14:27:45 +02:00
|
|
|
try:
|
2023-04-18 20:30:49 +02:00
|
|
|
timer = stopwatch(args.beep[0], enable_sound=args.sound)
|
2023-04-14 14:27:45 +02:00
|
|
|
timer.display()
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
pass
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|