Added the ability to escape colour codes with \, So using \{g in a text will now result in '{g' being printed instead of the text switching to bright green. This can be useful for documentation.

This commit is contained in:
Griatch 2012-04-15 23:42:57 +02:00
parent 5a2228763f
commit 63329f5420
2 changed files with 53 additions and 45 deletions

View file

@ -1161,7 +1161,7 @@ class CmdSetAttribute(ObjManipCommand):
the functionality of certain custom objects). This is indicated the functionality of certain custom objects). This is indicated
by you starting your value with one of {c'{n, {c"{n, {c({n, {c[{n or {c{ {n. by you starting your value with one of {c'{n, {c"{n, {c({n, {c[{n or {c{ {n.
Note that you should leave a space after starting a dictionary ('{ ') Note that you should leave a space after starting a dictionary ('{ ')
so as to not confuse the dictionary start with a colour code. so as to not confuse the dictionary start with a colour code like \{g.
Remember that if you use Python primitives like this, you must Remember that if you use Python primitives like this, you must
write proper Python syntax too - notably you must include quotes write proper Python syntax too - notably you must include quotes
around your strings or you will get an error. around your strings or you will get an error.

View file

@ -60,6 +60,9 @@ class ANSIParser(object):
""" """
A class that parses ansi markup A class that parses ansi markup
to ANSI command sequences to ANSI command sequences
We also allow to escape colour codes
by prepending with a \.
""" """
def __init__(self): def __init__(self):
@ -68,29 +71,29 @@ class ANSIParser(object):
# MUX-style mappings %cr %cn etc # MUX-style mappings %cr %cn etc
self.mux_ansi_map = [ self.mux_ansi_map = [
(r'%r', ANSI_RETURN), (r'(?<!\\)%r', ANSI_RETURN),
(r'%t', ANSI_TAB), (r'(?<!\\)%t', ANSI_TAB),
(r'%b', ANSI_SPACE), (r'(?<!\\)%b', ANSI_SPACE),
(r'%cf', ANSI_BLINK), (r'(?<!\\)%cf', ANSI_BLINK),
(r'%ci', ANSI_INVERSE), (r'(?<!\\)%ci', ANSI_INVERSE),
(r'%ch', ANSI_HILITE), (r'(?<!\\)%ch', ANSI_HILITE),
(r'%cn', ANSI_NORMAL), (r'(?<!\\)%cn', ANSI_NORMAL),
(r'%cx', ANSI_BLACK), (r'(?<!\\)%cx', ANSI_BLACK),
(r'%cX', ANSI_BACK_BLACK), (r'(?<!\\)%cX', ANSI_BACK_BLACK),
(r'%cr', ANSI_RED), (r'(?<!\\)%cr', ANSI_RED),
(r'%cR', ANSI_BACK_RED), (r'(?<!\\)%cR', ANSI_BACK_RED),
(r'%cg', ANSI_GREEN), (r'(?<!\\)%cg', ANSI_GREEN),
(r'%cG', ANSI_BACK_GREEN), (r'(?<!\\)%cG', ANSI_BACK_GREEN),
(r'%cy', ANSI_YELLOW), (r'(?<!\\)%cy', ANSI_YELLOW),
(r'%cY', ANSI_BACK_YELLOW), (r'(?<!\\)%cY', ANSI_BACK_YELLOW),
(r'%cb', ANSI_BLUE), (r'(?<!\\)%cb', ANSI_BLUE),
(r'%cB', ANSI_BACK_BLUE), (r'(?<!\\)%cB', ANSI_BACK_BLUE),
(r'%cm', ANSI_MAGENTA), (r'(?<!\\)%cm', ANSI_MAGENTA),
(r'%cM', ANSI_BACK_MAGENTA), (r'(?<!\\)%cM', ANSI_BACK_MAGENTA),
(r'%cc', ANSI_CYAN), (r'(?<!\\)%cc', ANSI_CYAN),
(r'%cC', ANSI_BACK_CYAN), (r'(?<!\\)%cC', ANSI_BACK_CYAN),
(r'%cw', ANSI_WHITE), (r'(?<!\\)%cw', ANSI_WHITE),
(r'%cW', ANSI_BACK_WHITE), (r'(?<!\\)%cW', ANSI_BACK_WHITE),
] ]
# Expanded mapping {r {n etc # Expanded mapping {r {n etc
@ -98,32 +101,32 @@ class ANSIParser(object):
hilite = ANSI_HILITE hilite = ANSI_HILITE
normal = ANSI_NORMAL normal = ANSI_NORMAL
self.ext_ansi_map = [ self.ext_ansi_map = [
(r'{r', hilite + ANSI_RED), (r'(?<!\\){r', hilite + ANSI_RED),
(r'{R', normal + ANSI_RED), (r'(?<!\\){R', normal + ANSI_RED),
(r'{g', hilite + ANSI_GREEN), (r'(?<!\\){g', hilite + ANSI_GREEN),
(r'{G', normal + ANSI_GREEN), (r'(?<!\\){G', normal + ANSI_GREEN),
(r'{y', hilite + ANSI_YELLOW), (r'(?<!\\){y', hilite + ANSI_YELLOW),
(r'{Y', normal + ANSI_YELLOW), (r'(?<!\\){Y', normal + ANSI_YELLOW),
(r'{b', hilite + ANSI_BLUE), (r'(?<!\\){b', hilite + ANSI_BLUE),
(r'{B', normal + ANSI_BLUE), (r'(?<!\\){B', normal + ANSI_BLUE),
(r'{m', hilite + ANSI_MAGENTA), (r'(?<!\\){m', hilite + ANSI_MAGENTA),
(r'{M', normal + ANSI_MAGENTA), (r'(?<!\\){M', normal + ANSI_MAGENTA),
(r'{c', hilite + ANSI_CYAN), (r'(?<!\\){c', hilite + ANSI_CYAN),
(r'{C', normal + ANSI_CYAN), (r'(?<!\\){C', normal + ANSI_CYAN),
(r'{w', hilite + ANSI_WHITE), # pure white (r'(?<!\\){w', hilite + ANSI_WHITE), # pure white
(r'{W', normal + ANSI_WHITE), #light grey (r'(?<!\\){W', normal + ANSI_WHITE), #light grey
(r'{x', hilite + ANSI_BLACK), #dark grey (r'(?<!\\){x', hilite + ANSI_BLACK), #dark grey
(r'{X', normal + ANSI_BLACK), #pure black (r'(?<!\\){X', normal + ANSI_BLACK), #pure black
(r'{n', normal) #reset (r'(?<!\\){n', normal) #reset
] ]
# xterm256 {123, %c134, # xterm256 {123, %c134,
self.xterm256_map = [ self.xterm256_map = [
(r'%c([1-5]{3})', self.parse_rgb), # %c123 - foreground colour (r'(?<!\\)%c([1-5]{3})', self.parse_rgb), # %c123 - foreground colour
(r'%c(b[1-5]{3})', self.parse_rgb), # %cb123 - background colour (r'(?<!\\)%c(b[1-5]{3})', self.parse_rgb), # %cb123 - background colour
(r'{([1-5]{3})', self.parse_rgb), # {123 - foreground colour (r'(?<!\\){([1-5]{3})', self.parse_rgb), # {123 - foreground colour
(r'{(b[1-5]{3})', self.parse_rgb) # {b123 - background colour (r'(?<!\\){(b[1-5]{3})', self.parse_rgb) # {b123 - background colour
] ]
# obs - order matters here, we want to do the xterms first since # obs - order matters here, we want to do the xterms first since
@ -213,6 +216,11 @@ class ANSIParser(object):
if strip_ansi: if strip_ansi:
# remove all ANSI escape codes # remove all ANSI escape codes
string = self.ansi_regex.sub("", string) string = self.ansi_regex.sub("", string)
# strip the \ in front of escaped colour codes, like \{r.
string = re.sub(r"\\{", "{", string)
string = re.sub(r"\\%r", "%r", string)
string = re.sub(r"\\%b", "%b", string)
string = re.sub(r"\\%c", "%c", string)
return string return string