1
0
mirror of https://github.com/alecthomas/chroma.git synced 2025-01-30 03:38:02 +02:00
chroma/formatters/console.go

47 lines
942 B
Go

package formatters
import (
"fmt"
"io"
. "github.com/alecthomas/chroma" // nolint
)
var DefaultConsoleTheme = map[TokenType]string{
Number: "\033[1m\033[33m",
Comment: "\033[36m",
String: "\033[1m\033[36m",
Keyword: "\033[1m\033[37m",
GenericHeading: "\033[1m",
GenericSubheading: "\033[1m",
}
// Console formatter.
//
// formatter := Console(DefaultConsoleTheme)
func Console(theme map[TokenType]string) Formatter {
return &consoleFormatter{theme}
}
type consoleFormatter struct {
theme map[TokenType]string
}
func (c *consoleFormatter) Format(w io.Writer, tokens []Token) error {
for _, token := range tokens {
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 = "\033[0m"
}
}
}
fmt.Fprint(w, clr)
fmt.Fprint(w, token.Value)
}
return nil
}