[tool] Add basic timer

This commit is contained in:
kleph 2021-03-31 04:33:22 +02:00
parent 954c79738b
commit 24057bf533

28
timer.py Executable file
View file

@ -0,0 +1,28 @@
#!/usr/bin/python
import argparse
import os
import time
TIMER_FILE = '/home/kleph/stream/timer.txt'
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--timer', help="start time in seconds", type=int, default=300)
parser.add_argument('-f', '--file', help="where to write the time", type=str, default=TIMER_FILE)
args = parser.parse_args()
def write_countdown(filename, stimer):
t = stimer
with open(filename, 'w') as f:
while t > 0:
t -= 1
min, sec = divmod(t, 60)
print(f'{min:02d}:{sec:02d}')
f.seek(0)
f.write(f'{min:02d}:{sec:02d}')
os.fsync(f)
time.sleep(1)
write_countdown(args.file, args.timer)