Change Utils.color to take a string to wrap in color

This commit is contained in:
jesopo 2018-09-21 11:38:55 +01:00
parent 62df014b29
commit a87196c608
5 changed files with 20 additions and 24 deletions

View file

@ -156,12 +156,12 @@ FONT_BOLD, FONT_ITALIC, FONT_UNDERLINE, FONT_INVERT = ("\x02", "\x1D",
"\x1F", "\x16") "\x1F", "\x16")
FONT_COLOR, FONT_RESET = "\x03", "\x0F" FONT_COLOR, FONT_RESET = "\x03", "\x0F"
def color(foreground, background=None): def color(s, foreground, background=None):
foreground = str(foreground).zfill(2) foreground = str(foreground).zfill(2)
if background: if background:
background = str(background).zfill(2) background = str(background).zfill(2)
return "%s%s%s" % (FONT_COLOR, foreground, return "%s%s%s%s%s" % (FONT_COLOR, foreground,
"" if not background else ",%s" % background) "" if not background else ",%s" % background, s, FONT_COLOR)
def bold(s): def bold(s):
return "%s%s%s" % (FONT_BOLD, s, FONT_BOLD) return "%s%s%s" % (FONT_BOLD, s, FONT_BOLD)

View file

@ -19,7 +19,7 @@ CHOICES = [
"It is certain", "It is certain",
"Naturally", "Naturally",
"Reply hazy, try again later", "Reply hazy, try again later",
Utils.color(4) + Utils.underline("DO NOT WASTE MY TIME"), Utils.underline(Utils.color("DO NOT WASTE MY TIME", Utils.COLOR_RED)),
"Hmm... Could be!", "Hmm... Could be!",
"I'm leaning towards no", "I'm leaning towards no",
"Without a doubt", "Without a doubt",

View file

@ -37,12 +37,10 @@ class Out(object):
class StdOut(Out): class StdOut(Out):
def prefix(self): def prefix(self):
return "%s%s%s" % (Utils.color(Utils.COLOR_GREEN), return Utils.color(Utils.bold(self.module_name), Utils.COLOR_GREEN)
self.module_name, Utils.FONT_RESET)
class StdErr(Out): class StdErr(Out):
def prefix(self): def prefix(self):
return "%s!%s%s" % (Utils.color(Utils.COLOR_RED), return Utils.color(Utils.bold(self.module_name), Utils.COLOR_RED)
self.module_name, Utils.FONT_RESET)
class Module(object): class Module(object):
def __init__(self, bot, events, exports): def __init__(self, bot, events, exports):

View file

@ -145,10 +145,13 @@ class Module(object):
duck += DUCK_TAIL duck += DUCK_TAIL
duck += random.choice(DUCK_HEAD) duck += random.choice(DUCK_HEAD)
duck = str(Utils.color(4) + Utils.bold( if random.randint(1, 20) == 1:
duck + random.choice(DUCK_MESSAGE_RARE)) + Utils.color( # rare!
4)) if 1 == random.randint(1, 20) else duck + random.choice( message = random.choice(DUCK_MESSAGE_RARE)
DUCK_MESSAGE) duck = Utils.color(Utils.bold(duck + message), Utils.COLOR_RED)
else:
# not rare!
duck += random.choice(DUCK_MESSAGE)
channel.send_message(duck) channel.send_message(duck)
channel.games["ducks"]["duck_spawned"] = 1 channel.games["ducks"]["duck_spawned"] = 1

View file

@ -195,8 +195,7 @@ class Module(object):
query["stationManagerCode"]) query["stationManagerCode"])
else: else:
station_summary = "%s (%s, %s%s)" % (query["locationName"], query["crs"], query["stationManagerCode"], station_summary = "%s (%s, %s%s)" % (query["locationName"], query["crs"], query["stationManagerCode"],
", %s%s severe messages%s" % (Utils.color(Utils.COLOR_RED), nrcc_severe, Utils.color(Utils.FONT_RESET)) if nrcc_severe else "" ", %s%s severe messages%s" % (Utils.color(nrcc_severe, Utils.COLOR_RED) if nrcc_severe else ""))
)
if not "trainServices" in query and not "busServices" in query and not "ferryServices" in query: if not "trainServices" in query and not "busServices" in query and not "ferryServices" in query:
return event["stdout"].write("%s: No services for the next %s minutes" % ( return event["stdout"].write("%s: No services for the next %s minutes" % (
@ -291,7 +290,7 @@ class Module(object):
t["origin_summary"] if t["terminating"] or filter["type"]=="arrival" else t["dest_summary"] t["origin_summary"] if t["terminating"] or filter["type"]=="arrival" else t["dest_summary"]
) for t in trains_filtered]) ) for t in trains_filtered])
else: else:
trains_string = ", ".join(["%s%s (%s, %s%s%s%s, %s%s%s%s%s)" % ( trains_string = ", ".join(["%s%s (%s, %s%s%s%s, %s%s%s)" % (
"from " if not filter["type"][0] in "ad" and t["terminating"] else '', "from " if not filter["type"][0] in "ad" and t["terminating"] else '',
t["origin_summary"] if t["terminating"] or filter["type"]=="arrival" else t["dest_summary"], t["origin_summary"] if t["terminating"] or filter["type"]=="arrival" else t["dest_summary"],
t["uid"], t["uid"],
@ -300,9 +299,7 @@ class Module(object):
"*" if t["platform_hidden"] else '', "*" if t["platform_hidden"] else '',
"?" if "platformsAreUnreliable" in query and query["platformsAreUnreliable"] else '', "?" if "platformsAreUnreliable" in query and query["platformsAreUnreliable"] else '',
t["times"][filter["type"]]["prefix"].replace(filter["type"][0], '') if not t["cancelled"] else "", t["times"][filter["type"]]["prefix"].replace(filter["type"][0], '') if not t["cancelled"] else "",
Utils.color(colours[t["times"][filter["type"]]["status"]]), Utils.color(t["times"][filter["type"]]["shortest"*filter["st"] or "short"], colours[t["times"][filter["type"]]["status"]]),
t["times"][filter["type"]]["shortest"*filter["st"] or "short"],
Utils.color(Utils.FONT_RESET),
bool(t["activity"])*", " + "+".join(t["activity"]), bool(t["activity"])*", " + "+".join(t["activity"]),
) for t in trains_filtered]) ) for t in trains_filtered])
if event.get("external"): if event.get("external"):
@ -371,7 +368,7 @@ class Module(object):
if "delayReason" in query: if "delayReason" in query:
disruptions.append("Delayed (%s%s)" % (query["delayReason"]["value"], " at " + query["delayReason"]["_tiploc"] if query["delayReason"]["_tiploc"] else "")) disruptions.append("Delayed (%s%s)" % (query["delayReason"]["value"], " at " + query["delayReason"]["_tiploc"] if query["delayReason"]["_tiploc"] else ""))
if disruptions and not external: if disruptions and not external:
disruptions = Utils.color(Utils.COLOR_RED) + ", ".join(disruptions) + Utils.color(Utils.FONT_RESET) + " " disruptions = Utils.color(", ".join(disruptions), Utils.COLOR_RED) + " "
elif disruptions and external: elif disruptions and external:
disruptions = ", ".join(disruptions) disruptions = ", ".join(disruptions)
else: disruptions = "" else: disruptions = ""
@ -450,16 +447,14 @@ class Module(object):
elif station["called"]: elif station["called"]:
station["times"]["arrival"]["status"], station["times"]["departure"]["status"] = 0, 0 station["times"]["arrival"]["status"], station["times"]["departure"]["status"] = 0, 0
station["summary"] = "%s%s (%s%s%s%s%s%s%s)%s" % ( station["summary"] = "%s%s (%s%s%s%s%s)%s" % (
"*" * station["passing"], "*" * station["passing"],
station["name"], station["name"],
station["crs"] + ", " if station["name"] != station["crs"] else '', station["crs"] + ", " if station["name"] != station["crs"] else '',
station["length"] + " cars, " if station["length"] and (station["first"] or (station["last"]) or station["associations"]) else '', station["length"] + " cars, " if station["length"] and (station["first"] or (station["last"]) or station["associations"]) else '',
("~" if station["times"][filter["type"]]["estimate"] else '') + ("~" if station["times"][filter["type"]]["estimate"] else '') +
station["times"][filter["type"]]["prefix"].replace(filter["type"][0], ""), station["times"][filter["type"]]["prefix"].replace(filter["type"][0], ""),
Utils.color(colours[station["times"][filter["type"]]["status"]]), Utils.color(station["times"][filter["type"]]["short"], colours[station["times"][filter["type"]]["status"]]),
station["times"][filter["type"]]["short"],
Utils.color(Utils.FONT_RESET),
", "*bool(station["activity_p"]) + "+".join(station["activity_p"]), ", "*bool(station["activity_p"]) + "+".join(station["activity_p"]),
", ".join([a["summary"] for a in station["associations"]]), ", ".join([a["summary"] for a in station["associations"]]),
) )
@ -498,7 +493,7 @@ class Module(object):
else: else:
event["stdout"].write("%s%s %s %s (%s%s%s/%s/%s): %s" % (disruptions, query["operatorCode"], event["stdout"].write("%s%s %s %s (%s%s%s/%s/%s): %s" % (disruptions, query["operatorCode"],
query["trainid"], query["serviceType"], query["trainid"], query["serviceType"],
Utils.color(Utils.COLOR_LIGHTBLUE), done_count, Utils.color(Utils.FONT_RESET), Utils.color(done_count, Utils.COLOR_LIGHTBLUE),
len(stations_filtered), total_count, len(stations_filtered), total_count,
", ".join([s["summary"] for s in stations_filtered]))) ", ".join([s["summary"] for s in stations_filtered])))