get_ansi_colour_code

pyhelpers.ops.get_ansi_colour_code(colours, show_valid_colours=False)[source]

Returns the ANSI escape code(s) for the given colour name(s).

Parameters:
  • colours (str | list[str] | tuple[str]) – A single colour name (str) or a sequence of colour names (e.g. ‘red’, [‘red’, ‘green’]).

  • show_valid_colours (bool) – If True, returns a tuple containing the ANSI code(s) and a set of valid colour names.

Returns:

The ANSI escape code(s) for the given colour(s), or an error message if an invalid colour is provided.

Return type:

str | list[str] | tuple[str | list[str], set[str]]

Examples:

>>> from pyhelpers.ops import get_ansi_colour_code
>>> get_ansi_colour_code('red')
'\033[31m'
>>> get_ansi_colour_code(['red', 'blue'])
['\033[31m', '\033[34m']
>>> get_ansi_colour_code('invalid_colour')
Traceback (most recent call last):
    ...
ValueError: 'invalid_colour' is not a valid colour name.
>>> get_ansi_colour_code('red', show_valid_colours=True)
('\033[31m', {'black', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white', ...})