Skip to content

Ansi handler

ansi_handler

Handle basic logging with color via ANSI commands.

Will call into win32 commands as needed when needed

CONSOLE_SCREEN_BUFFER_INFO

Object representing a console screen buffer.

COORD class-attribute instance-attribute

COORD = _COORD

struct in wincon.h.

__str__

__str__() -> str

String representation of the console screen buffer.

Win32Console

A simple wrapper around the few methods calls to windows.

winapi_test staticmethod

winapi_test() -> Callable

Returns the winapi_test.

GetConsoleScreenBufferInfo staticmethod

GetConsoleScreenBufferInfo(
    stream_id: int = STDOUT,
) -> CONSOLE_SCREEN_BUFFER_INFO

Returns the console screen buffer info object.

SetConsoleTextAttribute staticmethod

SetConsoleTextAttribute(stream_id: int, attrs: int) -> bool

Sets the console text attribute.

WinColor

Enum representing Windows Console colors.

AnsiColor

Defines the different codes for the ansi colors.

__contains__ classmethod

__contains__(item: str) -> bool

Verifies we contain the color.

ColoredFormatter

The formatter that outputs ANSI codes as needed.

__init__

__init__(
    msg: str = "", use_azure: bool = False
) -> ColoredFormatter

Inits the formatter.

format

format(record: logging.LogRecord) -> str

Formats the given record and returns it.

ColoredStreamHandler

Class for logging in Color..

__init__

__init__(
    stream: Optional[IO] = None,
    strip: Optional[str] = None,
    convert: Optional[str] = None,
) -> ColoredStreamHandler

Inits a Colored Stream Handler.

handle

handle(record: logging.LogRecord) -> bool

Conditionally emit the specified logging record.

Emission depends on filters which may have been added to the handler. Wrap the actual emission of the record with acquisition/release of the I/O thread lock. Returns whether the filter passed the record for emission.

get_win32_calls

get_win32_calls() -> dict

Returns a dict for converting ANSI Colors to Windows Colors.

set_foreground

set_foreground(
    fore: Optional[int] = None,
    light: bool = False,
    on_stderr: bool = False,
) -> None

Does the win32 call to set the foreground.

set_background

set_background(
    back: Optional[int] = None,
    light: bool = False,
    on_stderr: bool = False,
) -> None

Does the win32 call to see the background.

set_console

set_console(
    attrs: Optional[int] = None, on_stderr: bool = False
) -> None

Does the win32 call to set the console text attribute.

get_attrs

get_attrs() -> int

Gets the current settings for the style and colors selected.

set_attrs

set_attrs(value: int) -> None

Sets the attributes for the style and colors selected.

write

write(text: str) -> None

Writes to stream, stripping ANSI if specified.

write_and_convert

write_and_convert(text: str) -> None

Write the given text to the strip stripping and converting ANSI.

write_plain_text

write_plain_text(
    text: str,
    start: Optional[int] = None,
    end: Optional[int] = None,
) -> None

Writes plain text to our stream.

convert_ansi

convert_ansi(paramstring: str, command: str) -> None

Converts an ANSI command to a win32 command.

extract_params

extract_params(command: str, paramstring: str) -> tuple

Extracts the parameters in the ANSI command.

call_win32

call_win32(command: str, params: list) -> None

Calls the win32 apis set_foreground and set_background.

emit

emit(record: logging.LogRecord) -> None

Logging.handler method we are overriding to emit a record.

SetConsoleTextAttribute

SetConsoleTextAttribute() -> None

Create blank lambda for when on unix / linux.

winapi_test

winapi_test() -> None

Create blank lambda for when on unix / linux.

get_ansi_string

get_ansi_string(color: AnsiColor = AnsiColor.RESET) -> str

Returns the string formatted ANSI command for the specific color.