Package configshell :: Module console :: Class Console
[hide private]
[frames] | no frames]

Class Console

object --+
         |
        Console

Implements various utility methods providing a console UI support toolkit, most notably an epytext-to-console text renderer using ANSI escape sequences. It uses the Borg pattern to share state between instances.

Instance Methods [hide private]
 
__init__(self, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
Initializes a Console instance.
 
escape(self, sequence, reply_terminator=None)
Sends an escape sequence to the console, and reads the reply terminated by reply_terminator.
 
get_width(self)
Returns the console width, or maximum width if we are not a terminal device.
 
get_cursor_xy(self)
Get the current text cursor x, y coordinates.
 
set_cursor_xy(self, xpos, ypos)
Set the cursor x, y coordinates.
 
raw_write(self, text, output=sys.stdout)
Raw console printing function.
 
display(self, text, no_lf=False, error=False)
Display a text with a default style.
 
epy_write(self, text)
Renders and print and epytext-formatted text on the console.
 
indent(self, text, margin=2)
Indents text by margin space.
 
dedent(self, text)
A convenience function to easily write multiline text blocks that will be later assembled in to a unique epytext string.
 
render_text(self, text, fgcolor=None, bgcolor=None, styles=None, open_end=False, todefault=False)
Renders some text with ANSI console colors and attributes.
str
wordwrap(self, text, indent=0, startindex=0, splitchars='')
Word-wrap the given string.
string
render_domtree(self, tree, indent=0, seclevel=0)
Convert a DOM document encoding epytext to an 8-bits ascii string with ANSI formating for simpler styles.

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

Class Variables [hide private]
  _max_width = 132
  _escape = '\033['
  _ansi_format = _escape+ '%dm%s'
  _ansi_reset = _escape+ '0m'
  _re_ansi_seq = re.compile('(\033\[..?m)')
  _ansi_styles = {'bold': 1, 'underline': 4, 'blink': 5, 'revers...
  colors = ['black', 'red', 'green', 'yellow', 'blue', 'magenta'...
  _ansi_fgcolors = dict(zip(colors, range(30, 38)))
  _ansi_bgcolors = dict(zip(colors, range(40, 48)))
  __borg_state = {}
Properties [hide private]

Inherited from object: __class__

Method Details [hide private]

__init__(self, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
(Constructor)

 

Initializes a Console instance.

Parameters:
  • stdin (file object) - The console standard input.
  • stdout (file object) - The console standard output.
Overrides: object.__init__

escape(self, sequence, reply_terminator=None)

 

Sends an escape sequence to the console, and reads the reply terminated by reply_terminator. If reply_terminator is not specified, the reply will not be read.

Parameters:
  • reply_terminator (str) - The expected end-of-reply marker.
  • sequence (str)

set_cursor_xy(self, xpos, ypos)

 

Set the cursor x, y coordinates.

Parameters:
  • xpos (int) - The x coordinate of the cursor.
  • ypos (int) - The y coordinate of the cursor.

raw_write(self, text, output=sys.stdout)

 

Raw console printing function.

Parameters:
  • text (str) - The text to print.

display(self, text, no_lf=False, error=False)

 

Display a text with a default style.

Parameters:
  • text (str) - Text to display
  • no_lf (bool) - Do not display a line feed.

indent(self, text, margin=2)

 

Indents text by margin space.

Parameters:
  • text (str) - The text to be indented.

dedent(self, text)

 

A convenience function to easily write multiline text blocks that will be later assembled in to a unique epytext string. It removes heading newline chars and common indentation.

render_text(self, text, fgcolor=None, bgcolor=None, styles=None, open_end=False, todefault=False)

 

Renders some text with ANSI console colors and attributes.

Parameters:
  • fgcolor (str) - ANSI color to use for text: black, red, green, yellow, blue, magenta. cyan. white
  • bgcolor (str) - ANSI color to use for background: black, red, green, yellow, blue, magenta. cyan. white
  • styles (list of str) - List of ANSI styles to use: bold, underline, blink, reverse, concealed
  • open_end (bool) - Do not reset text style at the end ot the output.
  • todefault (bool) - Instead of resetting style at the end of the output, reset to default color. Only if not open_end.

wordwrap(self, text, indent=0, startindex=0, splitchars='')

 

Word-wrap the given string. I.e., add newlines to the string such that any lines that are longer than terminal width or max_width are broken into shorter lines (at the first whitespace sequence that occurs before the limit. If the given string contains newlines, they will not be removed. Any lines that begin with whitespace will not be wordwrapped.

This version takes into account ANSI escape characters:

  • stop escape sequence styling at the end of a split line
  • start it again on the next line if needed after the indent
  • do not account for the length of the escape sequences when wrapping
Parameters:
  • indent (int) - If specified, then indent each line by this number of spaces.
  • startindex (int) - If specified, then assume that the first line is already preceeded by startindex characters.
  • splitchars - A list of non-whitespace characters which can be used to split a line. (E.g., use '/\' to allow path names to be split over multiple lines.)
Returns: str

render_domtree(self, tree, indent=0, seclevel=0)

 

Convert a DOM document encoding epytext to an 8-bits ascii string with ANSI formating for simpler styles.

Parameters:
  • tree (Element) - A DOM document encoding of an epytext string.
  • indent (int) - The indentation for the string representation of tree. Each line of the returned string will begin with indent space characters.
  • seclevel (int) - The section level that tree appears at. This is used to generate section headings.
Returns: string
The formated string.

Class Variable Details [hide private]

_ansi_styles

Value:
{'bold': 1, 'underline': 4, 'blink': 5, 'reverse': 7, 'concealed': 8}

colors

Value:
['black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white'\
]