Skip to content

Commit

Permalink
display dates in serialized string, if required
Browse files Browse the repository at this point in the history
Breaking: removed prepend_date argument.
  • Loading branch information
ederag committed Dec 9, 2019
1 parent c599a00 commit d8c6425
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/hamster/edit_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,11 @@ def on_tags_changed(self, widget):
def update_cmdline(self, select=None):
"""Update the cmdline entry content."""
self.cmdline_fact = self.fact.copy(description=None)
label = self.cmdline_fact.serialized(prepend_date=False)
label = self.cmdline_fact.serialized(default_day=self.date)
with self.cmdline.handler_block(self.cmdline.checker):
self.cmdline.set_text(label)
if select:
time_str = self.cmdline_fact.serialized_time(prepend_date=False)
time_str = self.cmdline_fact.serialized_time(default_day=self.date)
self.cmdline.select_region(0, len(time_str))

def update_fields(self):
Expand Down
34 changes: 23 additions & 11 deletions src/hamster/lib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@

from copy import deepcopy

from hamster.lib.parsing import DATE_FMT, TIME_FMT, parse_fact
from hamster.lib.parsing import DATE_FMT, TIME_FMT, DATETIME_FMT, parse_fact
from hamster.lib.stuff import (
datetime_to_hamsterday,
hamsterday_time_to_datetime,
hamster_now,
hamster_today,
)


Expand Down Expand Up @@ -150,20 +151,31 @@ def serialized_name(self):
res += " %s" % " ".join("#%s" % tag for tag in self.tags)
return res

def serialized_time(self, prepend_date=True):
time = ""
def serialized_range(self, default_day=None):
"""Return a string representing the time range.
Start date is shown only if start does not belong to default_day.
End date is shown only if end does not belong to
the same hamster day as start.
"""
time_str = ""
if self.start_time:
if prepend_date:
time += self.date.strftime(DATE_FMT) + " "
time += self.start_time.strftime(TIME_FMT)
if datetime_to_hamsterday(self.start_time) != default_day:
time_str += self.start_time.strftime(DATETIME_FMT)
else:
time_str += self.start_time.strftime(TIME_FMT)
if self.end_time:
time = "%s-%s" % (time, self.end_time.strftime(TIME_FMT))
return time
if datetime_to_hamsterday(self.end_time) != datetime_to_hamsterday(self.start_time):
end_time_str = self.end_time.strftime(DATETIME_FMT)
else:
end_time_str = self.end_time.strftime(TIME_FMT)
time_str = "{} - {}".format(time_str, end_time_str)
return time_str

def serialized(self, prepend_date=True):
def serialized(self, default_day=None):
"""Return a string fully representing the fact."""
name = self.serialized_name()
datetime = self.serialized_time(prepend_date)
datetime = self.serialized_range(default_day)
# no need for space if name or datetime is missing
space = " " if name and datetime else ""
return "{}{}{}".format(datetime, space, name)
Expand Down Expand Up @@ -192,7 +204,7 @@ def __eq__(self, other):
)

def __repr__(self):
return self.serialized(prepend_date=True)
return self.serialized(default_day=None)


def default_logger(name):
Expand Down
1 change: 1 addition & 0 deletions src/hamster/lib/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

DATE_FMT = "%Y-%m-%d" # ISO format
TIME_FMT = "%H:%M"
DATETIME_FMT = "{} {}".format(DATE_FMT, TIME_FMT)
# separator between times and activity
ACTIVITY_SEPARATOR = "\s+"

Expand Down
2 changes: 1 addition & 1 deletion src/hamster/widgets/activityentry.py
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ def update_suggestions(self, text=""):

if variant_fact:
variant_fact.description = None
variant = variant_fact.serialized(prepend_date=False)
variant = variant_fact.serialized(default_day=None)
variants.append((description, variant))

else:
Expand Down

0 comments on commit d8c6425

Please sign in to comment.