From 24057bf533b6cf4fa6b95af2587468ce31f984ce Mon Sep 17 00:00:00 2001 From: kleph Date: Wed, 31 Mar 2021 04:33:22 +0200 Subject: [PATCH] [tool] Add basic timer --- timer.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100755 timer.py diff --git a/timer.py b/timer.py new file mode 100755 index 0000000..1204552 --- /dev/null +++ b/timer.py @@ -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) \ No newline at end of file