Produce colored terminal text with an xml-like markup
pip install ansimarkupAnsimarkup
Ansimarkup is an XML-like markup for producing colored terminal text.
from ansimarkup import ansiprint as print
print("<b>bold text</b>"))
print("<red>red text</red>", "<red,green>red text on a green background</red,green>")
print("<fg #ffaf00>orange text</fg #ffaf00>")
Installation
The latest stable version of ansimarkup can be installed from PyPi:
python3 -m pip install ansimarkup
Usage
Basic
from ansimarkup import parse, ansiprint
# parse() converts the tags to the corresponding ansi escape sequence.
parse("<b>bold</b> <d>dim</d>")
# ansiprint() works exactly like print(), but first runs parse() on all arguments.
ansiprint("<b>bold</b>", "<d>dim</d>")
ansiprint("<b>bold</b>", "<d>dim</d>", sep=":", file=sys.stderr)
Colors and styles
# Colors may be specified in one of several ways.
parse("<red>red foreground</red>")
parse("<RED>red background</RED>")
parse("<fg red>red foreground</fg red>")
parse("<bg red>red background</bg red>")
# Xterm, hex and rgb colors are accepted by the <fg> and <bg> tags.
parse("<fg 86>aquamarine foreground</fg 86>")
parse("<bg #00005f>dark blue background</bg #00005f>")
parse("<fg 0,95,0>dark green foreground</fg 0,95,0>")
# Tags may be nested.
parse("<r><Y>red text on a yellow foreground</Y></r>")
# The above may be more concisely written as:
parse("<r,y>red text on a yellow background</r,y>")
# This shorthand also supports style tags.
parse("<b,r,y>bold red text on a yellow background</b,r,y>")
parse("<b,r,>bold red text</b,r,>")
parse("<b,,y>bold regular text on a yellow background</b,,y>")
# Unrecognized tags are left as-is.
parse("<b><element1></element1></b>")
For a list of markup tags, please refer to tags.py.
User-defined tags
Custom tags or overrides for existing tags may be defined by creating a new instance: