Part of twisted.conch.insults View Source
Character attribute manipulation API
This module provides a domain-specific language (using Python syntax) for the creation of text with additional display attributes associated with it. It is intended as an alternative to manually building up strings containing ECMA 48 character attribute control codes. It currently supports foreground and background colors (black, red, green, yellow, blue, magenta, cyan, and white), intensity selection, underlining, blinking and reverse video. Character set selection support is planned.
Character attributes are specified by using two Python operations:
attribute lookup and indexing. For example, the string "Hello
world" with red foreground and all other attributes set to their
defaults, assuming the name twisted.conch.insults.text.attributes has been
imported and bound to the name "A" (with the statement from
twisted.conch.insults.text import attributes as A
, for example) one
uses this expression:
| A.fg.red["Hello world"]
Other foreground colors are set by substituting their name for "red". To set both a foreground and a background color, this expression is used:
| A.fg.red[A.bg.green["Hello world"]]
Note that either A.bg.green can be nested within A.fg.red or vice versa. Also note that multiple items can be nested within a single index operation by separating them with commas:
| A.bg.green[A.fg.red["Hello"], " ", A.fg.blue["world"]]
Other character attributes are set in a similar fashion. To specify a blinking version of the previous expression:
| A.blink[A.bg.green[A.fg.red["Hello"], " ", A.fg.blue["world"]]]
A.reverseVideo
, A.underline
, and
A.bold
are also valid.
A third operation is actually supported: unary negation. This turns off an attribute when an enclosing expression would otherwise have caused it to be on. For example:
| A.underline[A.fg.red["Hello", -A.underline[" world"]]]
Author | Jp Calderone |
Class | CharacterAttributes | Undocumented |
Function | flatten | Serialize a sequence of characters with attribute information |
Class | _Attribute | Undocumented |
Class | _NormalAttr | Undocumented |
Class | _OtherAttr | Undocumented |
Class | _ColorAttr | Undocumented |
Class | _ForegroundColorAttr | Undocumented |
Class | _BackgroundColorAttr | Undocumented |
Serialize a sequence of characters with attribute information
The resulting string can be interpreted by VT102-compatible terminals so that the contained characters are displayed and, for those attributes which the terminal supports, have the attributes specified in the input.
For example, if your terminal is VT102 compatible, you might run this for a colorful variation on the "hello world" theme:
| from twisted.conch.insults.text import flatten, attributes as A | from twisted.conch.insults.helper import CharacterAttribute | print flatten( | A.normal[A.bold[A.fg.red['He'], A.fg.green['ll'], A.fg.magenta['o'], ' ', | A.fg.yellow['Wo'], A.fg.blue['rl'], A.fg.cyan['d!']]], | CharacterAttribute())
Parameters | output | Object returned by accessing attributes of the module-level attributes object. |
attrs | A twisted.conch.insults.helper.CharacterAttribute
instance | |
Returns | A VT102-friendly string |