duck.utils.ansi

Utilities for removing ANSI escape codes from strings and collections of strings.

These helpers are useful when processing terminal output, log messages, or any text containing ANSI color and formatting sequences.

Module Contents

Functions

remove_ansi_escape_codes

Remove ANSI escape codes from multiple strings.

remove_ansi_escape_codes_str

Remove ANSI escape codes from a string.

Data

ANSI_REMOVAL_PATTERN

API

duck.utils.ansi.ANSI_REMOVAL_PATTERN

‘compile(…)’

duck.utils.ansi.remove_ansi_escape_codes(lines: Union[list[str], tuple[str, ...], set[str]]) list[str][source]

Remove ANSI escape codes from multiple strings.

Parameters:

lines – A list, tuple, or set containing strings with optional ANSI escape codes.

Returns:

A list of strings with ANSI escape codes removed.

Raises:
  • TypeError – If lines is not a list, tuple, or set.

  • TypeError – If any item in lines is not a string.

duck.utils.ansi.remove_ansi_escape_codes_str(string: str) str[source]

Remove ANSI escape codes from a string.

Parameters:

string – A string with optional ANSI escape codes.

Returns:

The string with ANSI escape codes removed.

Raises:

TypeError – If string is not a string.