highlight

package
v1.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 10, 2026 License: AGPL-3.0 Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package highlight colors keywords in a terminal's output: words such as errors and warnings, addresses, links and times, by built-in rules (Presets) and the user's own (Rule). It colors what the screen shows, never what the program wrote, and leaves text the program colored as it is unless asked otherwise.

Index

Constants

View Source
const (
	MaxRules   = 200
	MaxPattern = 1000
)

MaxRules bounds the user's rules, and MaxPattern their patterns' bytes.

Variables

View Source
var ErrEmpty = errors.New("highlight: empty pattern")

ErrEmpty is the error of a rule without a pattern.

View Source
var Presets = []Preset{
	{ID: "errors", Sample: "ERROR connection refused: permission denied", /* contains filtered or unexported fields */},
	{ID: "warnings", Sample: "WARNING disk almost full, retrying after timeout", /* contains filtered or unexported fields */},
	{ID: "info", Sample: "INFO server started  DEBUG cache hit", /* contains filtered or unexported fields */},
	{ID: "success", Sample: "Build succeeded: 12 passed, service active (running)", /* contains filtered or unexported fields */},
	{ID: "network", Sample: "from 192.168.1.20:22 via fe80::1 (00:1a:2b:3c:4d:5e)", /* contains filtered or unexported fields */},
	{ID: "urls", Sample: "see https://example.com/docs?page=2", /* contains filtered or unexported fields */},
	{ID: "datetime", Sample: "2026-11-05T09:30:00Z  Nov  5 09:30:01", /* contains filtered or unexported fields */},
	{ID: "http", Sample: `"GET /api/items HTTP/1.1" 200  status=503`, /* contains filtered or unexported fields */},
	{ID: "ids", Sample: "id 3f2b8c1e-9d4a-4b6f-8e2a-1c5d7f9b0a3e  sha1 2fd4e1c67a2d28fced849ee1bb76e7391b93eb12", /* contains filtered or unexported fields */},
}

Presets are the built-in rules, in the order they match (the early ones first): an earlier group's match wins over a later's.

Functions

func PresetIDs

func PresetIDs() []string

PresetIDs are the IDs of the presets, all of them on by default.

Types

type Highlighter

type Highlighter struct {
	// contains filtered or unexported fields
}

Highlighter matches the rules of the user's, then the presets on, in the lines a terminal shows. It remembers the matches of the lines it has seen, so a screen that scrolls costs only its new lines. It is not safe for concurrent use.

func New

func New(rules []Rule, on []string) *Highlighter

New is a Highlighter of the user's rules on (those that compile), first, and the presets whose IDs on lists.

func (*Highlighter) Apply

func (h *Highlighter) Apply(lines []vt.Line, override bool)

Apply shows the matches in the cells of lines, the rows a terminal shows, top first. A line that wraps into the next is matched with it. The cells the program colored keep their colors unless override.

func (*Highlighter) Empty

func (h *Highlighter) Empty() bool

Empty says whether h has no rules.

func (*Highlighter) Spans

func (h *Highlighter) Spans(text string) []Span

Spans are the matches in text, in order and apart. Where matches overlap, the earlier rule's wins, and a later match that overlaps one shown does not show at all, so no word is colored in part.

type Preset

type Preset struct {
	ID     string
	Sample string
	// contains filtered or unexported fields
}

Preset is a group of built-in rules the user turns on or off as one. Sample is a line its rules color, for the settings to show.

type Rule

type Rule struct {
	Pattern       string `json:"pattern"`
	Regex         bool   `json:"regex,omitempty"`
	CaseSensitive bool   `json:"caseSensitive,omitempty"`
	WholeWord     bool   `json:"wholeWord,omitempty"`
	Style
	Off bool `json:"off,omitempty"`
}

Rule is a rule of the user's: text, or a regular expression (RE2), and how its matches show.

func (Rule) Compile

func (r Rule) Compile() (*regexp.Regexp, error)

Compile is the expression of r's pattern.

type Span

type Span struct {
	From, To int
	Style    Style
}

Span is a match in a text: the bytes From to To show in Style.

type Style

type Style struct {
	Color      uint8 `json:"color"`
	Background bool  `json:"background,omitempty"`
	Bold       bool  `json:"bold,omitempty"`
	Underline  bool  `json:"underline,omitempty"`
}

Style is how a match shows: Color is an index of the terminal's 16 colors, for the text or, with Background, behind it.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL