Skip to content

Commit

Permalink
Implemented resume, start now uses ts-objects
Browse files Browse the repository at this point in the history
  • Loading branch information
kwillno committed Jul 31, 2023
1 parent c0c817e commit e1d46f5
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 21 deletions.
29 changes: 24 additions & 5 deletions jakt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,19 @@ def start(self, project: str, tags: list[str]) -> dict:
if tags == ():
tags = ["<no tags>"]

timeslot = {"start": round(time()), "project": project, "tags": tags}

timeslotJSON = json.dumps(timeslot, indent=4)
ts = timeslot(
ID=self.generateUniqueID(),
start=round(time()),
end=None,
project=project,
tags=tags
)

with open(self.pathCurrent, "w") as f:
f.write(timeslotJSON)
f.write(str(ts.toDictString()))
f.close()

return timeslot
return ts

def stop(self) -> timeslot:
if not os.path.exists(self.pathCurrent):
Expand Down Expand Up @@ -143,6 +147,21 @@ def report(self) -> JaktReport:

return JaktReport(self)

def resume(self) -> timeslot:
"""
Starts new timeslot with same options as previously logged timeslot
"""

# Get last logged timeslot
timeslots = self.getTimeslots()
timeslots.reverse()
last_ts = timeslots[0]

response = self.start(project = last_ts.project, tags = last_ts.tags)

return response


## Get and put data
def getCategories(self) -> list[str]:
"""
Expand Down
41 changes: 31 additions & 10 deletions jakt/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ def start(ctx, project, tags):
try:
response = jkt.start(project=project, tags=tags)

project = click.style(project, fg="blue", bold=True)
project = click.style(response.project, fg="blue", bold=True)
hrStart = click.style(
datetime.fromtimestamp(response["start"]).strftime("%H:%M"),
datetime.fromtimestamp(response.start).strftime("%H:%M"),
fg="red",
bold=True,
)
tags = click.style(" ".join(str(t) for t in response["tags"]), fg="green")
tags = click.style(" ".join(str(t) for t in response.tags), fg="green")

click.echo(f"{project} started at {hrStart}")
click.echo(f"Tags: {tags}")
Expand Down Expand Up @@ -278,19 +278,40 @@ def report(ctx, project, tag):
click.echo(f" - {hrTag} {hrTagTime}")


"""

@cli.command()
def pause():
# Takes a break in current timeslot
pass
@click.pass_context
def resume(ctx):
"""
Start new timeslot with same settings
"""
jkt = ctx.obj["jakt"]

try:
response = jkt.resume()

project = click.style(response.project, fg="blue", bold=True)
hrStart = click.style(
datetime.fromtimestamp(response.start).strftime("%H:%M"),
fg="red",
bold=True,
)
tags = click.style(" ".join(str(t) for t in response.tags), fg="green")

click.echo(f"{project} started at {hrStart}")
click.echo(f"Tags: {tags}")

except JaktActiveError:
click.echo("Other timer already running")
ctx.invoke(status)


"""
@cli.command()
def resume():
# Resumes paused timeslot
def pause():
# Takes a break in current timeslot
pass
@cli.command()
def config():
# Sets new values in configuration file
Expand Down
25 changes: 19 additions & 6 deletions jakt/timeslot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
import json


class timeslot:
Expand All @@ -8,15 +9,20 @@ def __init__(self, ID: str, start: int, end: int, project: str, tags: list[str])
self.start = start
self.end = end
self.start_dt = datetime.fromtimestamp(self.start)
self.end_dt = datetime.fromtimestamp(self.end)
if end:
self.end_dt = datetime.fromtimestamp(self.end)

start = datetime.fromtimestamp(self.start)
end = datetime.fromtimestamp(self.end)
self.duration = end - start

else:
self.end_dt = None
self.duration = None

self.project = project
self.tags = tags

# Calculate duration
start = datetime.fromtimestamp(self.start)
end = datetime.fromtimestamp(self.end)
self.duration = end - start


def __str__(self):
return f"ts: {self.id} {self.project} {self.tags} {self.start_dt.strftime('%d-%m-%y %H:%M')} - {self.end_dt.strftime('%H:%M')}"
Expand Down Expand Up @@ -48,6 +54,13 @@ def toDict(self) -> dict:

return obj

def toDictString(self) -> str:
"""
Returns a JSON-string
"""

return json.dumps(self.toDict())

def getDurationHR(self):
"""
Return the data needed to display Human Readale duration
Expand Down

0 comments on commit e1d46f5

Please sign in to comment.