Python strip ansi

11 Python code examples are found related to " strip ansi". You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example.
Example 1
Source File: text.py    From Carnets with BSD 3-Clause "New" or "Revised" License 9 votes vote down vote up
def strip_ansi(source):
    """
    Remove ansi escape codes from text.
    
    Parameters
    ----------
    source : str
        Source to remove the ansi from
    """
    return re.sub(r'\033\[(\d|;)+?m', '', source)


#-----------------------------------------------------------------------------
# Utils to columnize a list of string
#----------------------------------------------------------------------------- 
Example 2
Source File: util_misc.py    From netharn with Apache License 2.0 8 votes vote down vote up
def strip_ansi(text):
    r"""
    Removes all ansi directives from the string.

    References:
        http://stackoverflow.com/questions/14693701/remove-ansi
        https://stackoverflow.com/questions/13506033/filtering-out-ansi-escape-sequences

    Examples:
        >>> line = '\t\u001b[0;35mBlabla\u001b[0m     \u001b[0;36m172.18.0.2\u001b[0m'
        >>> escaped_line = strip_ansi(line)
        >>> assert escaped_line == '\tBlabla     172.18.0.2'
    """
    # ansi_escape1 = re.compile(r'\x1b[^m]*m')
    # text = ansi_escape1.sub('', text)
    # ansi_escape2 = re.compile(r'\x1b\[([0-9,A-Z]{1,2}(;[0-9]{1,2})?(;[0-9]{3})?)?[m|K]?')
    ansi_escape3 = re.compile(r'(\x9B|\x1B\[)[0-?]*[ -/]*[@-~]', flags=re.IGNORECASE)
    text = ansi_escape3.sub('', text)
    return text 
Example 3
Source File: console.py    From Carnets with BSD 3-Clause "New" or "Revised" License 7 votes vote down vote up
def strip_ansi_codes(s):
    """
    Remove ANSI color codes from the string.
    """
    return re.sub('\033\\[([0-9]+)(;[0-9]+)*m', '', s) 
Example 4
Source File: utils.py    From WebPocket with GNU General Public License v3.0 6 votes vote down vote up
def strip_ansi(text: str) -> str:
    """Strip ANSI escape codes from a string.

    :param text: string which may contain ANSI escape codes
    :return: the same string with any ANSI escape codes removed
    """
    return constants.ANSI_ESCAPE_RE.sub('', text) 
Example 5
Source File: ansi.py    From Computable with MIT License 5 votes vote down vote up
def strip_ansi(source):
    """
    Remove ansi from text
    
    Parameters
    ----------
    source : str
        Source to remove the ansi from
    """
    
    return re.sub(r'\033\[(\d|;)+?m', '', source) 
Example 6
Source File: string.py    From pytos with Apache License 2.0 5 votes vote down vote up
def strip_ansi_codes(ansi_str):
    """
    Strip ANSI codes from a string.
    :param ansi_str: The string from which to strip ANSI codes.
    :return: The string minus ANSI codes.
    :rtype: str
    """
    ansi_escape = re.compile(r'\x1b(?:\[\??|\(|\)|=)[0-9a-zA-Z;<=#]*')
    return ansi_escape.sub('', ansi_str) 
Example 7
Source File: string.py    From boss with MIT License 5 votes vote down vote up
def strip_ansi(text):
    ''' Strip ANSI escape character codes from a string. '''
    if not text:
        return text

    return re.sub(ANSI_CODES_REGEX, '', text) 
Example 8
Source File: util.py    From molecule with MIT License 5 votes vote down vote up
def strip_ansi_escape(data):
    """Remove all ANSI escapes from string or bytes.

    If bytes is passed instead of string, it will be converted to string
    using UTF-8.
    """
    if isinstance(data, bytes):
        data = data.decode("utf-8")

    return re.sub(r"\x1b[^m]*m", "", data) 
Example 9
Source File: util.py    From molecule with MIT License 5 votes vote down vote up
def strip_ansi_color(data):
    """Remove ANSI colors from string or bytes."""
    if isinstance(data, bytes):
        data = data.decode("utf-8")

    # Taken from tabulate
    invisible_codes = re.compile(r"\x1b\[\d*m")

    return re.sub(invisible_codes, "", data) 
Example 10
Source File: common.py    From sen with MIT License 4 votes vote down vote up
def strip_from_ansi_esc_sequences(text):
    """
    find ANSI escape sequences in text and remove them

    :param text: str
    :return: list, should be passed to ListBox
    """
    # esc[ + values + control character
    # h, l, p commands are complicated, let's ignore them
    seq_regex = r"\x1b\[[0-9;]*[mKJusDCBAfH]"
    regex = re.compile(seq_regex)
    start = 0
    response = ""
    for match in regex.finditer(text):
        end = match.start()
        response += text[start:end]

        start = match.end()
    response += text[start:len(text)]
    return response


# def colorize_text(text):
#     """
#     finds ANSI color escapes in text and transforms them to urwid
#
#     :param text: str
#     :return: list, should be passed to ListBox
#     """
#     # FIXME: not finished
#     response = []
#     # http://ascii-table.com/ansi-escape-sequences.php
#     regex_pattern = r"(?:\x1b\[(\d+)?(?:;(\d+))*m)([^\x1b]+)"  # [%d;%d;...m
#     regex = re.compile(regex_pattern, re.UNICODE)
#     for match in regex.finditer(text):
#         groups = match.groups()
#         t = groups[-1]
#         color_specs = groups[:-1]
#         urwid_spec = translate_asci_sequence(color_specs)
#         if urwid_spec:
#             item = (urwid.AttrSpec(urwid_spec, "main_list_dg"), t)
#         else:
#             item = t
#         item = urwid.AttrMap(urwid.Text(t, align="left", wrap="any"), "main_list_dg", "main_list_white")
#         response.append(item)
#     return response