...

Source file src/github.com/alecthomas/chroma/v2/styles/api.go

Documentation: github.com/alecthomas/chroma/v2/styles

     1  package styles
     2  
     3  import (
     4  	"embed"
     5  	"io/fs"
     6  	"sort"
     7  
     8  	"github.com/alecthomas/chroma/v2"
     9  )
    10  
    11  //go:embed *.xml
    12  var embedded embed.FS
    13  
    14  // Registry of Styles.
    15  var Registry = func() map[string]*chroma.Style {
    16  	registry := map[string]*chroma.Style{}
    17  	// Register all embedded styles.
    18  	files, err := fs.ReadDir(embedded, ".")
    19  	if err != nil {
    20  		panic(err)
    21  	}
    22  	for _, file := range files {
    23  		if file.IsDir() {
    24  			continue
    25  		}
    26  		r, err := embedded.Open(file.Name())
    27  		if err != nil {
    28  			panic(err)
    29  		}
    30  		style, err := chroma.NewXMLStyle(r)
    31  		if err != nil {
    32  			panic(err)
    33  		}
    34  		registry[style.Name] = style
    35  		_ = r.Close()
    36  	}
    37  	return registry
    38  }()
    39  
    40  // Fallback style. Reassign to change the default fallback style.
    41  var Fallback = Registry["swapoff"]
    42  
    43  // Register a chroma.Style.
    44  func Register(style *chroma.Style) *chroma.Style {
    45  	Registry[style.Name] = style
    46  	return style
    47  }
    48  
    49  // Names of all available styles.
    50  func Names() []string {
    51  	out := []string{}
    52  	for name := range Registry {
    53  		out = append(out, name)
    54  	}
    55  	sort.Strings(out)
    56  	return out
    57  }
    58  
    59  // Get named style, or Fallback.
    60  func Get(name string) *chroma.Style {
    61  	if style, ok := Registry[name]; ok {
    62  		return style
    63  	}
    64  	return Fallback
    65  }
    66  

View as plain text