From dd3031fcc3bab891aee70c0419f13d33f3efeacc Mon Sep 17 00:00:00 2001 From: Christopher Thompson Date: Sat, 10 Oct 2020 00:14:22 -0400 Subject: [PATCH] Factor out countdown timer (and add another docstring) --- pomodoroprompt.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/pomodoroprompt.py b/pomodoroprompt.py index 4d15e37..9424d49 100644 --- a/pomodoroprompt.py +++ b/pomodoroprompt.py @@ -84,8 +84,17 @@ def record_task(whatdid, start, end=None): timewriter.writerow([start, end, whatdid]) def str_minutes(time_diff): + """ Return at least whole seconds from a time difference + """ return str(time_diff).split('.')[0] +def countdown_to(until): + """ Display a timer counting down to until + """ + while datetime.now(pytz.utc) <= until: + to_go = until-datetime.now(pytz.utc) + print('\r', str_minutes(to_go), sep='', end='') + time.sleep(1) def main(): whatdid = '' @@ -97,10 +106,7 @@ def main(): log_step('Start with plan: ' + whatnext, start) try: print('Working on: ', whatnext) - while datetime.now(pytz.utc) <= end: - to_go = end-datetime.now(pytz.utc) - print('\r', str_minutes(to_go), sep='', end='') - time.sleep(1) + countdown_to(end) playsound('res/timesup.aac') whatdid = pomodoro_prompt_report(whatnext) record_task(whatdid, start) @@ -111,10 +117,7 @@ def main(): # We're taking a break. Clear out task start/end times. start = None end = datetime.now(pytz.utc) + timedelta(minutes=SHORT_MIN) - while datetime.now(pytz.utc) <= end: - to_go = end-datetime.now(pytz.utc) - print('\r', str_minutes(to_go), sep='', end='') - time.sleep(1) + countdown_to(end) playsound('res/timesup.aac') continue except KeyboardInterrupt: