tea.console Module

tea.console.clear_screen(numlines=100)[source]

Clear the console.

Parameters:numlines (int) – This is an optional argument used only as a fall-back if the operating system console doesn’t have clear screen function.
Return type:None
tea.console.getch()[source]

Cross-platform getch() function.

Same as the getch function from msvcrt library, but works on all platforms. :rtype: str :return: One character got from standard input.

tea.console.color.set_color(fg='normal', bg='normal', fg_dark=False, bg_dark=False, underlined=False)[source]

Set the console color.

>>> set_color(Color.red, Color.blue)
>>> set_color('red', 'blue')
>>> set_color() # returns back to normal
tea.console.color.strip_colors(text)[source]

Helper function used to strip out the color tags so other function can determine the real text line lengths.

Parameters:text (str) – Text to strip color tags from
Return type:str
Returns:Stripped text.
tea.console.color.cprint(text, fg='normal', bg='normal', fg_dark=False, bg_dark=False, underlined=False, parse=False)[source]

Prints string in to stdout using colored font.

See L{set_color} for more details about colors.

Parameters:
  • color (str or list[str]) – If color is a string than this is a color in which the text will appear. If color is a list of strings than all desired colors will be used as a mask (this is used when you wan to set foreground and background color).
  • text (str) – Text that needs to be printed.
tea.console.color.colorize_output(output, colors, indent=0)[source]

Prints output to console using provided color mappings.

Color mapping is dict with regular expressions as key and tuple of two as values. Key is used to match if line should be colorized and tuple contains color to be used and boolean value that indicates if dark foreground is used. For example:

>>> CLS = {
>>>     re.compile(r'^(--- .*)$'): (Color.red, False)
>>> }

will colorize lines that start with ‘—’ to red.

If different parts of line needs to be in different color then dict must be supplied in colors with keys that are named group from regular expression and values that are tuples of color and boolean that indicates if dark foreground is used. For example:

>>> CLS = {
>>>     re.compile(r'^(?P<key>user:\s+)(?P<user>.*)$'): {
>>>         'key': (Color.yellow, True),
>>>         'user': (Color.cyan,   False)
>>>     }
>>> }

will colorize line ‘user: Some user’ so that ‘user:’ part is yellow with dark foreground and ‘Some user’ part is cyan without dark foreground.

tea.console.format.format_page(text)[source]

Formats the text for output adding ASCII frame around the text.

Parameters:text (str) – Text that needs to be formatted.
Return type:string
Returns:Formatted string.
tea.console.format.table(text)[source]

Formats the text as a table

Text in format:

first | second row 2 col 1 | 4

Will be formatted as:

+-------------+--------+
| first       | second |
+-------------+--------+
| row 2 col 1 | 4      |
+-------------+--------+
Parameters:text (str) – Text that needs to be formatted.
Return type:str
Returns:Formatted string.
tea.console.format.hbar(width)[source]

Returns ASCII HBar +---+ with the specified width.

Parameters:width (int) – Width of the central part of the bar.
Return type:str
Returns:ASCII HBar.
tea.console.format.print_page(text)[source]

Formats the text and prints it on stdout.

Text is formatted by adding a ASCII frame around it and coloring the text. Colors can be added to text using color tags, for example:

My [FG_BLUE]blue[NORMAL] text. My [BG_BLUE]blue background[NORMAL] text.
tea.console.format.wrap_text(text, width=80)[source]

Wraps text lines to maximum width characters.

Wrapped text is aligned against the left text border.

Parameters:
  • text (str) – Text to wrap.
  • width (int) – Maximum number of characters per line.
Return type:

str

Returns:

Wrapped text.

tea.console.format.rjust_text(text, width=80, indent=0, subsequent=None)[source]

Same as L{wrap_text} with the difference that the text is aligned against the right text border.

Parameters:
  • text (str) – Text to wrap and align.
  • width (int) – Maximum number of characters per line.
  • indent (int) – Indentation of the first line.
  • subsequent (int or None) – Indentation of all other lines, if it is None, then the indentation will be same as for the first line.
tea.console.format.center_text(text, width=80)[source]

Center all lines of the text.

It is assumed that all lines width is smaller then B{width}, because the line width will not be checked.

Parameters:
  • text (str) – Text to wrap.
  • width (int) – Maximum number of characters per line.
Return type:

str

Returns:

Centered text.

tea.console.utils.clear_screen(numlines=100)[source]

Clear the console.

Parameters:numlines (int) – This is an optional argument used only as a fall-back if the operating system console doesn’t have clear screen function.
Return type:None
tea.console.utils.getch()[source]

Cross-platform getch() function.

Same as the getch function from msvcrt library, but works on all platforms. :rtype: str :return: One character got from standard input.