Skip to content

Instantly share code, notes, and snippets.

@tebeka
Created December 8, 2020 18:26
Show Gist options
  • Save tebeka/624f6b1e6d4ae56baffe2af31c7737c3 to your computer and use it in GitHub Desktop.
Save tebeka/624f6b1e6d4ae56baffe2af31c7737c3 to your computer and use it in GitHub Desktop.
"Back in" progress bar
#!/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