Package igraph :: Package app :: Module shell :: Class TerminalController
[hide private]
[frames] | no frames]

Class TerminalController

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.


Author: Edward Loper

Instance Methods [hide private]
 
__init__(self, term_stream=<epydoc.docintrospecter._DevNull instance at 0x2c3e40>)
Create a `TerminalController` and initialize its attributes with appropriate values for the current terminal.
 
_render_sub(self, match)
 
_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).
Class Variables [hide private]
  BG_BLACK = ''
  BG_BLUE = ''
  BG_CYAN = ''
  BG_GREEN = ''
  BG_MAGENTA = ''
  BG_RED = ''
  BG_WHITE = ''
  BG_YELLOW = ''
  BLACK = ''
  BLINK = ''
  BLUE = ''
  BOL = ''
  BOLD = ''
  CLEAR_BOL = ''
  CLEAR_EOL = ''
  CLEAR_EOS = ''
  CLEAR_SCREEN = ''
  COLS = None
  CYAN = ''
  DIM = ''
  DOWN = ''
  GREEN = ''
  HIDE_CURSOR = ''
  LEFT = ''
  LINES = None
  MAGENTA = ''
  NORMAL = ''
  RED = ''
  REVERSE = ''
  RIGHT = ''
  SHOW_CURSOR = ''
  UP = ''
  WHITE = ''
  YELLOW = ''
  _ANSICOLORS = ['BLACK', 'RED', 'GREEN', 'YELLOW', 'BLUE', 'MAG...
  _COLORS = ['BLACK', 'BLUE', 'GREEN', 'CYAN', 'RED', 'MAGENTA',...
  _STRING_CAPABILITIES = ['BOL=cr', 'UP=cuu1', 'DOWN=cud1', 'LEF...
Method Details [hide private]

__init__(self, term_stream=<epydoc.docintrospecter._DevNull instance at 0x2c3e40>)
(Constructor)

 

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


Class Variable Details [hide private]

_ANSICOLORS

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

_COLORS

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

_STRING_CAPABILITIES

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