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.
Source code in ansi_text_viewer/ansi_escape_handler.py
reset
¶
Restore default colors and clear all text attributes.
Source code in ansi_text_viewer/ansi_escape_handler.py
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:
Source code in ansi_text_viewer/ansi_escape_handler.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 | |
__get_format
¶
Source code in ansi_text_viewer/ansi_escape_handler.py
__apply_sgr
¶
Source code in ansi_text_viewer/ansi_escape_handler.py
setDefaultColors
¶
Override the colors used by SGR reset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fg
|
QColor | None
|
Default foreground, or None to keep the current one. |
None
|
bg
|
QColor | None
|
Default background, or None to keep the current one. |
None
|
Examples:
Source code in ansi_text_viewer/ansi_escape_handler.py
defaultColors
¶
setAnsi8Color
¶
Override one ANSI base color.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
Base color 0..7. |
required |
color
|
QColor
|
Replacement color. |
required |
bright
|
bool
|
Whether it applies to the bright variant. |
False
|
Source code in ansi_text_viewer/ansi_escape_handler.py
resetPalette
¶
palette
¶
__ansi8_color
¶
Source code in ansi_text_viewer/ansi_escape_handler.py
__color_256
¶
Source code in ansi_text_viewer/ansi_escape_handler.py
ansi_text_viewer.search_handler.SearchHandler
¶
Finds matches and hides non-matching blocks in the viewer.
Create a handler bound to viewer.
Source code in ansi_text_viewer/search_handler.py
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. |
Source code in ansi_text_viewer/search_handler.py
__update_search_highlights
¶
Source code in ansi_text_viewer/search_handler.py
next_match
¶
Jump to the next match, wrapping around.
Returns:
| Type | Description |
|---|---|
int
|
The 1-based index of the now-active match. |
Source code in ansi_text_viewer/search_handler.py
prev_match
¶
Jump to the previous match, wrapping around.
Returns:
| Type | Description |
|---|---|
int
|
The 1-based index of the now-active match. |
Source code in ansi_text_viewer/search_handler.py
clear_search_highlight
¶
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
|
Source code in ansi_text_viewer/search_handler.py
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
|
Source code in ansi_text_viewer/search_handler.py
filter_blocks
¶
Apply the current filter from start_block to the document end.
Source code in ansi_text_viewer/search_handler.py
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 |