Highlighting¶
Syntax highlighting is data, not code: each rule is a pure function from
one line of text to style spans. Rules run in registration order and later
spans merge over earlier ones; link anchors additionally skip overlaps, so
a file.py:10 inside a URL stays one link. Lines over 10,000 characters
skip highlighting entirely (mega-line guard).
Add a language without touching the viewer:
from ansi_text_viewer.highlight import HighlightRule, Span
class TodoRule:
def spans(self, text):
i = text.find("TODO")
if i >= 0:
yield Span(i, i + 4, bold=True)
viewer.add_highlight_rule(TodoRule())
viewer.remove_highlight_rule(rule) # symmetrical removal
Built-in rules¶
LogLevelRule—[ERROR]/WARN:-style markers in bold level colors. Colors are injected through the constructor, so custom palettes compose:LogLevelRule({"ERROR": QColor(255, 0, 0)}).TracebackRule— whole-line red bold for PythonTracebackheaders.JsonKeyRule— blue"key":names.CMakeRule—CMake Error(red) vsWarning(orange),-- Configuring…status lines in blue,[ 50%] Built targetin green. Error wins over status on the same line.JustRule—#comments (gray italic),recipe:names (blue bold),NAME :=assignments (purple bold),{{vars}}(magenta). Comment lines suppress the other justfile patterns.UrlRule—http(s)://anchors, blue underlined.FileRule—path/to/file.py:lineanchors; the:linesuffix is stripped from the href so the OS opens the file. Skips anything containing://so URLs keep a single anchor.
A Span carries start/end offsets plus optional foreground, bold,
italic, underline, and anchor_href. Spans are frozen dataclasses, so
rules stay trivially unit-testable without a widget.
ansi_text_viewer.highlight.Span
dataclass
¶
A styled range inside one line (offsets are 0-based).
ansi_text_viewer.highlight.HighlightRule
¶
ansi_text_viewer.highlight.LogLevelRule
¶
Colors [ERROR]/WARN:-style level markers bold.
Create the rule, optionally overriding level colors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
colors
|
dict[str, QColor] | None
|
Level name to color, defaults to built-ins. |
None
|
Source code in ansi_text_viewer/highlight.py
ansi_text_viewer.highlight.TracebackRule
¶
Marks Python Traceback lines red and bold.
spans
¶
Yield a whole-line span when text is a traceback header.
ansi_text_viewer.highlight.CMakeRule
¶
Colors CMake errors, status lines and build progress.
spans
¶
Yield spans for CMake output in text (error wins over status).
Source code in ansi_text_viewer/highlight.py
ansi_text_viewer.highlight.JustRule
¶
Colors justfile comments, recipes, assignments and variables.
spans
¶
Yield spans for justfile syntax in text.
Source code in ansi_text_viewer/highlight.py
ansi_text_viewer.highlight.UrlRule
¶
Turns http(s):// URLs into anchors.
spans
¶
Yield an underlined blue anchor span per URL in text.
Source code in ansi_text_viewer/highlight.py
ansi_text_viewer.highlight.FileRule
¶
Turns path/to/file.py:line references into anchors.
spans
¶
Yield an anchor span per file reference (href drops :line).