Add humanize_remaining formatter (#671)

* Add humanize_remaining formatter

Allows for formating the time remaning to next event in _human readable
format_. Work only if `humanize` module is available.
This commit is contained in:
Josip Janzic 2018-12-21 03:28:38 +01:00 committed by chestm007
parent 9e3b45812c
commit b31b5e4f49

View File

@ -8,6 +8,11 @@ from i3pystatus import IntervalModule, formatp, SettingsBase
from i3pystatus.core.color import ColorRangeModule from i3pystatus.core.color import ColorRangeModule
from i3pystatus.core.desktop import DesktopNotification from i3pystatus.core.desktop import DesktopNotification
try:
import humanize
except ImportError:
pass
def strip_microseconds(delta): def strip_microseconds(delta):
return delta - timedelta(microseconds=delta.microseconds) return delta - timedelta(microseconds=delta.microseconds)
@ -50,7 +55,8 @@ class CalendarEvent:
""" """
event_dict = dict( event_dict = dict(
title=self.title, title=self.title,
remaining=self.time_remaining remaining=self.time_remaining,
humanize_remaining=self.humanize_time_remaining,
) )
def is_formatter(x): def is_formatter(x):
@ -64,6 +70,13 @@ class CalendarEvent:
def time_remaining(self): def time_remaining(self):
return strip_microseconds(self.start - datetime.now(tz=self.start.tzinfo)) return strip_microseconds(self.start - datetime.now(tz=self.start.tzinfo))
@property
def humanize_time_remaining(self):
try:
return humanize.naturaltime(datetime.now(tz=self.start.tzinfo) - self.start)
except NameError:
raise ImportError('Missing humanize module')
def __str__(self): def __str__(self):
return "{}(title='{}', start={}, end={}, recurring={})" \ return "{}(title='{}', start={}, end={}, recurring={})" \
.format(type(self).__name__, .format(type(self).__name__,
@ -112,8 +125,11 @@ class Calendar(IntervalModule, ColorRangeModule):
* {title} - the title or summary of the event * {title} - the title or summary of the event
* {remaining_time} - how long until this event is due * {remaining_time} - how long until this event is due
* {humanize_remaining} - how long until this event is due in human readable format
Additional formatters may be provided by the backend, consult their documentation for details. Additional formatters may be provided by the backend, consult their documentation for details.
.. note:: Optionally requires `humanize` to display time in human readable format.
""" """
settings = ( settings = (