From ceb73f586c73d2bd6135681a36df5854a90c5d96 Mon Sep 17 00:00:00 2001 From: jesopo Date: Wed, 29 Aug 2018 15:52:04 +0100 Subject: [PATCH] Only show 2 biggest units in !seen --- Utils.py | 23 +++++++++++++++++------ modules/seen.py | 7 ++++--- 2 files changed, 21 insertions(+), 9 deletions(-) diff --git a/Utils.py b/Utils.py index beedb219..773e3150 100644 --- a/Utils.py +++ b/Utils.py @@ -175,23 +175,34 @@ def from_pretty_time(pretty_time): if seconds > 0: 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) hours, minutes = divmod(minutes, 60) days, hours = divmod(hours, 24) weeks, days = divmod(days, 7) out = "" - if not weeks == 0: + units = 0 + if weeks and minimum_unit >= UNIT_WEEK and units < max_units: out += "%dw" % weeks - if not days == 0: + units += 1 + if days and minimum_unit >= UNIT_DAY and units < max_units: out += "%dd" % days - if not hours == 0: + units += 1 + if hours and minimum_unit >= UNIT_HOUR and units < max_units: out += "%dh" % hours - if not minutes == 0: + units += 1 + if minutes and minimum_unit >= UNIT_MINUTE and units < max_units: out += "%dm" % minutes - if not seconds == 0: + units += 1 + if seconds and minimum_unit >= UNIT_SECOND and units < max_units: out += "%ds" % seconds + units += 1 return out IS_TRUE = ["true", "yes", "on", "y"] diff --git a/modules/seen.py b/modules/seen.py index ad2e7dc6..750b06e4 100644 --- a/modules/seen.py +++ b/modules/seen.py @@ -18,9 +18,10 @@ class Module(object): seen_seconds = event["server"].get_user(event["args_split"][0] ).get_setting("seen") if seen_seconds: - since, unit = Utils.time_unit(time.time()-seen_seconds) - event["stdout"].write("%s was last seen %s %s ago" % ( - event["args_split"][0], since, unit)) + since = Utils.to_pretty_time(time.time()-seen_seconds, + max_units=2) + event["stdout"].write("%s was last seen %s ago" % ( + event["args_split"][0], since)) else: event["stderr"].write("I have never seen %s before." % ( event["args_split"][0]))