2017-06-02 00:17:21 +10:00
|
|
|
package formatters
|
|
|
|
|
|
|
|
import (
|
2017-06-02 11:42:52 +10:00
|
|
|
"fmt"
|
2017-06-02 00:17:21 +10:00
|
|
|
"io"
|
|
|
|
|
|
|
|
. "github.com/alecthomas/chroma" // nolint
|
|
|
|
)
|
|
|
|
|
|
|
|
var DefaultConsoleTheme = map[TokenType]string{
|
2017-06-02 11:42:52 +10:00
|
|
|
Number: "\033[1m\033[33m",
|
|
|
|
Comment: "\033[36m",
|
2017-06-04 22:18:35 +10:00
|
|
|
CommentPreproc: "\033[1m\033[32m",
|
2017-06-02 11:42:52 +10:00
|
|
|
String: "\033[1m\033[36m",
|
|
|
|
Keyword: "\033[1m\033[37m",
|
|
|
|
GenericHeading: "\033[1m",
|
|
|
|
GenericSubheading: "\033[1m",
|
2017-06-04 22:18:35 +10:00
|
|
|
GenericStrong: "\033[1m",
|
|
|
|
GenericUnderline: "\033[4m",
|
|
|
|
GenericDeleted: "\033[9m",
|
2017-06-02 00:17:21 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
// Console formatter.
|
|
|
|
//
|
2017-06-04 22:18:35 +10:00
|
|
|
// formatter := Console(nil)
|
2017-06-02 00:17:21 +10:00
|
|
|
func Console(theme map[TokenType]string) Formatter {
|
2017-06-04 22:18:35 +10:00
|
|
|
if theme == nil {
|
|
|
|
theme = DefaultConsoleTheme
|
|
|
|
}
|
2017-06-02 00:17:21 +10:00
|
|
|
return &consoleFormatter{theme}
|
|
|
|
}
|
|
|
|
|
|
|
|
type consoleFormatter struct {
|
|
|
|
theme map[TokenType]string
|
|
|
|
}
|
|
|
|
|
2017-06-02 15:15:15 +10:00
|
|
|
func (c *consoleFormatter) Format(w io.Writer) (func(Token), error) {
|
|
|
|
return func(token Token) {
|
2017-06-02 00:17:21 +10:00
|
|
|
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 {
|
2017-06-04 22:18:35 +10:00
|
|
|
clr = ""
|
2017-06-02 00:17:21 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-06-02 11:42:52 +10:00
|
|
|
fmt.Fprint(w, clr)
|
|
|
|
fmt.Fprint(w, token.Value)
|
2017-06-04 22:18:35 +10:00
|
|
|
fmt.Fprintf(w, "\033[0m")
|
2017-06-02 15:15:15 +10:00
|
|
|
}, nil
|
2017-06-02 00:17:21 +10:00
|
|
|
}
|