Skip to content

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 Python Traceback headers.
  • JsonKeyRule — blue "key": names.
  • CMakeRule — CMake Error (red) vs Warning (orange), -- Configuring… status lines in blue, [ 50%] Built target in 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:line anchors; the :line suffix 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

Span(
    start,
    end,
    foreground=None,
    bold=False,
    italic=False,
    underline=False,
    anchor_href=None,
)

A styled range inside one line (offsets are 0-based).

ansi_text_viewer.highlight.HighlightRule

Bases: Protocol

Structural type for pluggable highlight rules.

spans

spans(text)

Yield style spans for one line of text.

ansi_text_viewer.highlight.LogLevelRule

LogLevelRule(colors=None)

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

colors instance-attribute

colors = (
    dict(colors)
    if colors
    else dict(_DEFAULT_LOG_LEVEL_COLORS)
)

spans

spans(text)

Yield a bold span per level marker in text.

ansi_text_viewer.highlight.TracebackRule

Marks Python Traceback lines red and bold.

spans

spans(text)

Yield a whole-line span when text is a traceback header.

ansi_text_viewer.highlight.JsonKeyRule

Colors JSON object keys blue.

spans

spans(text)

Yield a span per "key": name in text.

ansi_text_viewer.highlight.CMakeRule

Colors CMake errors, status lines and build progress.

spans

spans(text)

Yield spans for CMake output in text (error wins over status).

ansi_text_viewer.highlight.JustRule

Colors justfile comments, recipes, assignments and variables.

spans

spans(text)

Yield spans for justfile syntax in text.

ansi_text_viewer.highlight.UrlRule

Turns http(s):// URLs into anchors.

spans

spans(text)

Yield an underlined blue anchor span per URL in text.

ansi_text_viewer.highlight.FileRule

Turns path/to/file.py:line references into anchors.

spans

spans(text)

Yield an anchor span per file reference (href drops :line).