add subcommands
This commit is contained in:
parent
0912288522
commit
a23a944a03
1 changed files with 43 additions and 0 deletions
43
ttracker.py
43
ttracker.py
|
@ -1,5 +1,6 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
import argparse
|
||||
import datetime
|
||||
import sys, tempfile, os
|
||||
import sqlite3
|
||||
|
@ -17,9 +18,26 @@ def db_get_current_entry(cur):
|
|||
return res
|
||||
return (None, None, None)
|
||||
|
||||
def show_last_entries(cur):
|
||||
res = cur.execute("SELECT * FROM ttracker ORDER BY start DESC LIMIT 10").fetchall()
|
||||
for e in res:
|
||||
start = datetime.datetime.fromtimestamp(e[0])
|
||||
if e[1] is not None:
|
||||
end = datetime.datetime.fromtimestamp(e[1])
|
||||
print(f'{when_are_we(start.date())}, {start.strftime("%H:%M")}-{end.strftime("%H:%M")}: {e[2]}')
|
||||
else:
|
||||
print(f'{when_are_we(start.date())}, {start.strftime("%H:%M")}-')
|
||||
|
||||
|
||||
def new_entry(cur, now):
|
||||
cur.execute(f'INSERT INTO ttracker VALUES (?, null, null)', (int(now.timestamp()),))
|
||||
|
||||
def cancel_last_entry(cur, entry):
|
||||
if entry[0] != None:
|
||||
cur.execute("DELETE FROM ttracker WHERE start = ?", (entry[0],))
|
||||
else:
|
||||
cur.execute("UPDATE ttracker SET end=null, text=null WHERE start = (SELECT start FROM ttracker ORDER BY start DESC LIMIT 1)")
|
||||
|
||||
def end_entry(cur, entry, now):
|
||||
entry_start = datetime.datetime.fromtimestamp(entry[0])
|
||||
|
||||
|
@ -63,11 +81,36 @@ def when_are_we(date):
|
|||
return f'{delta} days ago'
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(
|
||||
prog='ttracker',
|
||||
description='Simple time tracker')
|
||||
|
||||
subparsers = parser.add_subparsers()
|
||||
parser_show = subparsers.add_parser('show')
|
||||
parser_show.set_defaults(show=True)
|
||||
|
||||
parser_show = subparsers.add_parser('cancel')
|
||||
parser_show.set_defaults(cancel=True)
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
now = datetime.datetime.now()
|
||||
con = sqlite3.connect(DATABASE)
|
||||
cur = con.cursor()
|
||||
db_init(cur)
|
||||
|
||||
if 'show' in args:
|
||||
show_last_entries(cur)
|
||||
return
|
||||
|
||||
entry = db_get_current_entry(cur)
|
||||
|
||||
if 'cancel' in args:
|
||||
cancel_last_entry(cur, entry)
|
||||
con.commit()
|
||||
show_last_entries(cur)
|
||||
return
|
||||
|
||||
if entry[0] is None:
|
||||
new_entry(cur, now)
|
||||
print(f'Add new entry starting at {now.strftime("%a %d %b")} ({when_are_we(now.date())}) - {now.strftime("%H:%M")}')
|
||||
|
|
Loading…
Add table
Reference in a new issue