Only show 2 biggest units in !seen

This commit is contained in:
jesopo 2018-08-29 15:52:04 +01:00
parent 745141cb6d
commit ceb73f586c
2 changed files with 21 additions and 9 deletions

View file

@ -175,23 +175,34 @@ def from_pretty_time(pretty_time):
if seconds > 0: if seconds > 0:
return seconds return seconds
def to_pretty_time(total_seconds): UNIT_SECOND = 5
UNIT_MINUTE = 4
UNIT_HOUR = 3
UNIT_DAY = 2
UNIT_WEEK = 1
def to_pretty_time(total_seconds, minimum_unit=UNIT_SECOND, max_units=6):
minutes, seconds = divmod(total_seconds, 60) minutes, seconds = divmod(total_seconds, 60)
hours, minutes = divmod(minutes, 60) hours, minutes = divmod(minutes, 60)
days, hours = divmod(hours, 24) days, hours = divmod(hours, 24)
weeks, days = divmod(days, 7) weeks, days = divmod(days, 7)
out = "" out = ""
if not weeks == 0: units = 0
if weeks and minimum_unit >= UNIT_WEEK and units < max_units:
out += "%dw" % weeks out += "%dw" % weeks
if not days == 0: units += 1
if days and minimum_unit >= UNIT_DAY and units < max_units:
out += "%dd" % days out += "%dd" % days
if not hours == 0: units += 1
if hours and minimum_unit >= UNIT_HOUR and units < max_units:
out += "%dh" % hours out += "%dh" % hours
if not minutes == 0: units += 1
if minutes and minimum_unit >= UNIT_MINUTE and units < max_units:
out += "%dm" % minutes out += "%dm" % minutes
if not seconds == 0: units += 1
if seconds and minimum_unit >= UNIT_SECOND and units < max_units:
out += "%ds" % seconds out += "%ds" % seconds
units += 1
return out return out
IS_TRUE = ["true", "yes", "on", "y"] IS_TRUE = ["true", "yes", "on", "y"]

View file

@ -18,9 +18,10 @@ class Module(object):
seen_seconds = event["server"].get_user(event["args_split"][0] seen_seconds = event["server"].get_user(event["args_split"][0]
).get_setting("seen") ).get_setting("seen")
if seen_seconds: if seen_seconds:
since, unit = Utils.time_unit(time.time()-seen_seconds) since = Utils.to_pretty_time(time.time()-seen_seconds,
event["stdout"].write("%s was last seen %s %s ago" % ( max_units=2)
event["args_split"][0], since, unit)) event["stdout"].write("%s was last seen %s ago" % (
event["args_split"][0], since))
else: else:
event["stderr"].write("I have never seen %s before." % ( event["stderr"].write("I have never seen %s before." % (
event["args_split"][0])) event["args_split"][0]))