diff options
-rw-r--r-- | README.md | 3 | ||||
-rw-r--r-- | timebomb.py | 30 |
2 files changed, 24 insertions, 9 deletions
@@ -4,4 +4,5 @@ ### TODO -- [ ] progress bar: dynamic length (depending on terminal width) +- [x] progress bar: dynamic length (depending on terminal width) +- [x] fix input like -m 400 -> convert to mins and secs diff --git a/timebomb.py b/timebomb.py index 9cb8496..c3229ce 100644 --- a/timebomb.py +++ b/timebomb.py @@ -9,6 +9,7 @@ from tkinter import messagebox from tkinter import Canvas import argparse import time +import shutil root = Tk() @@ -59,18 +60,31 @@ def main(): message(m=time_m, s=time_s) def sleep(m, s): + time_sum_s = - (m * 60) + s + time_m, time_s = divmod(time_sum_s, 60) + print('Start: ' + time.ctime()) - print('Sleeping for: ' + str(m) \ - + ' mins and ' + str(s) + ' seconds') + print('Sleeping for: ' + str(time_m) \ + + ' mins and ' + str(time_s) + ' seconds') + + for x in range(time_sum_s): + # because dynamic resizing this needs to be here + cols, rows = shutil.get_terminal_size(fallback=(80, 24)) - sleep_time_s = s + (m*60) - pbar_len = 40 - for x in range(sleep_time_s): pr_m, pr_s = divmod(x, 60) - print(' {:02d}:{:02d} '.format(pr_m, pr_s), end='') - progress = int(x/(sleep_time_s-1) * pbar_len) + left = ' {:02d}:{:02d} '.format(pr_m, pr_s) + right = ' {:02d}:{:02d} '.format(time_m, time_s) + + pbar_len = cols - len(left) - len(right) + if pbar_len < 20: + pbar_len = 20 + + progress = int(x/(time_sum_s-1) * pbar_len) + + print(left, end='') print('#' * progress + '_' * int(pbar_len-progress), end='') - print(' {:02d}:{:02d} '.format(m, s), end='\r') + print(right, end='\r') + time.sleep(1) if __name__ == '__main__': |