mirror of
https://github.com/alecthomas/chroma.git
synced 2025-02-01 12:57:53 +02:00
54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package formatters
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
|
|
. "github.com/alecthomas/chroma" // nolint
|
|
)
|
|
|
|
var DefaultConsoleTheme = map[TokenType]string{
|
|
Number: "\033[1m\033[33m",
|
|
Comment: "\033[36m",
|
|
CommentPreproc: "\033[1m\033[32m",
|
|
String: "\033[1m\033[36m",
|
|
Keyword: "\033[1m\033[37m",
|
|
GenericHeading: "\033[1m",
|
|
GenericSubheading: "\033[1m",
|
|
GenericStrong: "\033[1m",
|
|
GenericUnderline: "\033[4m",
|
|
GenericDeleted: "\033[9m",
|
|
}
|
|
|
|
// Console formatter.
|
|
//
|
|
// formatter := Console(nil)
|
|
func Console(theme map[TokenType]string) Formatter {
|
|
if theme == nil {
|
|
theme = DefaultConsoleTheme
|
|
}
|
|
return &consoleFormatter{theme}
|
|
}
|
|
|
|
type consoleFormatter struct {
|
|
theme map[TokenType]string
|
|
}
|
|
|
|
func (c *consoleFormatter) Format(w io.Writer) (func(Token), error) {
|
|
return func(token Token) {
|
|
clr, ok := c.theme[token.Type]
|
|
if !ok {
|
|
clr, ok = c.theme[token.Type.SubCategory()]
|
|
if !ok {
|
|
clr, ok = c.theme[token.Type.Category()]
|
|
if !ok {
|
|
clr = ""
|
|
}
|
|
}
|
|
}
|
|
fmt.Fprint(w, clr)
|
|
fmt.Fprint(w, token.Value)
|
|
fmt.Fprintf(w, "\033[0m")
|
|
}, nil
|
|
}
|