Skip to content

Instantly share code, notes, and snippets.

@CtrlAltCuteness
Created July 13, 2020 15:41
Show Gist options
  • Save CtrlAltCuteness/ddd98e54d1ecfbcbae87d9f4a84dc2f5 to your computer and use it in GitHub Desktop.
Save CtrlAltCuteness/ddd98e54d1ecfbcbae87d9f4a84dc2f5 to your computer and use it in GitHub Desktop.
#!/data/data/com.termux/files/usr/bin/python3
# vim: fileencoding=utf8:filetype=python:nu:ts=2:shiftwidth=2:softtabstop=2:expandtab
__all__ = []
from sys import argv
from time import monotonic, sleep
from math import floor, ceil
from re import compile as compile_, sub
def asSpan (x, u):
if type(x) is not int or x < 0: return '!'
x, y = divmod(x, 60)
r = [ (y, 's') ]
x, y = divmod(x, 60)
r.append( (y, 'm') )
x, y = divmod(x, 24)
r.append( (y, 'h') )
r.append( (x, 'd') )
return ' '.join('{:02d}{}'.format(*i) for i in r[::-1][u:])
def main2 (target, u, label):
while True:
x = ceil(target - monotonic())
if x <= 0:
output(label + 'Done')
return
output(label + asSpan(x, u))
sleep(0.25)
def main (arg, label='', eol=''):
arg = filter(None, sub(r'[,\s]+', ',', arg).split(','))
t = 0
p = compile_(r'''([0-9]+)([smhd]?)''')
d = { 's': 1, '': 1, 'm': 60, 'h': 3600, 'd': 86400 }
for i in arg:
i, x = p.fullmatch(i).groups()
t += int(i) * d[x]
u = -1 - sum( (1 if t >= i else 0) for i in (60, 3600, 86400) )
if eol: label += '\n'
del p, d, i, x, eol
arg = '\aPress [Enter] to re-run with: {}\nUse EOF / Break to exit.\n'.format(asSpan(t, u))
while True:
main2(t + monotonic(), u, label)
input(arg)
def output (text):
print('\x1b[3J\x1b[H\x1b[m\x1b[2J{}'.format(text), flush=True)
if __name__ == '__main__':
try:
main(*argv[1:])
except Exception as e:
#raise e
try:
print()
except:
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment