Help us shape the future of Go! Take the 2026 Go Developer Survey

highlight

package
v1.6.0 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2026 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package highlight provides small, terminal-safe syntax highlighting for human-facing previews. It deliberately does not alter source content or participate in scanning, token counts, or selection.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ANSI added in v1.2.0

func ANSI(hex string, mods ...string) string

ANSI builds a truecolor SGR sequence for hex (e.g. "#cba6f7" or "cba6f7") with optional SGR modifiers ("1" for bold, "2" for faint). It returns "" for unparseable input so callers degrade to unstyled text.

func Render

func Render(path string, content []byte, options Options) string

Render highlights complete content. It is intended for a terminal writer; callers must decide whether their output is a TTY before enabling it.

func RenderDocumentLine added in v1.3.0

func RenderDocumentLine(doc *Document, line int, options Options) string

RenderDocumentLine renders one parsed line with the supplied palette.

func RenderLine

func RenderLine(path, line string, options Options) string

RenderLine highlights one line. Keeping this operation line-local makes it suitable for the TUI, which can render only the visible preview rows.

Types

type ColorProfile added in v1.3.0

type ColorProfile uint8

ColorProfile controls semantic ANSI color degradation.

const (
	ProfileTrueColor ColorProfile = iota
	ProfileANSI256
	ProfileANSI16
	ProfileNone
)

type Document added in v1.3.0

type Document struct {
	Path     string
	Language string
	Lines    []Line
}

Document is the cached semantic representation used by the preview.

func Parse added in v1.3.0

func Parse(path string, content []byte, options Options) *Document

Parse builds a conservative semantic document. Unknown languages receive generic lexical classes without attempting language inference.

type Line added in v1.3.0

type Line struct {
	Text  string
	Spans []Span
}

Line contains original source text and its theme-independent spans.

type Options

type Options struct {
	Enabled  bool
	Theme    Theme
	MaxBytes int
	Profile  ColorProfile
	// Palette overrides the built-in Theme palette when non-nil. The TUI
	// sets it from the active color preset so preview highlighting follows
	// the picker's theme; Theme is kept as the fallback.
	Palette *Palette
	// Syntax is the semantic palette used by the cached document renderer.
	// Palette remains supported for compatibility with existing callers.
	Syntax *SyntaxPalette
}

Options controls terminal highlighting.

type Palette added in v1.2.0

type Palette struct {
	Keyword, String, Number, Comment, TypeName string
}

Palette holds the SGR sequences for each token class. The TUI builds one per color preset so preview highlighting matches the picker's theme.

type Span added in v1.3.0

type Span struct {
	Start int
	End   int
	Kind  TokenKind
}

Span is a half-open semantic range within a line. Offsets are byte offsets.

type SyntaxCache added in v1.3.0

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

SyntaxCache stores theme-independent documents by path and content hash. It is intentionally process-local; semantic results are not persisted.

func NewSyntaxCache added in v1.3.0

func NewSyntaxCache() *SyntaxCache

NewSyntaxCache creates an empty cache with a bounded number of documents.

func NewSyntaxCacheWithCapacity added in v1.3.0

func NewSyntaxCacheWithCapacity(maxEntries int) *SyntaxCache

NewSyntaxCacheWithCapacity creates a cache holding at most maxEntries documents. A non-positive capacity uses the default of 256.

func (*SyntaxCache) Get added in v1.3.0

func (c *SyntaxCache) Get(path string, content []byte, options Options) *Document

Get returns a cached document or parses content once for the current hash. Parsing deliberately happens outside the mutex: a large preview must not block another caller from invalidating or reading a different entry.

func (*SyntaxCache) Invalidate added in v1.3.0

func (c *SyntaxCache) Invalidate(path string)

Invalidate removes one path from the cache.

type SyntaxPalette added in v1.3.0

type SyntaxPalette map[TokenKind]TokenStyle

SyntaxPalette maps semantic token meanings to appearance styles.

type Theme

type Theme string
const (
	ThemeAuto  Theme = "auto"
	ThemeNone  Theme = "none"
	ThemeDark  Theme = "dark"
	ThemeLight Theme = "light"
)

type TokenKind added in v1.3.0

type TokenKind uint8

TokenKind identifies the meaning of a source span without embedding color or terminal details.

const (
	TokenPlain TokenKind = iota
	TokenComment
	TokenDocComment
	TokenKeyword
	TokenString
	TokenStringEscape
	TokenRegex
	TokenNumber
	TokenBool
	TokenNull
	TokenType
	TokenFunction
	TokenVariable
	TokenConstant
	TokenProperty
	TokenBuiltin
	TokenOperator
	TokenPunctuation
	TokenAttribute
	TokenDecorator
	TokenTag
	TokenTagAttribute
	TokenMarkupHeading
	TokenMarkupLink
	TokenShebang
	TokenDiffAdded
	TokenDiffRemoved
	TokenDiffHunk
)

type TokenStyle added in v1.3.0

type TokenStyle struct {
	Foreground string
	Bold       bool
	Faint      bool
	Italic     bool
	Underline  bool
}

TokenStyle describes appearance while keeping terminal rendering separate.

Jump to

Keyboard shortcuts

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