Skip to content

color

color(*args, auto_end=True)

Color a message.

Format the arguments by replacing the colors in the Colors class. For instance::

color('{blue}Hello there{end}\n{yellow}WARNING')

Parameters:

Name Type Description Default
args str

Strings to color

()
auto_end bool

Add a string to end coloring

True

Returns:

Type Description
str

A formatted message.

Source code in m/color/colors.py
def color(*args: str, auto_end: bool = True) -> str:
    r"""Color a message.

    Format the arguments by replacing the colors in the Colors class.
    For instance::

        color('{blue}Hello there{end}\n{yellow}WARNING')

    Args:
        args: Strings to color
        auto_end: Add a string to end coloring

    Returns:
        A formatted message.
    """
    no_color = color_disabled()
    color_map = no_color_dict if no_color else color_dict
    end = '' if no_color or not auto_end else Color.end
    msg_list = [msg.format(**color_map) for msg in args]
    return ''.join(msg_list) + end

color_disabled()

Check for environment variable NO_COLOR.

Utilities that format strings with color should call this function to determine if the user wants color in the final output.

Returns:

Type Description
bool

True if it exists and its set to true or 1.

Source code in m/color/disable.py
def color_disabled() -> bool:
    """Check for environment variable NO_COLOR.

    Utilities that format strings with color should call this function
    to determine if the user wants color in the final output.

    Returns:
        True if it exists and its set to true or 1.
    """
    return os.environ.get('NO_COLOR', 'false') in {'true', '1', 'True'}

highlight_json(text)

Highlight a json string.

Parameters:

Name Type Description Default
text str

The json string to highlight.

required

Returns:

Type Description
str

A colorized json string.

Source code in m/color/pygment.py
def highlight_json(text: str) -> str:
    """Highlight a json string.

    Args:
        text: The json string to highlight.

    Returns:
        A colorized json string.
    """
    return _highlight(text, JsonLexer())

highlight_yaml(text)

Highlight a yaml string.

Parameters:

Name Type Description Default
text str

The yaml string to highlight.

required

Returns:

Type Description
str

A colorized yaml string.

Source code in m/color/pygment.py
def highlight_yaml(text: str) -> str:
    """Highlight a yaml string.

    Args:
        text: The yaml string to highlight.

    Returns:
        A colorized yaml string.
    """
    return _highlight(text, YamlLexer())