From a47020e5d7150839b02d350063d25a1e3e6c3eec Mon Sep 17 00:00:00 2001 From: Jason Green Date: Thu, 19 Oct 2017 21:48:52 -0400 Subject: [PATCH 1/4] added the ability also specify background color in 8 color mode --- colout/colout.py | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/colout/colout.py b/colout/colout.py index a5744d9..641abf5 100755 --- a/colout/colout.py +++ b/colout/colout.py @@ -44,6 +44,18 @@ context["colors"] = { "magenta": 5, "purple": 5, "cyan": 6, "white": 7, "none": -1 } + +context["backgrounds"] = { + "black": 0, + "red": 1, + "green": 2, + "yellow": 3, + "blue": 4, + "magenta": 5, + "cyan": 6, + "white": 7 + } + context["themes"] = {} # pre-defined colormaps @@ -508,6 +520,7 @@ def colorin(text, color="red", style="normal"): color_code = "" style_code = "" + background_code = "" # Convert the style code if style == "random" or style == "Random": @@ -515,8 +528,11 @@ def colorin(text, color="red", style="normal"): else: if style in context["styles"]: style_code = str(context["styles"][style]) + + color_background = color.strip().split("_") + color = color_background[0] + background = color_background[1] if len(color_background) == 2 else None - color = color.strip() m = mode(color) if color == "none": @@ -572,9 +588,17 @@ def colorin(text, color="red", style="normal"): else: raise UnknownColor(color) + if background in context["backgrounds"] and m == 8: + background_code = endmarks[m] + str(40 + context["backgrounds"][background]) + elif background == None: + pass + else: + raise UnknownColor(background) + + if color_code is not None: if not debug: - return start + style_code + endmarks[m] + color_code + "m" + text + stop + return start + style_code + endmarks[m] + color_code + background_code + "m" + text + stop else: return start + style_code + endmarks[m] + color_code + "m" \ + " Date: Fri, 20 Oct 2017 21:30:43 -0400 Subject: [PATCH 3/4] fixed color --- colout/colout.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/colout/colout.py b/colout/colout.py index d492747..73e43ea 100755 --- a/colout/colout.py +++ b/colout/colout.py @@ -347,7 +347,7 @@ def color_random( color ): global context m = mode(color) if m == 8: - color_name = random.choice(context["colormaps"]["random"]) + color_name = random.choice(list(context["colormaps"]["random"])) color_code = context["colors"][color_name] color_code = str(30 + color_code) From d06b1b449a93483561052415dcb2d418ae811d9f Mon Sep 17 00:00:00 2001 From: nojhan Date: Mon, 26 Feb 2018 17:50:14 +0100 Subject: [PATCH 4/4] Allow background without foreground ("none.blue") Use a configurable fore/background separator. Declare fore- and background colors only once. Pretty print context in debug mode. --- colout/colout.py | 83 ++++++++++++++++++++++++++++-------------------- 1 file changed, 49 insertions(+), 34 deletions(-) diff --git a/colout/colout.py b/colout/colout.py index cd7d68b..422ad6a 100755 --- a/colout/colout.py +++ b/colout/colout.py @@ -18,6 +18,8 @@ import string import hashlib import functools import argparse +import pprint +import copy # set the SIGPIPE handler to kill the program instead of # ending in a write error when a broken pipe occurs @@ -38,23 +40,21 @@ context["styles"] = { "reverse": 7, "conceal": 8 } -# Available color names in 8-colors mode -context["colors"] = { - "black": 0, "red": 1, "green": 2, "yellow": 3, "orange":3, "blue": 4, - "magenta": 5, "purple": 5, "cyan": 6, "white": 7, "none": -1 -} +# Available color names in 8-colors mode. +eight_colors = ["black","red","green","yellow","blue","magenta","cyan","white"] +# Given in that order, the ASCII code is the index. +eight_color_codes = {n:i for i,n in enumerate(eight_colors)} +# One can add synonyms. +eight_color_codes["orange"] = eight_color_codes["yellow"] +eight_color_codes["purple"] = eight_color_codes["magenta"] +# Foreground colors has a special "none" item. +# Note: use copy to avoid having the same reference over fore/background. +context["colors"] = copy.copy(eight_color_codes) +context["colors"]["none"] = -1 -context["backgrounds"] = { - "black": 0, - "red": 1, - "green": 2, - "yellow": 3, - "blue": 4, - "magenta": 5, - "cyan": 6, - "white": 7 - } +# Background has the same colors than foreground, but without the none code. +context["backgrounds"] = copy.copy(eight_color_codes) context["themes"] = {} @@ -90,6 +90,10 @@ context["scale"] = (0,100) context["lexers"] = [] +# Character use as a delimiter +# between foreground and background. +context["groundmark"]="." + class UnknownColor(Exception): pass @@ -492,10 +496,14 @@ def color_lexer( name, style, text ): return "<"+name+">"+ highlight(text, lexer, formatter)[:-1] + "" -def colorin(text, color="red", style="normal"): +def colorin(text, color="red", style="normal", groundmark=context["groundmark"]): """ Return the given text, surrounded by the given color ASCII markers. + The given color may be either a single name, encoding the foreground color, + or a pair of names, delimited by the given groundmark, + encoding foreground and background, e.g. "red.blue". + If the given color is a name that exists in available colors, a 8-colors mode is assumed, else, a 256-colors mode. @@ -521,25 +529,25 @@ def colorin(text, color="red", style="normal"): color_code = "" style_code = "" background_code = "" - list_style_code = [] - + style_codes = [] + # Convert the style code if style == "random" or style == "Random": style = random.choice(list(context["styles"].keys())) else: - styles = style.split(".") + styles = style.split(groundmark) for astyle in styles: if astyle in context["styles"]: - list_style_code.append(str(context["styles"][astyle])) - style_code = ";".join(list_style_code) - - color_background = color.strip().split(".") - color = color_background[0] - background = color_background[1] if len(color_background) == 2 else None + style_codes.append(str(context["styles"][astyle])) + style_code = ";".join(style_codes) + + color_pair = color.strip().split(groundmark) + color = color_pair[0] + background = color_pair[1] if len(color_pair) == 2 else "none" m = mode(color) - if color == "none": + if color == "none" and background == "none": # if no color, style cannot be applied if not debug: return text @@ -594,20 +602,24 @@ def colorin(text, color="red", style="normal"): if background in context["backgrounds"] and m == 8: background_code = endmarks[m] + str(40 + context["backgrounds"][background]) - elif background == None: + elif background == "none": pass else: raise UnknownColor(background) - if color_code is not None: if not debug: return start + style_code + endmarks[m] + color_code + background_code + "m" + text + stop else: - return start + style_code + endmarks[m] + color_code + "m" \ - + "" \ + return start + style_code + endmarks[m] + color_code + background_code + "m" \ + + "" \ + text + "" + stop else: if not debug: @@ -726,8 +738,6 @@ def write(colored, stream = sys.stdout): """ Write "colored" on sys.stdout, then flush. """ - if isinstance(colored, unicode): - colored = colored.encode('utf-8') try: stream.write(colored) stream.flush() @@ -912,6 +922,11 @@ if __name__ == "__main__": ################## # Load resources # ################## + + if debug: + setting = pprint.pformat(context, depth=2) + logging.debug(setting) + try: # Search for available resources files (themes, palettes) # in the same dir as the colout.py script