Skip to content

ANSItools

Module: terminaltexteffects.utils.ansitools

Collection of functions that generate ANSI escape codes for various terminal formatting effects.

These escape codes can be used to modify the appearance of text in a terminal.

Functions:

Name Description
parse_ansi_color_sequence

str) -> int | str: Parse an 8-bit or 24-bit ANSI color sequence.

dec_save_cursor_position

Save the cursor position using DEC sequence.

dec_restore_cursor_position

Restore the cursor position using DEC sequence.

hide_cursor

Hide the cursor.

show_cursor

Show the cursor.

move_cursor_up

int) -> str: Move the cursor up y lines.

move_cursor_to_column

int) -> str: Move the cursor to the specified column.

reset_all

Reset all formatting.

apply_bold

Apply bold formatting.

apply_dim

Apply dim formatting.

apply_italic

Apply italic formatting.

apply_underline

Apply underline formatting.

apply_blink

Apply blink formatting.

apply_reverse

Apply reverse formatting.

apply_hidden

Apply hidden formatting.

apply_strikethrough

Apply strikethrough formatting.

Apply blink formatting.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def apply_blink() -> str:
    """Apply blink formatting.

    Returns:
        str: ANSI escape code

    """
    return "\033[5m"

apply_bold()

Apply bold formatting.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def apply_bold() -> str:
    """Apply bold formatting.

    Returns:
        str: ANSI escape code

    """
    return "\033[1m"

apply_dim()

Apply dim formatting.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def apply_dim() -> str:
    """Apply dim formatting.

    Returns:
        str: ANSI escape code

    """
    return "\033[2m"

apply_hidden()

Apply hidden formatting.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def apply_hidden() -> str:
    """Apply hidden formatting.

    Returns:
        str: ANSI escape code

    """
    return "\033[8m"

apply_italic()

Apply italic formatting.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def apply_italic() -> str:
    """Apply italic formatting.

    Returns:
        str: ANSI escape code

    """
    return "\033[3m"

apply_reverse()

Apply reverse formatting.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def apply_reverse() -> str:
    """Apply reverse formatting.

    Returns:
        str: ANSI escape code

    """
    return "\033[7m"

apply_strikethrough()

Apply strikethrough formatting.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def apply_strikethrough() -> str:
    """Apply strikethrough formatting.

    Returns:
        str: ANSI escape code

    """
    return "\033[9m"

apply_underline()

Apply underline formatting.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def apply_underline() -> str:
    """Apply underline formatting.

    Returns:
        str: ANSI escape code

    """
    return "\033[4m"

dec_restore_cursor_position()

Restore the cursor position using DEC sequence.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def dec_restore_cursor_position() -> str:
    """Restore the cursor position using DEC sequence.

    Returns:
        str: ANSI escape code

    """
    return "\0338"

dec_save_cursor_position()

Save the cursor position using DEC sequence.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def dec_save_cursor_position() -> str:
    """Save the cursor position using DEC sequence.

    Returns:
        str: ANSI escape code

    """
    return "\0337"

hide_cursor()

Hide the cursor.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def hide_cursor() -> str:
    """Hide the cursor.

    Returns:
        str: ANSI escape code

    """
    return "\033[?25l"

move_cursor_to_column(x)

Move the cursor to the specified column.

Parameters:

Name Type Description Default
x int

column number

required

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def move_cursor_to_column(x: int) -> str:
    """Move the cursor to the specified column.

    Args:
        x (int): column number

    Returns:
        str: ANSI escape code

    """
    return f"\033[{x}G"

move_cursor_up(y)

Move the cursor up y lines.

Parameters:

Name Type Description Default
y int

number of lines to move up

required

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def move_cursor_up(y: int) -> str:
    """Move the cursor up y lines.

    Args:
        y (int): number of lines to move up

    Returns:
        str: ANSI escape code

    """
    return f"\033[{y}A"

parse_ansi_color_sequence(sequence)

Parse an 8-bit or 24-bit ANSI color sequence.

Returns the color code as an integer, in the case of 8-bit, or a hex string in the case of 24-bit.

Parameters:

Name Type Description Default
sequence str

ANSI color sequence

required

Returns:

Type Description
int | str

int | str: 8-bit color int or 24-bit color str

Source code in terminaltexteffects/utils/ansitools.py
def parse_ansi_color_sequence(sequence: str) -> int | str:
    """Parse an 8-bit or 24-bit ANSI color sequence.

    Returns the color code as an integer, in the case of 8-bit, or a hex string in the case of 24-bit.

    Args:
        sequence (str): ANSI color sequence

    Returns:
        int | str: 8-bit color int or 24-bit color str

    """
    # Remove escape characters
    sequence = re.sub(r"(\033\[|\x1b\[)", "", sequence).strip("m")
    # detect 24-bit colors
    if re.match(r"^(38;2|48;2)", sequence):
        sequence = re.sub(r"^(38;2;|48;2;)", "", sequence)
        colors = []
        for color in sequence.split(";"):
            if color:
                colors.append(int(color))
            else:
                colors.append(0)  # default to 0 if no value in field (e.g. 38;2;;0m)
        return "".join(f"{color:02X}" for color in colors)
    # detect 8-bit colors
    if re.match(r"^(38;5|48;5)", sequence):
        sequence = re.sub(r"^(38;5;|48;5;)", "", sequence)
        return int(sequence)
    msg = "Invalid ANSI color sequence"
    raise ValueError(msg)

reset_all()

Reset all formatting.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def reset_all() -> str:
    """Reset all formatting.

    Returns:
        str: ANSI escape code

    """
    return "\033[0m"

show_cursor()

Show the cursor.

Returns:

Name Type Description
str str

ANSI escape code

Source code in terminaltexteffects/utils/ansitools.py
def show_cursor() -> str:
    """Show the cursor.

    Returns:
        str: ANSI escape code

    """
    return "\033[?25h"