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.
This commit is contained in:
parent
b284020355
commit
d06b1b449a
1 changed files with 49 additions and 34 deletions
|
|
@ -18,6 +18,8 @@ import string
|
||||||
import hashlib
|
import hashlib
|
||||||
import functools
|
import functools
|
||||||
import argparse
|
import argparse
|
||||||
|
import pprint
|
||||||
|
import copy
|
||||||
|
|
||||||
# set the SIGPIPE handler to kill the program instead of
|
# set the SIGPIPE handler to kill the program instead of
|
||||||
# ending in a write error when a broken pipe occurs
|
# ending in a write error when a broken pipe occurs
|
||||||
|
|
@ -38,23 +40,21 @@ context["styles"] = {
|
||||||
"reverse": 7, "conceal": 8
|
"reverse": 7, "conceal": 8
|
||||||
}
|
}
|
||||||
|
|
||||||
# Available color names in 8-colors mode
|
# Available color names in 8-colors mode.
|
||||||
context["colors"] = {
|
eight_colors = ["black","red","green","yellow","blue","magenta","cyan","white"]
|
||||||
"black": 0, "red": 1, "green": 2, "yellow": 3, "orange":3, "blue": 4,
|
# Given in that order, the ASCII code is the index.
|
||||||
"magenta": 5, "purple": 5, "cyan": 6, "white": 7, "none": -1
|
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"] = {
|
# Background has the same colors than foreground, but without the none code.
|
||||||
"black": 0,
|
context["backgrounds"] = copy.copy(eight_color_codes)
|
||||||
"red": 1,
|
|
||||||
"green": 2,
|
|
||||||
"yellow": 3,
|
|
||||||
"blue": 4,
|
|
||||||
"magenta": 5,
|
|
||||||
"cyan": 6,
|
|
||||||
"white": 7
|
|
||||||
}
|
|
||||||
|
|
||||||
context["themes"] = {}
|
context["themes"] = {}
|
||||||
|
|
||||||
|
|
@ -90,6 +90,10 @@ context["scale"] = (0,100)
|
||||||
|
|
||||||
context["lexers"] = []
|
context["lexers"] = []
|
||||||
|
|
||||||
|
# Character use as a delimiter
|
||||||
|
# between foreground and background.
|
||||||
|
context["groundmark"]="."
|
||||||
|
|
||||||
class UnknownColor(Exception):
|
class UnknownColor(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
@ -492,10 +496,14 @@ def color_lexer( name, style, text ):
|
||||||
return "<"+name+">"+ highlight(text, lexer, formatter)[:-1] + "</"+name+">"
|
return "<"+name+">"+ highlight(text, lexer, formatter)[:-1] + "</"+name+">"
|
||||||
|
|
||||||
|
|
||||||
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.
|
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,
|
If the given color is a name that exists in available colors,
|
||||||
a 8-colors mode is assumed, else, a 256-colors mode.
|
a 8-colors mode is assumed, else, a 256-colors mode.
|
||||||
|
|
||||||
|
|
@ -521,25 +529,25 @@ def colorin(text, color="red", style="normal"):
|
||||||
color_code = ""
|
color_code = ""
|
||||||
style_code = ""
|
style_code = ""
|
||||||
background_code = ""
|
background_code = ""
|
||||||
list_style_code = []
|
style_codes = []
|
||||||
|
|
||||||
# Convert the style code
|
# Convert the style code
|
||||||
if style == "random" or style == "Random":
|
if style == "random" or style == "Random":
|
||||||
style = random.choice(list(context["styles"].keys()))
|
style = random.choice(list(context["styles"].keys()))
|
||||||
else:
|
else:
|
||||||
styles = style.split(".")
|
styles = style.split(groundmark)
|
||||||
for astyle in styles:
|
for astyle in styles:
|
||||||
if astyle in context["styles"]:
|
if astyle in context["styles"]:
|
||||||
list_style_code.append(str(context["styles"][astyle]))
|
style_codes.append(str(context["styles"][astyle]))
|
||||||
style_code = ";".join(list_style_code)
|
style_code = ";".join(style_codes)
|
||||||
|
|
||||||
color_background = color.strip().split(".")
|
color_pair = color.strip().split(groundmark)
|
||||||
color = color_background[0]
|
color = color_pair[0]
|
||||||
background = color_background[1] if len(color_background) == 2 else None
|
background = color_pair[1] if len(color_pair) == 2 else "none"
|
||||||
|
|
||||||
m = mode(color)
|
m = mode(color)
|
||||||
|
|
||||||
if color == "none":
|
if color == "none" and background == "none":
|
||||||
# if no color, style cannot be applied
|
# if no color, style cannot be applied
|
||||||
if not debug:
|
if not debug:
|
||||||
return text
|
return text
|
||||||
|
|
@ -594,20 +602,24 @@ def colorin(text, color="red", style="normal"):
|
||||||
|
|
||||||
if background in context["backgrounds"] and m == 8:
|
if background in context["backgrounds"] and m == 8:
|
||||||
background_code = endmarks[m] + str(40 + context["backgrounds"][background])
|
background_code = endmarks[m] + str(40 + context["backgrounds"][background])
|
||||||
elif background == None:
|
elif background == "none":
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
raise UnknownColor(background)
|
raise UnknownColor(background)
|
||||||
|
|
||||||
|
|
||||||
if color_code is not None:
|
if color_code is not None:
|
||||||
if not debug:
|
if not debug:
|
||||||
return start + style_code + endmarks[m] + color_code + background_code + "m" + text + stop
|
return start + style_code + endmarks[m] + color_code + background_code + "m" + text + stop
|
||||||
else:
|
else:
|
||||||
return start + style_code + endmarks[m] + color_code + "m" \
|
return start + style_code + endmarks[m] + color_code + background_code + "m" \
|
||||||
+ "<color name=" + str(color) + " code=" + color_code \
|
+ "<color name=" + str(color) \
|
||||||
+ " style=" + str(style) + " stylecode=" + style_code \
|
+ " code=" + color_code \
|
||||||
+ " mode=" + str(m) + ">" \
|
+ " style=" + str(style) \
|
||||||
|
+ " stylecode=" + style_code \
|
||||||
|
+ " background=" + str(background) \
|
||||||
|
+ " backgroundcode=" + background_code.strip(endmarks[m]) \
|
||||||
|
+ " mode=" + str(m) \
|
||||||
|
+ ">" \
|
||||||
+ text + "</color>" + stop
|
+ text + "</color>" + stop
|
||||||
else:
|
else:
|
||||||
if not debug:
|
if not debug:
|
||||||
|
|
@ -726,8 +738,6 @@ def write(colored, stream = sys.stdout):
|
||||||
"""
|
"""
|
||||||
Write "colored" on sys.stdout, then flush.
|
Write "colored" on sys.stdout, then flush.
|
||||||
"""
|
"""
|
||||||
if isinstance(colored, unicode):
|
|
||||||
colored = colored.encode('utf-8')
|
|
||||||
try:
|
try:
|
||||||
stream.write(colored)
|
stream.write(colored)
|
||||||
stream.flush()
|
stream.flush()
|
||||||
|
|
@ -912,6 +922,11 @@ if __name__ == "__main__":
|
||||||
##################
|
##################
|
||||||
# Load resources #
|
# Load resources #
|
||||||
##################
|
##################
|
||||||
|
|
||||||
|
if debug:
|
||||||
|
setting = pprint.pformat(context, depth=2)
|
||||||
|
logging.debug(setting)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Search for available resources files (themes, palettes)
|
# Search for available resources files (themes, palettes)
|
||||||
# in the same dir as the colout.py script
|
# in the same dir as the colout.py script
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue