syntax

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2026 License: BSD-3-Clause Imports: 10 Imported by: 5

README

Syntax

Go package for syntax highlighting + the hicat utility (a bit like syntax highlighted cat).

Installing hicat

The hicat utility can be installed with:

go install github.com/xyproto/syntax/cmd/hicat@latest
General info
  • License: BSD 3-Clause
  • Version: 1.0.0

Documentation

Overview

Package syntax provides syntax highlighting for source code using the same approach as Orbiton. It tokenizes input via text/scanner, classifies tokens into kinds, and wraps them in color tags that can be converted to ANSI escape codes by the vt package.

Theme selection is supported via the O_THEME (or THEME) environment variable.

Index

Constants

This section is empty.

Variables

View Source
var DefaultTextConfig = TextConfig{
	AndOr:         "red",
	AngleBracket:  "red",
	AssemblyEnd:   "lightyellow",
	Class:         "white",
	Comment:       "darkgray",
	Decimal:       "red",
	Dollar:        "white",
	Keyword:       "red",
	Literal:       "white",
	Mut:           "magenta",
	Plaintext:     "white",
	Private:       "red",
	Protected:     "red",
	Public:        "red",
	Punctuation:   "red",
	Self:          "magenta",
	Star:          "white",
	Static:        "lightyellow",
	String:        "lightwhite",
	Tag:           "white",
	TextAttrName:  "white",
	TextAttrValue: "white",
	TextTag:       "white",
	Type:          "white",
	Whitespace:    "",
}

DefaultTextConfig provides color names matching Orbiton's default theme.

View Source
var (

	// Keywords contains the default syntax highlighting keywords
	Keywords = map[string]struct{}{}/* 190 elements not displayed */

)

Functions

func AddAndRemoveKeywords

func AddAndRemoveKeywords(addAndDel ...[]string)

AddAndRemoveKeywords adds then removes keywords from the global Keywords map.

func AddKeywords

func AddKeywords(kws []string)

AddKeywords adds keywords to the global Keywords map.

func AddKeywordsAsUppercase

func AddKeywordsAsUppercase(xs []string)

AddKeywordsAsUppercase adds uppercased keywords to the global Keywords map.

func AdjustKeywords

func AdjustKeywords(m mode.Mode)

AdjustKeywords applies per-language keyword adjustments, matching Orbiton's logic.

func Annotate

func Annotate(src []byte, a Annotator, m mode.Mode) (annotate.Annotations, error)

Annotate tokenizes src and returns annotations for mode m.

func AsText

func AsText(src []byte, m mode.Mode, options ...Option) ([]byte, error)

AsText returns src highlighted for mode m, applying options to TextConfig.

func CatBytes

func CatBytes(sourceCodeData []byte, o *vt.TextOutput) error

CatBytes highlights sourceCodeData and writes it to stdout via the given TextOutput.

func ClearKeywords

func ClearKeywords()

ClearKeywords resets the global Keywords map.

func NewScanner

func NewScanner(src []byte) *scanner.Scanner

NewScanner returns a scanner.Scanner configured for syntax highlighting.

func NewScannerReader

func NewScannerReader(r io.Reader) *scanner.Scanner

NewScannerReader returns a scanner.Scanner configured for syntax highlighting from r.

func Print

func Print(s *scanner.Scanner, w io.Writer, p Printer, m mode.Mode) error

Print scans tokens from s and writes highlighted output using Printer p.

func RemoveKeywords

func RemoveKeywords(kws []string)

RemoveKeywords removes keywords from the global Keywords map.

func SetDefaultTextConfigFromEnv

func SetDefaultTextConfigFromEnv()

SetDefaultTextConfigFromEnv updates DefaultTextConfig based on O_THEME.

func SetKeywords

func SetKeywords(addAndDel ...[]string)

SetKeywords clears, then adds/removes keywords.

Types

type Annotator

type Annotator interface {
	Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)
}

Annotator produces syntax highlighting annotations.

type Kind

type Kind uint8

Kind represents a syntax highlighting kind (class) assigned to tokens.

const (
	Whitespace Kind = iota
	AndOr
	AngleBracket
	AssemblyEnd
	Class
	Comment
	Decimal
	Dollar
	Literal
	Keyword
	Mut
	Plaintext
	Private
	Protected
	Public
	Punctuation
	Self
	Star
	Static
	String
	Tag
	TextAttrName
	TextAttrValue
	TextTag
	Type
)

Supported highlighting kinds.

type Option

type Option func(*TextConfig)

Option is a function that modifies a TextConfig.

type Printer

type Printer interface {
	Print(w io.Writer, kind Kind, tokText string) error
}

Printer renders highlighted output.

type TextAnnotator

type TextAnnotator TextConfig

TextAnnotator wraps TextConfig to implement Annotator.

func (TextAnnotator) Annotate

func (a TextAnnotator) Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)

Annotate returns an annotation for the given token.

type TextConfig

type TextConfig struct {
	AndOr         string
	AngleBracket  string
	AssemblyEnd   string
	Class         string
	Comment       string
	Decimal       string
	Dollar        string
	Keyword       string
	Literal       string
	Mut           string
	Plaintext     string
	Private       string
	Protected     string
	Public        string
	Punctuation   string
	Self          string
	Star          string
	Static        string
	String        string
	Tag           string
	TextAttrName  string
	TextAttrValue string
	TextTag       string
	Type          string
	Whitespace    string
}

TextConfig maps token kinds to color tag names used by vt.TextOutput.DarkTags.

func LightTextConfigByName

func LightTextConfigByName(name string) TextConfig

LightTextConfigByName returns the TextConfig for the given theme name, preferring the light variant when one exists.

func NewDarkBlueEditTextConfig

func NewDarkBlueEditTextConfig() TextConfig

func NewDarkVSTextConfig

func NewDarkVSTextConfig() TextConfig

func NewDefaultTextConfig

func NewDefaultTextConfig() TextConfig

func NewLightBlueEditTextConfig

func NewLightBlueEditTextConfig() TextConfig

func NewLightVSTextConfig

func NewLightVSTextConfig() TextConfig

func NewLitmusTextConfig

func NewLitmusTextConfig() TextConfig

func NewNoColorTextConfig

func NewNoColorTextConfig() TextConfig

func NewOrbTextConfig

func NewOrbTextConfig() TextConfig

func NewPinetreeTextConfig

func NewPinetreeTextConfig() TextConfig

func NewRedBlackTextConfig

func NewRedBlackTextConfig() TextConfig

func NewSynthwaveTextConfig

func NewSynthwaveTextConfig() TextConfig

func NewTealTextConfig

func NewTealTextConfig() TextConfig

func NewZuluTextConfig

func NewZuluTextConfig() TextConfig

func TextConfigByName

func TextConfigByName(name string) TextConfig

TextConfigByName returns the TextConfig for the given theme name. If the name is not recognized, the default TextConfig is returned.

func TextConfigFromEnv

func TextConfigFromEnv() TextConfig

TextConfigFromEnv returns the TextConfig selected by the O_THEME (or THEME) environment variable, falling back to the default. If NO_COLOR is set, an empty TextConfig (no colors) is returned. If O_LIGHT is set, light theme variants are preferred.

func (TextConfig) GetClass

func (c TextConfig) GetClass(kind Kind) string

GetClass returns the color tag name for a given token kind.

type TextPrinter

type TextPrinter TextConfig

TextPrinter wraps TextConfig to implement Printer, emitting color tags.

func (TextPrinter) Print

func (p TextPrinter) Print(w io.Writer, kind Kind, tokText string) error

Print writes token text wrapped in <color>...<off> tags based on its kind.

Directories

Path Synopsis
cmd
hicat command

Jump to

Keyboard shortcuts

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