easycolor.style - Simple text style


This module defines the TextStyle class which is the basis for creating text styles. It also provides helper functions for easier usage.

easycolor.style.RESET

Reset all style.


class easycolor.style.TextStyle(fg=None, bg=None, opt=None, **kwargs)

Create a text style The keyword arguments fg, bg, opt can be used to initiallize the style. The foreground and background colors can be then changed and options can be added later.

Note: The bg, fg, opt methods return the style instance, to enable method chaining.

Valid colors:
‘black’, ‘red’, ‘green’, ‘yellow’, ‘blue’, ‘magenta’, ‘cyan’, ‘white’.
Valid options:
‘bold’, ‘faint’, ‘italic’, ‘undeline’, ‘blink’, ‘blink_fast’, ‘negative’, ‘conceal’, ‘strikeout’.
bg(value)

Add a background color.

compile()

Get the compiled ANSI espace sequence as a string.

fg(value)

Add a foreground color.

opt(value)

Add (an) option(s). value can be an itterable or a single string.


easycolor.style.make_style(fg=None, bg=None, opt=())

The keyword arguments fg, bg, opt can be used to define the style.


easycolor.style.wrap(text, fg=None, bg=None, opt=())

The keyword arguments fg, bg, opt can be used to define the style, appends reset after the text.

Usage

To wrap text, the wrap() function must be imported.

from easycolor.style import wrap as _w

The text is then wrapped as easy as:

# wrap text, appends a style-reset
print(_w('yellow on red', fg='yellow', bg='red'))
print('normal text')

To compile a style, the make_style() function is used:

from easycolor.style import make_style, RESET

Example:

# define some styles
black_on_white = make_style(fg='black', bg='white')
# we have to reset the background here
red_bold = make_style(fg='red', bg='reset', opt='bold')
# An options reset is only possible with a total reset
# we can however negate an option to remove it('!option').
green_bold_underline = make_style(fg='green', opt=('!bold', 'underline'))

print(black_on_white + 'Black on white' + red_bold + 'Red bold' +
      green_bold_underline + 'Green not bold underline' + RESET)

Note: For advanced formatting use the easycolor.parser module.