Gim/internal/theme/theme.go
Hayden Hargreaves 76fa55440e feat: syntax highlighting powered by chroma.
This is sick! It is not perfect, highlight groups would be cool, but for
now this works!
2026-03-16 22:48:57 -07:00

44 lines
929 B
Go

package theme
import (
"embed"
"fmt"
"github.com/alecthomas/chroma/v2"
"github.com/alecthomas/chroma/v2/styles"
)
//go:embed themes/*
var themeFS embed.FS
// RegisterAll: Registers all XML theme files embedded in the themes/ directory
// with chroma's style registry. After calling this, styles.Get() will recognize
// any theme defined in those files.
func RegisterAll() error {
entries, err := themeFS.ReadDir("themes")
if err != nil {
return fmt.Errorf("failed to read embedded themes directory: %w", err)
}
for _, entry := range entries {
if entry.IsDir() {
continue
}
f, err := themeFS.Open("themes/" + entry.Name())
if err != nil {
return fmt.Errorf("failed to open theme %s: %w", entry.Name(), err)
}
style, err := chroma.NewXMLStyle(f)
f.Close()
if err != nil {
return fmt.Errorf("failed to parse theme %s: %w", entry.Name(), err)
}
styles.Register(style)
}
return nil
}