mirror of
https://github.com/evennia/evennia.git
synced 2026-04-03 14:37:17 +02:00
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:
parent
5a2228763f
commit
63329f5420
2 changed files with 53 additions and 45 deletions
|
|
@ -1161,7 +1161,7 @@ class CmdSetAttribute(ObjManipCommand):
|
|||
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.
|
||||
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
|
||||
write proper Python syntax too - notably you must include quotes
|
||||
around your strings or you will get an error.
|
||||
|
|
|
|||
|
|
@ -60,6 +60,9 @@ class ANSIParser(object):
|
|||
"""
|
||||
A class that parses ansi markup
|
||||
to ANSI command sequences
|
||||
|
||||
We also allow to escape colour codes
|
||||
by prepending with a \.
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
|
|
@ -68,29 +71,29 @@ class ANSIParser(object):
|
|||
# MUX-style mappings %cr %cn etc
|
||||
|
||||
self.mux_ansi_map = [
|
||||
(r'%r', ANSI_RETURN),
|
||||
(r'%t', ANSI_TAB),
|
||||
(r'%b', ANSI_SPACE),
|
||||
(r'%cf', ANSI_BLINK),
|
||||
(r'%ci', ANSI_INVERSE),
|
||||
(r'%ch', ANSI_HILITE),
|
||||
(r'%cn', ANSI_NORMAL),
|
||||
(r'%cx', ANSI_BLACK),
|
||||
(r'%cX', ANSI_BACK_BLACK),
|
||||
(r'%cr', ANSI_RED),
|
||||
(r'%cR', ANSI_BACK_RED),
|
||||
(r'%cg', ANSI_GREEN),
|
||||
(r'%cG', ANSI_BACK_GREEN),
|
||||
(r'%cy', ANSI_YELLOW),
|
||||
(r'%cY', ANSI_BACK_YELLOW),
|
||||
(r'%cb', ANSI_BLUE),
|
||||
(r'%cB', ANSI_BACK_BLUE),
|
||||
(r'%cm', ANSI_MAGENTA),
|
||||
(r'%cM', ANSI_BACK_MAGENTA),
|
||||
(r'%cc', ANSI_CYAN),
|
||||
(r'%cC', ANSI_BACK_CYAN),
|
||||
(r'%cw', ANSI_WHITE),
|
||||
(r'%cW', ANSI_BACK_WHITE),
|
||||
(r'(?<!\\)%r', ANSI_RETURN),
|
||||
(r'(?<!\\)%t', ANSI_TAB),
|
||||
(r'(?<!\\)%b', ANSI_SPACE),
|
||||
(r'(?<!\\)%cf', ANSI_BLINK),
|
||||
(r'(?<!\\)%ci', ANSI_INVERSE),
|
||||
(r'(?<!\\)%ch', ANSI_HILITE),
|
||||
(r'(?<!\\)%cn', ANSI_NORMAL),
|
||||
(r'(?<!\\)%cx', ANSI_BLACK),
|
||||
(r'(?<!\\)%cX', ANSI_BACK_BLACK),
|
||||
(r'(?<!\\)%cr', ANSI_RED),
|
||||
(r'(?<!\\)%cR', ANSI_BACK_RED),
|
||||
(r'(?<!\\)%cg', ANSI_GREEN),
|
||||
(r'(?<!\\)%cG', ANSI_BACK_GREEN),
|
||||
(r'(?<!\\)%cy', ANSI_YELLOW),
|
||||
(r'(?<!\\)%cY', ANSI_BACK_YELLOW),
|
||||
(r'(?<!\\)%cb', ANSI_BLUE),
|
||||
(r'(?<!\\)%cB', ANSI_BACK_BLUE),
|
||||
(r'(?<!\\)%cm', ANSI_MAGENTA),
|
||||
(r'(?<!\\)%cM', ANSI_BACK_MAGENTA),
|
||||
(r'(?<!\\)%cc', ANSI_CYAN),
|
||||
(r'(?<!\\)%cC', ANSI_BACK_CYAN),
|
||||
(r'(?<!\\)%cw', ANSI_WHITE),
|
||||
(r'(?<!\\)%cW', ANSI_BACK_WHITE),
|
||||
]
|
||||
|
||||
# Expanded mapping {r {n etc
|
||||
|
|
@ -98,32 +101,32 @@ class ANSIParser(object):
|
|||
hilite = ANSI_HILITE
|
||||
normal = ANSI_NORMAL
|
||||
self.ext_ansi_map = [
|
||||
(r'{r', hilite + ANSI_RED),
|
||||
(r'{R', normal + ANSI_RED),
|
||||
(r'{g', hilite + ANSI_GREEN),
|
||||
(r'{G', normal + ANSI_GREEN),
|
||||
(r'{y', hilite + ANSI_YELLOW),
|
||||
(r'{Y', normal + ANSI_YELLOW),
|
||||
(r'{b', hilite + ANSI_BLUE),
|
||||
(r'{B', normal + ANSI_BLUE),
|
||||
(r'{m', hilite + ANSI_MAGENTA),
|
||||
(r'{M', normal + ANSI_MAGENTA),
|
||||
(r'{c', hilite + ANSI_CYAN),
|
||||
(r'{C', normal + ANSI_CYAN),
|
||||
(r'{w', hilite + ANSI_WHITE), # pure white
|
||||
(r'{W', normal + ANSI_WHITE), #light grey
|
||||
(r'{x', hilite + ANSI_BLACK), #dark grey
|
||||
(r'{X', normal + ANSI_BLACK), #pure black
|
||||
(r'{n', normal) #reset
|
||||
(r'(?<!\\){r', hilite + ANSI_RED),
|
||||
(r'(?<!\\){R', normal + ANSI_RED),
|
||||
(r'(?<!\\){g', hilite + ANSI_GREEN),
|
||||
(r'(?<!\\){G', normal + ANSI_GREEN),
|
||||
(r'(?<!\\){y', hilite + ANSI_YELLOW),
|
||||
(r'(?<!\\){Y', normal + ANSI_YELLOW),
|
||||
(r'(?<!\\){b', hilite + ANSI_BLUE),
|
||||
(r'(?<!\\){B', normal + ANSI_BLUE),
|
||||
(r'(?<!\\){m', hilite + ANSI_MAGENTA),
|
||||
(r'(?<!\\){M', normal + ANSI_MAGENTA),
|
||||
(r'(?<!\\){c', hilite + ANSI_CYAN),
|
||||
(r'(?<!\\){C', normal + ANSI_CYAN),
|
||||
(r'(?<!\\){w', hilite + ANSI_WHITE), # pure white
|
||||
(r'(?<!\\){W', normal + ANSI_WHITE), #light grey
|
||||
(r'(?<!\\){x', hilite + ANSI_BLACK), #dark grey
|
||||
(r'(?<!\\){X', normal + ANSI_BLACK), #pure black
|
||||
(r'(?<!\\){n', normal) #reset
|
||||
]
|
||||
|
||||
# xterm256 {123, %c134,
|
||||
|
||||
self.xterm256_map = [
|
||||
(r'%c([1-5]{3})', self.parse_rgb), # %c123 - foreground colour
|
||||
(r'%c(b[1-5]{3})', self.parse_rgb), # %cb123 - background colour
|
||||
(r'{([1-5]{3})', self.parse_rgb), # {123 - foreground colour
|
||||
(r'{(b[1-5]{3})', self.parse_rgb) # {b123 - background 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'(?<!\\){([1-5]{3})', self.parse_rgb), # {123 - foreground colour
|
||||
(r'(?<!\\){(b[1-5]{3})', self.parse_rgb) # {b123 - background colour
|
||||
]
|
||||
|
||||
# obs - order matters here, we want to do the xterms first since
|
||||
|
|
@ -213,6 +216,11 @@ class ANSIParser(object):
|
|||
if strip_ansi:
|
||||
# remove all ANSI escape codes
|
||||
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
|
||||
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue