mirror of
https://github.com/rwinkhart/glamour-temp-MUTN.git
synced 2026-09-06 17:07:15 -04:00
fix: register chroma style once
don't register chroma style if it's already registered this could cause a "concurrent map writes" fatal error when used in a concurrent setting like a bubbletea wish app. use a package-wide mutex to protect registering a new style.
This commit is contained in:
committed by
Christian Muehlhaeuser
parent
9aceb96bb7
commit
a99aac8527
+53
-35
@@ -2,6 +2,7 @@ package ansi
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"io"
|
"io"
|
||||||
|
"sync"
|
||||||
|
|
||||||
"github.com/alecthomas/chroma"
|
"github.com/alecthomas/chroma"
|
||||||
"github.com/alecthomas/chroma/quick"
|
"github.com/alecthomas/chroma/quick"
|
||||||
@@ -9,6 +10,17 @@ import (
|
|||||||
"github.com/muesli/reflow/indent"
|
"github.com/muesli/reflow/indent"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
// The chroma style theme name used for rendering.
|
||||||
|
chromaStyleTheme = "charm"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
// mutex for synchronizing access to the chroma style registry.
|
||||||
|
// Related https://github.com/alecthomas/chroma/pull/650
|
||||||
|
mutex = sync.Mutex{}
|
||||||
|
)
|
||||||
|
|
||||||
// A CodeBlockElement is used to render code blocks.
|
// A CodeBlockElement is used to render code blocks.
|
||||||
type CodeBlockElement struct {
|
type CodeBlockElement struct {
|
||||||
Code string
|
Code string
|
||||||
@@ -64,41 +76,47 @@ func (e *CodeBlockElement) Render(w io.Writer, ctx RenderContext) error {
|
|||||||
theme := rules.Theme
|
theme := rules.Theme
|
||||||
|
|
||||||
if rules.Chroma != nil && ctx.options.ColorProfile > 1 {
|
if rules.Chroma != nil && ctx.options.ColorProfile > 1 {
|
||||||
theme = "charm"
|
theme = chromaStyleTheme
|
||||||
styles.Register(chroma.MustNewStyle("charm",
|
mutex.Lock()
|
||||||
chroma.StyleEntries{
|
// Don't register the style if it's already registered.
|
||||||
chroma.Text: chromaStyle(rules.Chroma.Text),
|
_, ok := styles.Registry[theme]
|
||||||
chroma.Error: chromaStyle(rules.Chroma.Error),
|
if !ok {
|
||||||
chroma.Comment: chromaStyle(rules.Chroma.Comment),
|
styles.Register(chroma.MustNewStyle(theme,
|
||||||
chroma.CommentPreproc: chromaStyle(rules.Chroma.CommentPreproc),
|
chroma.StyleEntries{
|
||||||
chroma.Keyword: chromaStyle(rules.Chroma.Keyword),
|
chroma.Text: chromaStyle(rules.Chroma.Text),
|
||||||
chroma.KeywordReserved: chromaStyle(rules.Chroma.KeywordReserved),
|
chroma.Error: chromaStyle(rules.Chroma.Error),
|
||||||
chroma.KeywordNamespace: chromaStyle(rules.Chroma.KeywordNamespace),
|
chroma.Comment: chromaStyle(rules.Chroma.Comment),
|
||||||
chroma.KeywordType: chromaStyle(rules.Chroma.KeywordType),
|
chroma.CommentPreproc: chromaStyle(rules.Chroma.CommentPreproc),
|
||||||
chroma.Operator: chromaStyle(rules.Chroma.Operator),
|
chroma.Keyword: chromaStyle(rules.Chroma.Keyword),
|
||||||
chroma.Punctuation: chromaStyle(rules.Chroma.Punctuation),
|
chroma.KeywordReserved: chromaStyle(rules.Chroma.KeywordReserved),
|
||||||
chroma.Name: chromaStyle(rules.Chroma.Name),
|
chroma.KeywordNamespace: chromaStyle(rules.Chroma.KeywordNamespace),
|
||||||
chroma.NameBuiltin: chromaStyle(rules.Chroma.NameBuiltin),
|
chroma.KeywordType: chromaStyle(rules.Chroma.KeywordType),
|
||||||
chroma.NameTag: chromaStyle(rules.Chroma.NameTag),
|
chroma.Operator: chromaStyle(rules.Chroma.Operator),
|
||||||
chroma.NameAttribute: chromaStyle(rules.Chroma.NameAttribute),
|
chroma.Punctuation: chromaStyle(rules.Chroma.Punctuation),
|
||||||
chroma.NameClass: chromaStyle(rules.Chroma.NameClass),
|
chroma.Name: chromaStyle(rules.Chroma.Name),
|
||||||
chroma.NameConstant: chromaStyle(rules.Chroma.NameConstant),
|
chroma.NameBuiltin: chromaStyle(rules.Chroma.NameBuiltin),
|
||||||
chroma.NameDecorator: chromaStyle(rules.Chroma.NameDecorator),
|
chroma.NameTag: chromaStyle(rules.Chroma.NameTag),
|
||||||
chroma.NameException: chromaStyle(rules.Chroma.NameException),
|
chroma.NameAttribute: chromaStyle(rules.Chroma.NameAttribute),
|
||||||
chroma.NameFunction: chromaStyle(rules.Chroma.NameFunction),
|
chroma.NameClass: chromaStyle(rules.Chroma.NameClass),
|
||||||
chroma.NameOther: chromaStyle(rules.Chroma.NameOther),
|
chroma.NameConstant: chromaStyle(rules.Chroma.NameConstant),
|
||||||
chroma.Literal: chromaStyle(rules.Chroma.Literal),
|
chroma.NameDecorator: chromaStyle(rules.Chroma.NameDecorator),
|
||||||
chroma.LiteralNumber: chromaStyle(rules.Chroma.LiteralNumber),
|
chroma.NameException: chromaStyle(rules.Chroma.NameException),
|
||||||
chroma.LiteralDate: chromaStyle(rules.Chroma.LiteralDate),
|
chroma.NameFunction: chromaStyle(rules.Chroma.NameFunction),
|
||||||
chroma.LiteralString: chromaStyle(rules.Chroma.LiteralString),
|
chroma.NameOther: chromaStyle(rules.Chroma.NameOther),
|
||||||
chroma.LiteralStringEscape: chromaStyle(rules.Chroma.LiteralStringEscape),
|
chroma.Literal: chromaStyle(rules.Chroma.Literal),
|
||||||
chroma.GenericDeleted: chromaStyle(rules.Chroma.GenericDeleted),
|
chroma.LiteralNumber: chromaStyle(rules.Chroma.LiteralNumber),
|
||||||
chroma.GenericEmph: chromaStyle(rules.Chroma.GenericEmph),
|
chroma.LiteralDate: chromaStyle(rules.Chroma.LiteralDate),
|
||||||
chroma.GenericInserted: chromaStyle(rules.Chroma.GenericInserted),
|
chroma.LiteralString: chromaStyle(rules.Chroma.LiteralString),
|
||||||
chroma.GenericStrong: chromaStyle(rules.Chroma.GenericStrong),
|
chroma.LiteralStringEscape: chromaStyle(rules.Chroma.LiteralStringEscape),
|
||||||
chroma.GenericSubheading: chromaStyle(rules.Chroma.GenericSubheading),
|
chroma.GenericDeleted: chromaStyle(rules.Chroma.GenericDeleted),
|
||||||
chroma.Background: chromaStyle(rules.Chroma.Background),
|
chroma.GenericEmph: chromaStyle(rules.Chroma.GenericEmph),
|
||||||
}))
|
chroma.GenericInserted: chromaStyle(rules.Chroma.GenericInserted),
|
||||||
|
chroma.GenericStrong: chromaStyle(rules.Chroma.GenericStrong),
|
||||||
|
chroma.GenericSubheading: chromaStyle(rules.Chroma.GenericSubheading),
|
||||||
|
chroma.Background: chromaStyle(rules.Chroma.Background),
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
mutex.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) {
|
iw := indent.NewWriterPipe(w, indentation+margin, func(wr io.Writer) {
|
||||||
|
|||||||
Reference in New Issue
Block a user