Skip to content

Commit

Permalink
fix old-school bonus pomodoros to not overlap and to not bleed into t…
Browse files Browse the repository at this point in the history
…he next day

This commit was sponsored by Derek Veit, Jason Walker, Jason Mills,
and my other patrons.  If you want to join them, you can support my
work at https://glyph.im/patrons/.
  • Loading branch information
glyph committed Dec 12, 2024
1 parent 6af247c commit 7001cd6
Showing 1 changed file with 25 additions and 18 deletions.
43 changes: 25 additions & 18 deletions src/pomodouroboros/pommodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -542,12 +542,17 @@ def pendingPomodoros(self) -> Sequence[Pomodoro]:
allPending.pop(0)
return allPending

def bonusPomodoro(self, currentTime: datetime) -> Pomodoro:
def bonusPomodoro(self, currentTime: datetime) -> None:
"""
Create a new pomodoro that doesn't overlap with existing ones.
"""

def lengths() -> tuple[slice, datetime, timedelta, timedelta]:
"""
Look for the place to insert the new pomodoro, expressed as the
position to insert it in the pending intervals list (a 0-width
slice) and the start time, pomodoro length, and break length.
"""
allIntervals = self.elapsedIntervals + self.pendingIntervals
position = slice(len(self.pendingIntervals), 0)
if allIntervals:
Expand All @@ -565,41 +570,43 @@ def lengths() -> tuple[slice, datetime, timedelta, timedelta]:
pomodoroLength = firstPom.endTime - firstPom.startTime
breakLength = firstBreak.endTime - firstBreak.startTime
startingPoint = currentTime

for idx, anInterval in enumerate(allIntervals):
position = slice(idx, 0)
if anInterval.startTime > startingPoint:
potentialEnd = (
startingPoint + pomodoroLength + breakLength
)
if (
not anInterval.startTime
< potentialEnd
< anInterval.endTime
):
break
startingPoint = anInterval.endTime
else:
position = slice(len(self.pendingIntervals), 0)
else:
# Variables (we need to save these attributes in the
# constructor so we don't need to synthesize defaults here.)
pomodoroLength = timedelta(minutes=25)
breakLength = timedelta(minutes=5)
startingPoint = self.endTime


for idx, anInterval in enumerate(self.pendingIntervals):
position = slice(idx, 0)
if anInterval.startTime > startingPoint:
potentialEnd = (
startingPoint + pomodoroLength + breakLength
)
if (
not anInterval.startTime
< potentialEnd
< anInterval.endTime
):
break
startingPoint = anInterval.endTime
else:
position = slice(len(self.pendingIntervals), 0)

return position, startingPoint, pomodoroLength, breakLength

position, startingPoint, pomodoroLength, breakLength = lengths()
newStartTime = max(startingPoint, currentTime)
if (newStartTime + pomodoroLength).date() != self.startTime.date():
return None
newPomodoro = Pomodoro(
None, newStartTime, newStartTime + pomodoroLength
)
newBreak = Break(
newPomodoro.endTime, newPomodoro.endTime + breakLength
)
self.pendingIntervals[position] = [newPomodoro, newBreak]
return newPomodoro

def advanceToTime(
self, currentTimestamp: float, observer: PomObserver
Expand Down

0 comments on commit 7001cd6

Please sign in to comment.