Class Style
ANSI style object. Style
is used to store the ANSI
attributes pertaining to a single text style used in the BBS or external.
By definining styles in terms of an intermediary object (rather than
outputting ANSI escape sequences directly), the Hermes API runtime is
able to make certain optimizations and decisions about the text it is
sending to the user. For example, if a user does not support ANSI text,
the Style
object will silently discard the style change
commands when it writes the text to the user's screen.
Each style is contains the following attributes:
-
Foreground color
-
Background color
-
Intense text?
-
Underline text?
-
Blinking text?
The attributes are defined as constants in the hermes
module.
To construct a new Style
object, initialize it with the
desired attributes. For example, to define a style with blinking green
text on a black background, the following constructor would be used:
>>> flashingGreen = Style(fgGreen, bgBlack, attrBlinking)
The style parameters can be passed to the Style
constructor in any order. The following styles are identical:
>>> a = Style(fgGreen, bgBlack)
>>> b = Style(bgBlack, fgGreen)
>>> a == b
1
Once a Style
object has been created, the easiest way to
use it is to call it like a function with the text you wish to style:
>>> redText = Style(fgRed, bgBlack)
>>> redText('Warning: Something must be done!')
'\x1B[31;40mSomething must be done!\x1B[0m'
The strings will only be stylized if the user supports ANSI. If the
user does not support ANSI, then the string will appear in its original
form (for this example, assume that user.hasANSI
is
False
):
>>> redText = Style(fgRed, bgBlack)
>>> redText('Warning: Something must be done!')
'Something must be done!'
If a certain style will only be used once, for a single piece of text,
it can be constructed and used inline:
>>> Style(fgYellow, bgBlack)('Yellow text')
'\x1B[33;40mYellow text\x1B[0m'
This usage of the Style
class results in a
Style
object being constructed every time the string is
displayed. Not only is this inefficient, but it makes it hard to re-style
your external (you have to track down all of these temporary
Style
commands and change them). It is much better to define
all of your common Style
s as global constants in one place
in your external.
Method Summary |
|
__init__(self,
*args,
**kwargs)
|
|
__call__(self,
*args,
**kwargs)
|
|
__eq__(self,
other)
|
|
__repr__(self)
|
|
init(self,
*args)
|
|
reset(self)
|
|
set(self)
|