Support all rules in ansi.Colorize(); allow custom colors

This commit is contained in:
2025-06-15 15:42:22 -04:00
parent fb66728b59
commit 7e2b926431
3 changed files with 67 additions and 20 deletions
+45 -19
View File
@@ -2,6 +2,7 @@ package ansi
import ( import (
"errors" "errors"
"fmt"
"strings" "strings"
"github.com/rwinkhart/go-highlite" "github.com/rwinkhart/go-highlite"
@@ -10,7 +11,7 @@ import (
// Colorize takes a string of code and a language ID, and returns the code // Colorize takes a string of code and a language ID, and returns the code
// with ANSI color codes applied for syntax highlighting. // 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` // Load and parse the go syntax layer into a `*highlight.Def`
syntaxDef, err := highlite.ParseDef(syntax.Get(languageID)) syntaxDef, err := highlite.ParseDef(syntax.Get(languageID))
if err != nil { if err != nil {
@@ -31,29 +32,54 @@ func Colorize(inputCode, languageID string) (string, error) {
colN := 0 colN := 0
for _, c := range l { for _, c := range l {
if group, ok := matches[lineN][colN]; ok { if group, ok := matches[lineN][colN]; ok {
var colorCode string var colorOffset uint8
switch group { switch group {
case highlite.Groups["statement"]: case highlite.Groups["comment"]: // TODO static color
colorCode = "\033[32m" // Green colorOffset = 0
case highlite.Groups["identifier"]: case highlite.Groups["constant"]:
colorCode = "\033[94m" // Bright Blue colorOffset = 2
case highlite.Groups["preproc"]: case highlite.Groups["constant.bool"]:
colorCode = "\033[91m" // Bright Red colorOffset = 4
case highlite.Groups["special"]: case highlite.Groups["constant.number"]:
colorCode = "\033[31m" // Red colorOffset = 6
case highlite.Groups["constant.string"], highlite.Groups["constant"],
highlite.Groups["constant.number"]:
colorCode = "\033[36m" // Cyan
case highlite.Groups["constant.specialChar"]: 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"]: case highlite.Groups["type"]:
colorCode = "\033[33m" // Yellow colorOffset = 36
case highlite.Groups["comment"]:
colorCode = "\033[92m" // Bright Green
default: 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) outputString.WriteRune(c)
colN++ colN++
+1 -1
View File
@@ -128,7 +128,7 @@ echo "Hello, World!"`,
for _, example := range helloWorldExamples { for _, example := range helloWorldExamples {
fmt.Printf("Language: %s\n\n", example.language) 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 { if err != nil {
log.Printf("Error colorizing %s: %v\n", example.language, err) log.Printf("Error colorizing %s: %v\n", example.language, err)
fmt.Println("(Unable to highlight this code)") fmt.Println("(Unable to highlight this code)")
+21
View File
@@ -2,6 +2,27 @@ package syntax
import "sync" 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) var syntaxMap = make(map[string]*lazySyntax)
type lazySyntax struct { type lazySyntax struct {