diff options
| -rw-r--r-- | formatting.py | 24 | ||||
| -rw-r--r-- | inputs.py | 40 | ||||
| -rw-r--r-- | main.py | 50 |
3 files changed, 114 insertions, 0 deletions
diff --git a/formatting.py b/formatting.py new file mode 100644 index 0000000..a7c7ed7 --- /dev/null +++ b/formatting.py @@ -0,0 +1,24 @@ +import os +import csv + +def sec_to_min(sec): + """converts seconds to MM:SS""" + mins = int(sec / 60) + sec_left = sec % 60 + return str(mins).zfill(2) + ":" + str(sec_left).zfill(2) + +def sess_to_log(session, filename): + """writes session dict to logfile specified""" + fields = [] + for field in session.keys(): + fields.append(field) + + if os.path.exists(filename): + logmode = 'a' + else: + logmode = 'w' + with open(filename, logmode) as logfile: + writer = csv.DictWriter(logfile, fieldnames=fields) + if logmode == 'w': + writer.writeheader() + writer.writerow(session) diff --git a/inputs.py b/inputs.py new file mode 100644 index 0000000..47160c3 --- /dev/null +++ b/inputs.py @@ -0,0 +1,40 @@ +import sys, tty, termios + +def getch(): + import sys, tty, termios + old_settings = termios.tcgetattr(0) + new_settings = old_settings[:] + new_settings[3] &= ~termios.ICANON + try: + termios.tcsetattr(0, termios.TCSANOW, new_settings) + ch = sys.stdin.read(1) +# except BlockingIOError: +# pass + finally: + termios.tcsetattr(0, termios.TCSANOW, old_settings) + return ch + +def get_valid_char(char_list): + """returns input if it matches a list of valid chars, else retruns False""" + valid = False + + in_char = getch() + for char in char_list: + if in_char == char: + valid = True + break + + if not valid: + return False + return in_char + +def get_pos_int(): + """returns input as int; returns False if not a positive integer""" + try: + in_int = int(input()) + except: + return False + else: + if in_int < 1: + return False + return in_int @@ -0,0 +1,50 @@ +import pygal +import csv +import time +import datetime +import hashlib +from collections import OrderedDict +import inputs +import formatting as f + +print("welcome to lock-n-log, the best way to organise and analyse your focus time!") + +valid_timer_types = ('t', 's') +option = False +while not option: + print("(t)imer or (s)topwatch? ", end="", flush=True) + option = inputs.get_valid_char(valid_timer_types) + print("") + +if option == 't': + print("timer length (in minutes)? ", end="", flush=True) + timer_length = inputs.get_pos_int() + while not timer_length: + print("please enter a positive integer") + print("timer length (in minutes)? ", end="", flush=True) + timer_length = inputs.get_pos_int() + + # need timer to be accurate; sleep until next second start? processing time + # although small will still add to time so needs to be avoided + # timer loop + time_left = timer_length #* 60 + time_start = time.time() + while time_left: + # see https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 + print("\x1b[2K\r" + f.sec_to_min(time_left), end="", flush=True) + time_left -= 1 + time.sleep(1) + + print("\nfocus session finished!\nlogging...") + session = OrderedDict() + session["id"] = hashlib.sha256(str(time_start).encode('utf8')).hexdigest() + session["date"] = time.strftime("%Y-%m-%d") + session["start"] = time.strftime("%H:%M") + session["elapsed"] = timer_length * 60 + session["type"] = "focus" + session["tag"] = "work" + session["timer"] = "timer" + session["ratio"] = 0.2 + #print(session) + + f.sess_to_log(session, "lock-n-log.csv") |
