From 7e2b926431d3898d9d050179a28550f6676eaa40 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Sun, 15 Jun 2025 15:42:22 -0400 Subject: [PATCH] Support all rules in ansi.Colorize(); allow custom colors --- ansi/ansi.go | 64 ++++++++++++++++++++++++++++------------ demo/hello_world_demo.go | 2 +- syntax/1entry.go | 21 +++++++++++++ 3 files changed, 67 insertions(+), 20 deletions(-) diff --git a/ansi/ansi.go b/ansi/ansi.go index 7827a97..eca3128 100644 --- a/ansi/ansi.go +++ b/ansi/ansi.go @@ -2,6 +2,7 @@ package ansi import ( "errors" + "fmt" "strings" "github.com/rwinkhart/go-highlite" @@ -10,7 +11,7 @@ import ( // Colorize takes a string of code and a language ID, and returns the code // with ANSI color codes applied for syntax highlighting. -func Colorize(inputCode, languageID string) (string, error) { +func Colorize(inputCode, languageID string, startingColor uint8) (string, error) { // Load and parse the go syntax layer into a `*highlight.Def` syntaxDef, err := highlite.ParseDef(syntax.Get(languageID)) if err != nil { @@ -31,29 +32,54 @@ func Colorize(inputCode, languageID string) (string, error) { colN := 0 for _, c := range l { if group, ok := matches[lineN][colN]; ok { - var colorCode string + var colorOffset uint8 switch group { - case highlite.Groups["statement"]: - colorCode = "\033[32m" // Green - case highlite.Groups["identifier"]: - colorCode = "\033[94m" // Bright Blue - case highlite.Groups["preproc"]: - colorCode = "\033[91m" // Bright Red - case highlite.Groups["special"]: - colorCode = "\033[31m" // Red - case highlite.Groups["constant.string"], highlite.Groups["constant"], - highlite.Groups["constant.number"]: - colorCode = "\033[36m" // Cyan + case highlite.Groups["comment"]: // TODO static color + colorOffset = 0 + case highlite.Groups["constant"]: + colorOffset = 2 + case highlite.Groups["constant.bool"]: + colorOffset = 4 + case highlite.Groups["constant.number"]: + colorOffset = 6 case highlite.Groups["constant.specialChar"]: - colorCode = "\033[95m" // Bright Magenta + colorOffset = 8 + case highlite.Groups["constant.string"]: + colorOffset = 10 + case highlite.Groups["error"]: // TODO static color + colorOffset = 12 + case highlite.Groups["identifier"]: + colorOffset = 14 + case highlite.Groups["identifier.macro"]: + colorOffset = 16 + case highlite.Groups["identifier.var"]: + colorOffset = 18 + case highlite.Groups["keyword"]: + colorOffset = 20 + case highlite.Groups["preproc"]: + colorOffset = 22 + case highlite.Groups["special"]: + colorOffset = 24 + case highlite.Groups["statement"]: + colorOffset = 26 + case highlite.Groups["symbol"]: + colorOffset = 28 + case highlite.Groups["symbol.brackets"]: + colorOffset = 30 + case highlite.Groups["symbol.operator"]: + colorOffset = 32 + case highlite.Groups["todo"]: + colorOffset = 34 case highlite.Groups["type"]: - colorCode = "\033[33m" // Yellow - case highlite.Groups["comment"]: - colorCode = "\033[92m" // Bright Green + colorOffset = 36 default: - colorCode = "\033[0m" // Reset + colorOffset = 255 + } + if colorOffset == 255 { + outputString.WriteString("\033[0m") // Reset/default color + } else { + outputString.WriteString(fmt.Sprintf("\033[38;5;%dm", startingColor+colorOffset)) } - outputString.WriteString(colorCode) } outputString.WriteRune(c) colN++ diff --git a/demo/hello_world_demo.go b/demo/hello_world_demo.go index ef4e809..fbcd4c6 100644 --- a/demo/hello_world_demo.go +++ b/demo/hello_world_demo.go @@ -128,7 +128,7 @@ echo "Hello, World!"`, for _, example := range helloWorldExamples { fmt.Printf("Language: %s\n\n", example.language) - colorized, err := ansi.Colorize(example.code, example.language) + colorized, err := ansi.Colorize(example.code, example.language, 171) if err != nil { log.Printf("Error colorizing %s: %v\n", example.language, err) fmt.Println("(Unable to highlight this code)") diff --git a/syntax/1entry.go b/syntax/1entry.go index 899b8ba..1e90ab7 100644 --- a/syntax/1entry.go +++ b/syntax/1entry.go @@ -2,6 +2,27 @@ package syntax import "sync" +// Syntax Rules Master List +// comment +// constant +// constant.bool +// constant.number +// constant.specialChar +// constant.string +// error +// identifier +// identifier.macro +// identifier.var +// keyword +// preproc +// special +// statement +// symbol +// symbol.brackets +// symbol.operator +// todo +// type + var syntaxMap = make(map[string]*lazySyntax) type lazySyntax struct {