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
¶
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.
parse
¶
Parse text with escape sequences into display actions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Raw text that may contain |
required |
Returns:
| Type | Description |
|---|---|
list[tuple]
|
List of |
list[tuple]
|
actions such as |
Examples:
setDefaultColors
¶
Override the colors used by SGR reset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fg
|
tuple[int, int, int] | None
|
Default foreground |
None
|
bg
|
tuple[int, int, int] | None
|
Default background |
None
|
Examples:
setAnsi8Color
¶
Override one ANSI base color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
Base color 0..7. |
required |
color
|
tuple[int, int, int]
|
Replacement |
required |
bright
|
bool
|
Whether it applies to the bright variant. |
False
|
ansi_text_viewer.search_handler.SearchHandler
¶
Finds matches and hides non-matching blocks in the viewer.
Create a handler bound to viewer.
highlight_search
¶
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. |
next_match
¶
Jump to the next match, wrapping around.
Returns:
| Type | Description |
|---|---|
int
|
The 1-based index of the now-active match. |
prev_match
¶
Jump to the previous match, wrapping around.
Returns:
| Type | Description |
|---|---|
int
|
The 1-based index of the now-active match. |
setSearchHighlightColor
¶
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
¶
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
¶
Apply the current filter from start_block to the document end.
update_new_blocks
¶
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
¶
Bases: QWidget
Narrow widget beside the viewer for numbers and bookmark clicks.
Create the gutter for viewer.
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.