Created
January 13, 2021 20:33
-
-
Save svenk/40c26706453068fd4277641f3635db65 to your computer and use it in GitHub Desktop.
Simple logging
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import os, re, sys | |
from os import path | |
from pathlib import Path | |
from datetime import datetime, timedelta | |
from prompt_toolkit import prompt # bundled with iPython | |
from prompt_toolkit.completion import WordCompleter | |
pomodoro_directory = path.realpath(path.join(os.getcwd(), path.dirname(path.realpath(__file__)))) | |
logfile = path.join(pomodoro_directory, "log.txt") | |
slurped_logfile = Path(logfile).read_text() | |
hashtags = re.findall(r'(#[\w_-]+)', slurped_logfile) | |
#print("Available hashtags: ", hashtags) | |
hashtag_completer = WordCompleter(hashtags) | |
show_last_n = 1 | |
csv_sep = " " | |
lastlog = slurped_logfile.split("\n")[0:-show_last_n] | |
for l in lastlog: print(l) | |
tend = datetime.now() | |
tstart = tend - timedelta(hours=0, minutes=30) | |
prefix = csv_sep.join([tend.strftime('%Y-%m-%d'), tstart.strftime('%H:%M'), tend.strftime('%H:%M'),'']) | |
try: | |
logline = prompt('', | |
default=prefix, | |
completer=hashtag_completer, | |
complete_while_typing=True) | |
except KeyboardInterrupt: | |
print('Aborted') | |
try: | |
sys.exit(1) | |
except SystemExit: | |
sys.exit(1) | |
with open(logfile, "a") as fh: | |
print(logline, file=fh) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment