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 ¶
const ( MaxRules = 200 MaxPattern = 1000 )
MaxRules bounds the user's rules, and MaxPattern their patterns' bytes.
Variables ¶
var ErrEmpty = errors.New("highlight: empty pattern")
ErrEmpty is the error of a rule without a pattern.
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 ¶
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) 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 ¶
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.
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.