summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--inputs.py34
-rw-r--r--main.py44
2 files changed, 52 insertions, 26 deletions
diff --git a/inputs.py b/inputs.py
index 47160c3..37044f9 100644
--- a/inputs.py
+++ b/inputs.py
@@ -1,32 +1,35 @@
-import sys, tty, termios
+import sys
+import tty
+import termios
+import select
+import datetime
def getch():
- import sys, tty, termios
- old_settings = termios.tcgetattr(0)
+ """get next character without needing to press enter"""
+ old_settings = termios.tcgetattr(sys.stdin)
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 getch_nb(timeout):
+ """gets next character in cbreak; returns false if none; cbreak must be set before"""
+ ch = False
+ if select.select([sys.stdin], [], [], timeout) == ([sys.stdin], [], []):
+ ch = sys.stdin.read(1)
+ 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
+ return in_char
+ return False
def get_pos_int():
"""returns input as int; returns False if not a positive integer"""
@@ -38,3 +41,8 @@ def get_pos_int():
if in_int < 1:
return False
return in_int
+
+def delta_to_HM(delta):
+ """returns a %H:%M string from a timedelta object"""
+ mins, secs = divmod(int(delta.total_seconds()), 60)
+ return str(mins).zfill(2) + ":" + str(secs).zfill(2)
diff --git a/main.py b/main.py
index 2ce1450..93c6a5d 100644
--- a/main.py
+++ b/main.py
@@ -2,8 +2,12 @@ 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
@@ -29,23 +33,37 @@ if option == 't':
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:
+ 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" + f.sec_to_min(time_left), end="", flush=True)
- time_left -= 1
- time.sleep(1)
+ 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(time_start).encode('utf8')).hexdigest()
- session["date"] = time.strftime("%Y-%m-%d")
- session["start"] = time.strftime("%H:%M")
- session["elapsed"] = timer_length * 60
+ 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"