added beeping and timeout notification
This commit is contained in:
parent
36e6302cfa
commit
30655c26fc
|
@ -1,4 +1,7 @@
|
|||
#!/bin/env python3
|
||||
import beepy # pip install beepy
|
||||
from multiprocessing import Process
|
||||
import argparse
|
||||
import time
|
||||
import datetime
|
||||
import sys
|
||||
|
@ -6,23 +9,55 @@ import time
|
|||
|
||||
|
||||
class stopwatch:
|
||||
def __init__(self) -> None:
|
||||
|
||||
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:
|
||||
self.start_time = datetime.datetime.now().replace(microsecond=0)
|
||||
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
|
||||
print("Start time:\t%s" % self.start_time)
|
||||
print('='*120)
|
||||
|
||||
|
||||
def display(self) -> None:
|
||||
print("started at:\t%s" % self.start_time)
|
||||
text2 = ""
|
||||
while True:
|
||||
now = datetime.datetime.now().replace(microsecond=0)
|
||||
elapsed = (now - self.start_time)
|
||||
text0 = ("\rcurrent:\t%s" % now)
|
||||
text1 = ("elapsed:\t%s" % elapsed)
|
||||
sys.stdout.write('\r' + str(text0+"\t\t"+text1) + "\t" * 3)
|
||||
if elapsed.seconds / 60 >= self.beep_at and not self.has_beeped and not self.beep_at == 0:
|
||||
text0 += '\a'
|
||||
text2 = "🔔Beep!🔔"
|
||||
self.has_beeped = True
|
||||
if self.enable_sound:
|
||||
p = Process(target=beepy.beep, kwargs={"sound": "success"})
|
||||
p.start()
|
||||
sys.stdout.write('\r' + str(text0+"\t\t"+text1+"\t\t"+text2) + "\t" * 3)
|
||||
sys.stdout.flush()
|
||||
time.sleep(1)
|
||||
time.sleep(0.1)
|
||||
|
||||
def main():
|
||||
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()
|
||||
try:
|
||||
timer = stopwatch()
|
||||
timer = stopwatch(args.beep[0], enable_sound=args.sound)
|
||||
timer.display()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
|
Loading…
Reference in New Issue