Package libprs500 :: Module terminfo :: Class TerminalController
[hide private]
[frames] | no frames]

Class TerminalController

source code

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
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}')
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.

Instance Methods [hide private]
  __init__(self, term_stream=sys.stdout)
Create a `TerminalController` and initialize its attributes with appropriate values for the current terminal.
  _tigetstr(self, cap_name)
  render(self, template)
Replace each $-substitutions in the given template string with the corresponding terminal control string (if it's defined) or '' (if it's not).
  _render_sub(self, match)

Class Variables [hide private]
  BOL = ''
Move the cursor to the beginning of the line
  UP = ''
Move the cursor up one line
  DOWN = ''
Move the cursor down one line
  LEFT = ''
Move the cursor left one char
  RIGHT = ''
Move the cursor right one char
  CLEAR_SCREEN = ''
Clear the screen and move to home position
  CLEAR_EOL = ''
Clear to the end of the line.
  CLEAR_BOL = ''
Clear to the beginning of the line.
  CLEAR_EOS = ''
Clear to the end of the screen
  BOLD = ''
Turn on bold mode
  BLINK = ''
Turn on blink mode
  DIM = ''
Turn on half-bright mode
  REVERSE = ''
Turn on reverse-video mode
  NORMAL = ''
Turn off all modes
  HIDE_CURSOR = ''
Make the cursor invisible
  SHOW_CURSOR = ''
Make the cursor visible
  COLS = None
Width of the terminal (None for unknown)
  LINES = None
Height of the terminal (None for unknown)
  WHITE = ''
  YELLOW = ''
  MAGENTA = ''
  RED = ''
  CYAN = ''
  GREEN = ''
  BLUE = ''
  BLACK = ''
  BG_CYAN = ''
  BG_GREEN = ''
  BG_BLUE = ''
  BG_BLACK = ''
  BG_WHITE = ''
  BG_YELLOW = ''
  BG_MAGENTA = ''
  BG_RED = ''
  _STRING_CAPABILITIES = ['BOL=cr', 'UP=cuu1', 'DOWN=cud1', 'LEFT=cub1', 'RIG...
  _COLORS = ['BLACK', 'BLUE', 'GREEN', 'CYAN', 'RED', 'MAGENTA',...
  _ANSICOLORS = ['BLACK', 'RED', 'GREEN', 'YELLOW', 'BLUE', 'MAGENTA...

Method Details [hide private]

__init__(self, term_stream=sys.stdout)
(Constructor)

source code 
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).

_tigetstr(self, cap_name)

source code 
None

render(self, template)

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

_render_sub(self, match)

source code 
None

Class Variable Details [hide private]

BOL

Move the cursor to the beginning of the line
Value:
''                                                                     
      

UP

Move the cursor up one line
Value:
''                                                                     
      

DOWN

Move the cursor down one line
Value:
''                                                                     
      

LEFT

Move the cursor left one char
Value:
''                                                                     
      

RIGHT

Move the cursor right one char
Value:
''                                                                     
      

CLEAR_SCREEN

Clear the screen and move to home position
Value:
''                                                                     
      

CLEAR_EOL

Clear to the end of the line.
Value:
''                                                                     
      

CLEAR_BOL

Clear to the beginning of the line.
Value:
''                                                                     
      

CLEAR_EOS

Clear to the end of the screen
Value:
''                                                                     
      

BOLD

Turn on bold mode
Value:
''                                                                     
      

BLINK

Turn on blink mode
Value:
''                                                                     
      

DIM

Turn on half-bright mode
Value:
''                                                                     
      

REVERSE

Turn on reverse-video mode
Value:
''                                                                     
      

NORMAL

Turn off all modes
Value:
''                                                                     
      

HIDE_CURSOR

Make the cursor invisible
Value:
''                                                                     
      

SHOW_CURSOR

Make the cursor visible
Value:
''                                                                     
      

COLS

Width of the terminal (None for unknown)
Value:
None                                                                  
      

LINES

Height of the terminal (None for unknown)
Value:
None                                                                  
      

WHITE

None
Value:
''                                                                     
      

YELLOW

None
Value:
''                                                                     
      

MAGENTA

None
Value:
''                                                                     
      

RED

None
Value:
''                                                                     
      

CYAN

None
Value:
''                                                                     
      

GREEN

None
Value:
''                                                                     
      

BLUE

None
Value:
''                                                                     
      

BLACK

None
Value:
''                                                                     
      

BG_CYAN

None
Value:
''                                                                     
      

BG_GREEN

None
Value:
''                                                                     
      

BG_BLUE

None
Value:
''                                                                     
      

BG_BLACK

None
Value:
''                                                                     
      

BG_WHITE

None
Value:
''                                                                     
      

BG_YELLOW

None
Value:
''                                                                     
      

BG_MAGENTA

None
Value:
''                                                                     
      

BG_RED

None
Value:
''                                                                     
      

_STRING_CAPABILITIES

None
Value:
['BOL=cr',
 'UP=cuu1',
 'DOWN=cud1',
 'LEFT=cub1',
 'RIGHT=cuf1',
 'CLEAR_SCREEN=clear',
 'CLEAR_EOL=el',
 'CLEAR_BOL=el1',
...                                                                    
      

_COLORS

None
Value:
['BLACK', 'BLUE', 'GREEN', 'CYAN', 'RED', 'MAGENTA', 'YELLOW', 'WHITE'
]                                                                      
      

_ANSICOLORS

None
Value:
['BLACK', 'RED', 'GREEN', 'YELLOW', 'BLUE', 'MAGENTA', 'CYAN', 'WHITE'
]