~fabrixxm/confy

19c43436f7436e1d03c6f2a6b0d144e85ab660f7 — Evangelos Ribeiro Tzaras 4 months ago f669c15
Fix division by zero for events with zero duration

When a event has duration zero because the start and end times are identical,
ZeroDivisionError's are raised:

```
Traceback (most recent call last):
  File "/home/fortysixandtwo/install/lib/python3.9/site-packages/confy/widgets.py", line 130, in _on_draw
    prc = min(max(pos/dur, 0.0), 1.0)
ZeroDivisionError: float division by zero
```

This patch ensures that prc is either 0 if the event is in the future
or 1, if it's in the past.

Closes #18
2 files changed, 14 insertions(+), 2 deletions(-)

M src/pages.py
M src/widgets.py
M src/pages.py => src/pages.py +7 -1
@@ 507,7 507,13 @@ class EventDetailPage(BasePage):

        dur = (obj.end.timestamp() - obj.start.timestamp())
        pos = (now.timestamp() - obj.start.timestamp())
        prc = min(max(pos/dur, 0.0), 1.0)
        if dur > 0:
            prc = min(max(pos/dur, 0.0), 1.0)
        else:
            # not a valid event duration
            # set prc to 0 if the event is in the future or 1 if in the past
            prc = int(pos >= 0)

        x = w * prc

        cr.set_source_rgb(0, 0, 0)

M src/widgets.py => src/widgets.py +7 -1
@@ 127,7 127,13 @@ class EventActionRow(Handy.ActionRow):

        dur = (obj.end.timestamp() - obj.start.timestamp())
        pos = (now.timestamp() - obj.start.timestamp())
        prc = min(max(pos/dur, 0.0), 1.0)
        if dur > 0:
            prc = min(max(pos/dur, 0.0), 1.0)
        else:
            # not a valid event duration
            # set prc to 0 if the event is in the future or 1 if in the past
            prc = int(pos >= 0)

        y = h * prc

        cr.set_source_rgb(0, 0, 0)