Skip to content

Internals

The pieces the viewer composes. You rarely need these directly, but they are public, typed, and tested on their own.

ANSI decoding

AnsiEscapeHandler keeps terminal style state (colors, bold, italic, reverse, …) across parse() calls, exactly like a terminal: a \x1b[31m on one append still colors the next append until \x1b[0m resets it. Repeat counts are clamped at 10,000 and digit runs capped, so hostile input like \x1b[999999999C cannot freeze the UI. setDefaultColors() changes what reset restores (this is how themes work); setAnsi8Color() overrides individual palette entries.

Search engine

SearchHandler implements highlight search over the QTextDocument (plain or QRegularExpression, optional case sensitivity) with wrapping next_match() / prev_match() navigation, plus the hide-non-matching-blocks filter. Defenses: invalid regex falls back to a literal search instead of raising, and patterns over 500 characters are rejected to blunt ReDoS. Newly appended blocks are filtered on arrival via update_new_blocks(), so live streams respect an active filter.

Gutter

LineNumberArea paints line numbers, bookmark markers, and theme-aware gutter colors delegated from the viewer. Clicking it toggles the bookmark on that line (toggleBookmarkAtY() refuses invisible blocks).

ansi_text_viewer.ansi_escape_handler.AnsiEscapeHandler

AnsiEscapeHandler()

Stateful ANSI SGR and cursor-sequence decoder.

Keeps the current style (colors, bold, ...) across parse calls, like a terminal. Repeat counts are clamped to blunt DoS input.

Examples:

>>> handler = AnsiEscapeHandler()
>>> actions = handler.parse("\x1b[1mbold")
>>> actions[0][0]
'text'

Create a handler with default terminal styling.

_custom_8 instance-attribute

_custom_8 = {}

default_fg instance-attribute

default_fg = _DEFAULT_FG

default_bg instance-attribute

default_bg = _DEFAULT_BG

reset

reset()

Restore default colors and clear all text attributes.

parse

parse(text)

Parse text with escape sequences into display actions.

Parameters:

Name Type Description Default
text str

Raw text that may contain ESC[ sequences.

required

Returns:

Type Description
list[tuple]

List of ("text", str, TextStyle) tuples plus cursor

list[tuple]

actions such as ("cursor_up", n) or ("erase_line", mode).

Examples:

>>> handler = AnsiEscapeHandler()
>>> actions = handler.parse("plain")
>>> actions[0][:2]
('text', 'plain')

__get_format

__get_format()

__apply_sgr

__apply_sgr(params)

setDefaultColors

setDefaultColors(fg=None, bg=None)

Override the colors used by SGR reset.

Parameters:

Name Type Description Default
fg tuple[int, int, int] | None

Default foreground (r, g, b), or None to keep it.

None
bg tuple[int, int, int] | None

Default background (r, g, b), or None to keep it.

None

Examples:

>>> handler.setDefaultColors((0, 0, 0), (255, 255, 255))

defaultColors

defaultColors()

Return the current (foreground, background) defaults.

setAnsi8Color

setAnsi8Color(idx, color, bright=False)

Override one ANSI base color.

Parameters:

Name Type Description Default
idx int

Base color 0..7.

required
color tuple[int, int, int]

Replacement (r, g, b) color.

required
bright bool

Whether it applies to the bright variant.

False

resetPalette

resetPalette()

Drop all custom palette overrides.

palette

palette()

Return custom (index, bright) -> (r, g, b) overrides.

__ansi8_color

__ansi8_color(idx, bright=False)

__color_256

__color_256(idx)

ansi_text_viewer.search_handler.SearchHandler

SearchHandler(viewer)

Finds matches and hides non-matching blocks in the viewer.

Create a handler bound to viewer.

viewer instance-attribute

viewer = viewer

search_highlight_color instance-attribute

search_highlight_color = QColor(255, 255, 0, 100)

active_search_color instance-attribute

active_search_color = QColor(255, 165, 0, 150)

search_selections instance-attribute

search_selections = []

current_match_index instance-attribute

current_match_index = -1

filter_query instance-attribute

filter_query = ''

filter_use_regex instance-attribute

filter_use_regex = False

filter_match_case instance-attribute

filter_match_case = False
highlight_search(query, use_regex=False, match_case=False)

Highlight every match and jump to the first one.

Parameters:

Name Type Description Default
query str

Text or pattern (empty clears the highlight).

required
use_regex bool

Treat query as a regular expression.

False
match_case bool

Case-sensitive matching.

False

Returns:

Type Description
int

The number of matches found.

__update_search_highlights

__update_search_highlights()

next_match

next_match()

Jump to the next match, wrapping around.

Returns:

Type Description
int

The 1-based index of the now-active match.

prev_match

prev_match()

Jump to the previous match, wrapping around.

Returns:

Type Description
int

The 1-based index of the now-active match.

clear_search_highlight

clear_search_highlight()

Clear all search highlights and selection state.

setSearchHighlightColor

setSearchHighlightColor(color, active_color=None)

Set match backgrounds.

Parameters:

Name Type Description Default
color QColor

Background for inactive matches.

required
active_color QColor | None

Background for the active match, or None to keep it.

None

apply_filter

apply_filter(query, use_regex=False, match_case=False)

Hide blocks that do not match query.

Parameters:

Name Type Description Default
query str

Text or pattern (empty restores every block).

required
use_regex bool

Treat query as a regular expression.

False
match_case bool

Case-sensitive matching.

False

filter_blocks

filter_blocks(start_block)

Apply the current filter from start_block to the document end.

update_new_blocks

update_new_blocks(start_block_number)

Filter blocks appended after start_block_number.

Parameters:

Name Type Description Default
start_block_number int

First block that has not been filtered yet.

required

ansi_text_viewer.line_number_area.LineNumberArea

LineNumberArea(viewer)

Bases: QWidget

Narrow widget beside the viewer for numbers and bookmark clicks.

Create the gutter for viewer.

viewer instance-attribute

viewer = viewer

sizeHint

sizeHint()

Return the gutter width requested by the viewer.

paintEvent

paintEvent(event)

Delegate painting to the viewer.

mousePressEvent

mousePressEvent(event)

Toggle the bookmark of the clicked line.

Viewer event handlers

AnsiTextViewer overrides these Qt handlers to implement Ctrl + wheel zoom, Ctrl + click links, the right-click menu, and the gutter. Qt calls them for you; apps rarely call or override them.

Zoom on Ctrl+Wheel, otherwise scroll normally.

Open links only on Ctrl+Click; plain clicks just move the cursor.

Show a pointing hand while hovering a link.

Build the right-click menu (copy, bookmarks, view, save).

Keep the gutter geometry in sync on resize.

Return the gutter width in pixels (0 when hidden).

Sync viewport margins with the gutter width.

Repaint or scroll the gutter after document updates.

Paint line numbers and bookmark markers in the gutter.