class documentation

class TerminalController: (source)

Constructor: TerminalController(term_stream)

View In Hierarchy

A class that can be used to portably generate formatted output to a terminal.

TerminalController defines a set of instance variables whose values are initialized to the control sequence necessary to perform a given action. These can be simply included in normal output to the terminal:

>>> term = TerminalController()
>>> print('This is '+term.GREEN+'green'+term.NORMAL)
This is green

Alternatively, the render() method can used, which replaces ${action} with the string required to perform action:

>>> term = TerminalController()
>>> print(term.render('This is ${GREEN}green${NORMAL}'))
This is green

If the terminal doesn't support a given action, then the value of the corresponding instance variable will be set to ''. As a result, the above code will still work on terminals that do not support color, except that their output will not be colored. Also, this means that you can test whether the terminal supports a given action by simply testing the truth value of the corresponding instance variable:

>>> term = TerminalController()
>>> if term.CLEAR_SCREEN:
...     print 'This terminal supports clearning the screen.'
...

Finally, if the width and height of the terminal are known, then they will be stored in the COLS and LINES attributes.

Author
Edward Loper
Method __init__ Create a TerminalController and initialize its attributes with appropriate values for the current terminal. term_stream is the stream that will be used for terminal output; if this stream is not a tty, then the terminal is assumed to be a dumb terminal (i...
Method render Replace each $-substitutions in the given template string with the corresponding terminal control string (if it's defined) or '' (if it's not).
Constant BG_BLACK Undocumented
Constant BG_BLUE Undocumented
Constant BG_CYAN Undocumented
Constant BG_GREEN Undocumented
Constant BG_MAGENTA Undocumented
Constant BG_RED Undocumented
Constant BG_WHITE Undocumented
Constant BG_YELLOW Undocumented
Constant BLACK Undocumented
Constant BLINK Turn on blink mode
Constant BLUE Undocumented
Constant BOL Move the cursor to the beginning of the line
Constant BOLD Turn on bold mode
Constant CLEAR_BOL Clear to the beginning of the line.
Constant CLEAR_EOL Clear to the end of the line.
Constant CLEAR_EOS Clear to the end of the screen
Constant CLEAR_SCREEN Clear the screen and move to home position
Constant CYAN Undocumented
Constant DIM Turn on half-bright mode
Constant DOWN Move the cursor down one line
Constant GREEN Undocumented
Constant HIDE_CURSOR Make the cursor invisible
Constant LEFT Move the cursor left one char
Constant MAGENTA Undocumented
Constant NORMAL Turn off all modes
Constant RED Undocumented
Constant REVERSE Turn on reverse-video mode
Constant RIGHT Move the cursor right one char
Constant SHOW_CURSOR Make the cursor visible
Constant UP Move the cursor up one line
Constant WHITE Undocumented
Constant YELLOW Undocumented
Instance Variable COLS Undocumented
Instance Variable LINES Undocumented
Static Method _tigetstr Rewrites string capabilities to remove "delays" which are not required for modern terminals
Static Method _tparm Undocumented
Method _render_sub Helper function for render
Constant _ANSICOLORS Undocumented
Constant _COLORS Undocumented
Constant _STRING_CAPABILITIES Undocumented
def __init__(self, term_stream=sys.stdout): (source)

Create a TerminalController and initialize its attributes with appropriate values for the current terminal. term_stream is the stream that will be used for terminal output; if this stream is not a tty, then the terminal is assumed to be a dumb terminal (i.e., have no capabilities).

def render(self, template): (source)

Replace each $-substitutions in the given template string with the corresponding terminal control string (if it's defined) or '' (if it's not).

BG_BLACK: str = (source)

Undocumented

Value
''

Undocumented

Value
''

Undocumented

Value
''
BG_GREEN: str = (source)

Undocumented

Value
''
BG_MAGENTA: str = (source)

Undocumented

Value
''

Undocumented

Value
''
BG_WHITE: str = (source)

Undocumented

Value
''
BG_YELLOW: str = (source)

Undocumented

Value
''

Undocumented

Value
''

Turn on blink mode

Value
''

Undocumented

Value
''

Move the cursor to the beginning of the line

Value
''

Turn on bold mode

Value
''
CLEAR_BOL: str = (source)

Clear to the beginning of the line.

Value
''
CLEAR_EOL: str = (source)

Clear to the end of the line.

Value
''
CLEAR_EOS: str = (source)

Clear to the end of the screen

Value
''
CLEAR_SCREEN: str = (source)

Clear the screen and move to home position

Value
''

Undocumented

Value
''

Turn on half-bright mode

Value
''

Move the cursor down one line

Value
''

Undocumented

Value
''
HIDE_CURSOR: str = (source)

Make the cursor invisible

Value
''

Move the cursor left one char

Value
''

Undocumented

Value
''

Turn off all modes

Value
''

Undocumented

Value
''

Turn on reverse-video mode

Value
''

Move the cursor right one char

Value
''
SHOW_CURSOR: str = (source)

Make the cursor visible

Value
''

Move the cursor up one line

Value
''

Undocumented

Value
''

Undocumented

Value
''

Undocumented

Undocumented

def _tigetstr(cap_name): (source)

Rewrites string capabilities to remove "delays" which are not required for modern terminals

def _tparm(cap_name, param): (source)

Undocumented

def _render_sub(self, match): (source)

Helper function for render

_ANSICOLORS = (source)

Undocumented

Value
'BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE'.split()

Undocumented

Value
'BLACK BLUE GREEN CYAN RED MAGENTA YELLOW WHITE'.split()
_STRING_CAPABILITIES = (source)

Undocumented

Value
'\n    BOL=cr UP=cuu1 DOWN=cud1 LEFT=cub1 RIGHT=cuf1\n    CLEAR_SCREEN=clear CLE
AR_EOL=el CLEAR_BOL=el1 CLEAR_EOS=ed BOLD=bold\n    BLINK=blink DIM=dim REVERSE=
rev UNDERLINE=smul NORMAL=sgr0\n    HIDE_CURSOR=cinvis SHOW_CURSOR=cnorm'.split(
)