![]() |
Belle II Software development
|
Public Member Functions | |
| convert_color (cls, color) | |
| color (cls, foreground=None, background=None, bold=False, underline=False, inverted=False) | |
| fg (cls, color) | |
| bg (cls, color) | |
| reset (cls) | |
Static Public Member Functions | |
| supported () | |
Simple class to handle color output to the terminal.
This class allows to very easily add color output to the terminal
>>> from terminal_utils import ANSIColors as ac
>>> print(f"{ac.fg('red')}Some text in {ac.color(underline=True)}RED{ac.reset()}")
The basic colors can be specified by name (case insensitive) or by enum value.
Custom colors can be supplied using hex notation like ``#rgb`` or ``#rrggbb``
(Hint: ``matplotlib.colors.to_hex`` might be useful here). As an example to
use the viridis colormap to color the output on the terminal::
from matplotlib import cm
from matplotlib.colors import to_hex
from terminal_utils import ANSIColors as ac
# sample the viridis colormap at 16 points
for i in range(16):
# convert i to be in [0..1] and get the hex color
color = to_hex(cm.viridis(i/15))
# and print the hex color in the correct color
print(f"{i}. {ac.fg(color)}{color}{ac.reset()}")
If the output is not to a terminal color output will be disabled and nothing
will be added to the output, for example when redirecting the output to a
logfile.
Definition at line 33 of file terminal_utils.py.
| bg | ( | cls, | |
| color ) |
Shorthand for `color(background=color) <color>`
Definition at line 149 of file terminal_utils.py.
| color | ( | cls, | |
| foreground = None, | |||
| background = None, | |||
| bold = False, | |||
| underline = False, | |||
| inverted = False ) |
Change terminal colors to the given foreground/background colors and attributes.
This will return a string to be printed to change the color on the terminal.
To revert to default print the output of `reset()`
Parameters:
foreground (int or str): foreground color to use, can be any value accepted by `convert_color`
If None is given the current color will not be changed.
background (int or str): background color to use, can be any value accepted by `convert_color`.
If None is given the current color will not be changed.
bold (bool): Use bold font
underline (bool): Underline the text
inverted (bool): Flip background and foreground color
Definition at line 109 of file terminal_utils.py.
| convert_color | ( | cls, | |
| color ) |
Convert a color to the necessary ansi code. The argument can either bei
* an integer corresponding to the ansi color (see the enum values of this class)
* the name (case insensitive) of one of the enum values of this class
* a hex color code of the form ``#rgb`` or ``#rrggbb``
Raises:
KeyError: if the argument is a string not matching to one of the known colors
Definition at line 85 of file terminal_utils.py.
| fg | ( | cls, | |
| color ) |
Shorthand for `color(foreground=color) <color>`
Definition at line 144 of file terminal_utils.py.
| reset | ( | cls | ) |
Reset colors to default
Definition at line 154 of file terminal_utils.py.
|
static |
Check whether the output is a terminal. If this is False, the methods `color`, `fg`, `bg` and `reset` will only return an empty string as color output will be disabled
Definition at line 75 of file terminal_utils.py.