hbutils.encoding.ansi
- Overview:
Functions for ansi escaping and unescapes. See ANSI escape code - Wikipedia.
This module provides utilities to remove ANSI escape codes from strings, which are commonly used for terminal text formatting (colors, styles, etc.). The main functionality is to clean strings by removing these escape sequences, leaving only the plain text content.
ansi_unescape
- hbutils.encoding.ansi.ansi_unescape(string: str) str[source]
Unescape ansi string by removing ANSI escape codes.
This function removes ANSI escape sequences from the input string, which are typically used for terminal text formatting such as colors, bold, underline, etc. The function uses a regular expression pattern to match and remove these escape codes.
See ANSI escape code - Wikipedia.
- Parameters:
string (str) – Original output string containing ANSI escape codes.
- Returns:
Unescaped ansi string with all ANSI escape codes removed.
- Return type:
str
- Examples::
>>> from hbutils.encoding import ansi_unescape >>> ansi_unescape("[1;31mHello") # Remove red bold formatting 'Hello' >>> ansi_unescape("[2;37;41mWorld") # Remove dim white text on red background 'World'