easycolor.parser — Advanced text style


This module defines the ColorParser class which is used to create advnaced text styles.

class easycolor.parser.ColorParser(*args, **kwargs)

A Parser that supports html-like syntax to add advanced style to your text.

The syntax is as follows:

Open tag: <f:color,b:color,o:option,o:option....> All text in the tag will have the configured style applied to it, unless a nested tag overrides it.

Close tag: </f,b,o:option,o:option...> Note: options must be explicitly closed.

cprint(data)

A wrapper around parse, to print text

parse(data)

Parse a string that contains formatting. Automatically appends a RESET at the end. The parser resets everytime this method is called.

Usage

First obtain an instance:

from easycolor.parser import ColorParser
parser = ColorParser()

Then for printing:

cprint = parser.cprint
# Add rainbow colors!
cprint("""<f:red>r<f:yellow>ai<f:green>nb<f:blue>o<f:magenta>w"""
       """</f>o</f>bn</f>ia</f>r</f>""")
# complex foreground - background
cprint("""<f:red,b:yellow>red on yellow</b>just red<b:black>red on black!"""
       """<f:green>green on black</f>again red on black</f,b>normal""")

Parse a format string for a colored logger, for later use:

INFO = parser.parse("[<f:cyan>INFO</f>] <o:bold>{}")
print(INFO.format('Some info..'))