Module termcolors :: Class TermColors
[hide private]
[frames] | no frames]

Class TermColors

source code


Base class for terminal colorization.

Use getDefault for get default terminal colorization class for you OS.

Instance Methods [hide private]
 
__init__(self, stream=None)
stream - Stream for out colors.
source code
 
_calcColor(self, text_color, text_bright, bg_color, bg_bright)
Calculate color value.
source code
 
_setColor(self, value)
Set specified color from value.
source code
 
_calcResetColor(self)
Calculate value for reset colors to default.
source code
 
set_reset_color(self)
Save specified color to use for reset.
source code
 
black(self)
Set black color.
source code
 
blue(self)
Set blue color.
source code
 
green(self)
Set green color.
source code
 
cyan(self)
Set cyan color.
source code
 
red(self)
Set red color.
source code
 
magenta(self)
Set magenta color.
source code
 
yellow(self)
Set yellow color.
source code
 
white(self)
Set white color.
source code
 
bright(self)
Set bright color.
source code
 
dark(self)
Set dark color.
source code
 
bg(self)
Go to enter background color.
source code
 
txt(self)
Go to enter text color(this is default).
source code
 
set(self)
Set (apply) all specified colors.
source code
 
_decode_color(self)
Decode color to value from specified colors.
source code
 
_clear(self, save_last=True)
Clear specified colors to None and save to last (if not None).
source code
 
reset(self)
Try reset colors to default.
source code
 
write(self, s, skip=False, stream=None)
Colorized ouput string to stream.
source code
 
writeln(self, s='', skip=False, stream=None)
As write but add new line at end.
source code

Inherited from object: __delattr__, __format__, __getattribute__, __hash__, __new__, __reduce__, __reduce_ex__, __repr__, __setattr__, __sizeof__, __str__, __subclasshook__

Class Methods [hide private]
 
skipTags(cls, s)
Remove colorize tags from string.
source code
Static Methods [hide private]
 
getDefault(stream=None)
Return TermColors instance with default color scheme (initialized for current terminal).
source code
Class Variables [hide private]
  C_BLACK = 0
  C_BLUE = 1
  C_GREEN = 2
  C_CYAN = 3
  C_RED = 4
  C_MAGENTA = 5
  C_YELLOW = 6
  C_WHITE = 7
  C_RE_PATT = '<([rgbkcmyw_RGBKCMYW]{1,2})>((.|\n)*?)</\\1>'
Pattern for search tags.[1]-color string, [2]-string for colorize.
Instance Variables [hide private]
  _stream
stream for out colors.
  _color_table
Color table.
  _isbg
If true then now set bg colors.
  _txtbright
Use text bright.
  _bgbright
Use text bright.
  _ctxt
Text color.
  _cbg
Background color.
  _last_ctxt
Previous text color.
  _last_cbg
Previous bg color.
  _last_bgbright
Previous bg brightness.
  _last_txtbright
Previous text brightness.
  _reset_value
Color color value or None for use default.
  _recomp
Compiled pattern for RE.
  _tag_table
Table for match colr strings to color index.
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, stream=None)
(Constructor)

source code 

stream - Stream for out colors. In windows not used.

Default is None = use sys.stdout.

Overrides: object.__init__

_calcColor(self, text_color, text_bright, bg_color, bg_bright)

source code 

Calculate color value.

text_color - Text color, use class color constant C_xxxxx.
If color argument is None use last value or not use this component if no last. or use default value. See implementation detail.
bg_color - Background color, use class color constant C_xxxxx.
If color argument is None use last value or not use this component if no last. or use default value. See implementation detail.
text_bright - Text brightness,``True`` for bright, False for dark color.
If color argument is None use last value or not use this component if no last. or use default value. See implementation detail.
bg_bright - Background brightness,``True`` for bright, False for dark color.
If color argument is None use last value or not use this component if no last. or use default value. See implementation detail.

Return value for set color. Returned value must be right for use without check.

_setColor(self, value)

source code 

Set specified color from value.

Value must be right. (Not check more).

black(self)

source code 
Set black color. Return self instance.

blue(self)

source code 
Set blue color. Return self instance.

green(self)

source code 
Set green color. Return self instance.

cyan(self)

source code 
Set cyan color. Return self instance.

red(self)

source code 
Set red color. Return self instance.

magenta(self)

source code 
Set magenta color. Return self instance.

yellow(self)

source code 
Set yellow color. Return self instance.

white(self)

source code 
Set white color. Return self instance.

bright(self)

source code 
Set bright color. Return self instance.

dark(self)

source code 
Set dark color. Return self instance.

bg(self)

source code 
Go to enter background color. Return self instance.

txt(self)

source code 
Go to enter text color(this is default). Return self instance.

_clear(self, save_last=True)

source code 

Clear specified colors to None and save to last (if not None).

save_last - if true? then save to last value.

write(self, s, skip=False, stream=None)

source code 

Colorized ouput string to stream.

If stream None then use initialized self._stream, otherwise use sys.stdout.

skip - If True then remove color tags from string and out it without colorization.

Sintax is next (html-like): <CC>some text</CC>, where first C is a text color, second C is a background color.

color is next code:

r - red, g - green, b - blue, k - black, c - cyan, m - magenta, y - yellow, w - white, _ - not use (use previous or default).

Lowcase letter mean dark color, Uppercase mean bright color.

For example:

<R_>Bright red on default background.</R_>

<wk>Dark white on black</wk>


Instance Variable Details [hide private]

_color_table

Color table. Index is a color constants from class, C_xxxx.

_txtbright

Use text bright. If None - use last or default.

_bgbright

Use text bright. If None - use last or default.

_ctxt

Text color. If None - use last or default.

_cbg

Background color. If None - use last or default.

_tag_table

Table for match colr strings to color index. Store 2tuple: (color,bright).