import pygal import csv import time import datetime import datetime import hashlib from collections import OrderedDict import termios import tty import sys import inputs import formatting as f print("welcome to lock-n-log, the best way to organise and analyse your focus time!") print("tag for session? ", end="", flush=True) tag = input() if tag == "": tag = "work" 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() time_left = timer_length start = datetime.datetime.now() end = start + datetime.timedelta(seconds=time_left) # change to mins in final accum = 0 timeout = 1/120 # 120Hz refresh old_settings = termios.tcgetattr(sys.stdin) tty.setcbreak(sys.stdin.fileno()) while True: now = datetime.datetime.now() time_left = end - now if time_left.total_seconds() <= 0: break ch = inputs.getch_nb(timeout) if ch == 'q': print("exited early!") break # see https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797 print("\x1b[2K\r" + inputs.delta_to_HM(time_left), end="", flush=True) accum += timeout if accum >= 1: accum = 0 termios.tcsetattr(sys.stdin, termios.TCSADRAIN, old_settings) print("\nfocus session finished!\nlogging...") session = OrderedDict() session["id"] = hashlib.sha256(str(start).encode('utf8')).hexdigest() session["date"] = start.strftime("%Y-%m-%d") session["start"] = start.strftime("%H:%M") session["elapsed"] = int(timer_length - time_left.total_seconds()) session["type"] = "focus" session["tag"] = tag session["timer"] = "timer" session["ratio"] = 0.2 #print(session) f.sesh_to_log(session, "lock-n-log.csv")