Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
69 lines (57 sloc) 1.2 KB
package howdoi
import (
"bytes"
"log"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
debug "github.com/visionmedia/go-debug"
)
// ChromaColor struct for colorCode func
type ChromaColor struct {
Color bool
Tags []string
Theme string
}
func colorCode(s string, data *ChromaColor) string {
gLog := debug.Debug("colorCode")
if data.Color == false {
gLog("no need color")
return s
}
res := new(bytes.Buffer)
// lexer
lexer := lexers.Get("bash")
for _, v := range data.Tags {
lexer = lexers.Get(v)
if lexer != nil {
gLog("lexer : %s", v)
break
}
}
if lexer == nil {
gLog("lexer : %s", "noop")
lexer = lexers.Fallback
}
// styles
style := styles.Get(data.Theme)
if style == nil {
style = styles.Fallback
}
gLog("style : %s", style.Name)
// formatter
// [html json noop terminal terminal16m terminal256 tokens]
f := "terminal"
formatter := formatters.Get(f)
if formatter == nil {
formatter = formatters.Fallback
}
gLog("formatter : %s", f)
iterator, err := lexer.Tokenise(nil, s)
// done
err = formatter.Format(res, style, iterator)
if err != nil {
log.Fatalln(err)
}
return res.String()
}