diff options
-rwxr-xr-x | scripts/fancy_sleep.py | 52 |
1 files changed, 30 insertions, 22 deletions
diff --git a/scripts/fancy_sleep.py b/scripts/fancy_sleep.py index e65ad94..342f4c4 100755 --- a/scripts/fancy_sleep.py +++ b/scripts/fancy_sleep.py @@ -117,6 +117,7 @@ NUMBERS = [i.replace('.', ' ') for i in ( . """, )] +NUMBERS_LINES = 5 def print_translated(string): @@ -188,39 +189,44 @@ def test_parse_time(): assert parse_time('8d 6s') == parse_time('8d6s') +def get_hms_from_secs(seconds): + hours = int(seconds / 3600) + minutes = int(seconds / 60) % 60 + seconds = int(seconds) % 60 + return hours, minutes, seconds + + +def get_hms_string_from_secs(seconds): + hours, minutes, seconds = get_hms_from_secs(seconds) + time_string = '%02d:%02d:%02d' % (hours, minutes, seconds) + return time_string + + def fancy_sleep_display(target_time): - print() - print('\n'*5, '\x1b[5A', sep='', end='') - original_termios_attr = termios.tcgetattr(sys.stdin) + original_termios_attr = termios.tcgetattr(sys.stdout) noecho_termios_attr = original_termios_attr[:] noecho_termios_attr[3] &= ~termios.ECHO - termios.tcsetattr(sys.stdin, termios.TCSANOW, noecho_termios_attr) + termios.tcsetattr(sys.stdout, termios.TCSANOW, noecho_termios_attr) try: while True: - delta = target_time.timestamp() - time.time() + delta = target_time - time.time() if delta <= 0: break - hours = int(delta / 3600) - minutes = int(delta / 60) % 60 - seconds = int(delta) % 60 - - print('\x1b[s', end='') # save cursor - time_string = '%02d:%02d:%02d' % (hours, minutes, seconds) - #print('\rremainining time: %s' % time_string, end='') - print_translated(time_string) - print('\x1b[u', end='') # restore cursor - + #print('\x1b[s', end='') # save cursor + print_translated(get_hms_string_from_secs(delta)) + print(f'\x1b[{NUMBERS_LINES}A', end='') # go up 5 lines up + #print('\x1b[u', end='') # restore cursor time.sleep(1) finally: - print() - termios.tcsetattr(sys.stdin, termios.TCSANOW, original_termios_attr) + print('\x1b[5B', end='') # go up 5 lines down + termios.tcsetattr(sys.stdout, termios.TCSANOW, original_termios_attr) def main(): 'function called only when script invoked directly on command line' args = docopt.docopt(__doc__) - - curtime = datetime.datetime.now() + isatty = sys.stdout.isatty() + curtime = datetime.datetime.now().replace(microsecond=0) timedelta = parse_time(args['TIME']) target_time = curtime + timedelta @@ -228,17 +234,19 @@ def main(): print('ERROR: TIME is unparsable or equals to 0', file=sys.stderr) sys.exit(1) - #print('Launched at:', curtime.strftime('%F %T')) if args['--target']: print('Run until:', target_time.strftime('%F %T')) sys.stdout.flush() try: - if not sys.stdout.isatty(): + if not isatty: time.sleep(target_time.timestamp() - time.time()) else: - fancy_sleep_display(target_time) + fancy_sleep_display(int(target_time.timestamp())) except KeyboardInterrupt: + if isatty: + print('slept for', get_hms_string_from_secs( + time.time() - int(curtime.timestamp()))) sys.exit(130) if args['COMMAND']: |