Created
December 8, 2020 18:26
-
-
Save tebeka/624f6b1e6d4ae56baffe2af31c7737c3 to your computer and use it in GitHub Desktop.
"Back in" progress bar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
"""Timer with progress bar""" | |
from tqdm import tqdm | |
from time import sleep | |
from argparse import ArgumentParser | |
import re | |
from datetime import datetime, timedelta | |
from subprocess import run, PIPE | |
from os.path import expanduser | |
def duration(val): | |
match = re.match(r'^(\d+)([a-z]+)$', val) | |
if not match: | |
raise ValueError(f'bad duration - {val}') | |
amount, unit = int(match.group(1)), match.group(2) | |
if unit == 's': | |
pass | |
elif unit == 'm': | |
amount *= 60 | |
else: | |
raise ValueError(f'unknown time unit: {val}') | |
return amount | |
parser = ArgumentParser(description=__doc__) | |
parser.add_argument('duration', help='32s, 15m...', type=duration) | |
args = parser.parse_args() | |
end = datetime.now() + timedelta(seconds=args.duration) | |
prefix = f'☕ - Back at {end:%H:%M} ' | |
bar_format = prefix + '{bar}' | |
for _ in tqdm(range(args.duration), bar_format=bar_format): | |
sleep(1) | |
try: | |
run( | |
['mplayer', '-really-quiet', expanduser('~/.beep.wav')], | |
stderr=PIPE, | |
) | |
except KeyboardInterrupt: | |
pass |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment