2017-06-06 15:59:48 +10:00
|
|
|
package formatters
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/alecthomas/chroma"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TTY16m is a true-colour terminal formatter.
|
2017-06-06 21:23:50 +10:00
|
|
|
var TTY16m = Register("terminal16m", chroma.FormatterFunc(trueColourFormatter))
|
2017-06-06 15:59:48 +10:00
|
|
|
|
2017-09-20 22:19:36 +10:00
|
|
|
func trueColourFormatter(w io.Writer, style *chroma.Style, it chroma.Iterator) error {
|
|
|
|
for token := it(); token != nil; token = it() {
|
2017-06-06 15:59:48 +10:00
|
|
|
entry := style.Get(token.Type)
|
|
|
|
if !entry.IsZero() {
|
|
|
|
out := ""
|
2017-09-23 21:55:56 +10:00
|
|
|
if entry.Bold == chroma.Yes {
|
2017-06-06 15:59:48 +10:00
|
|
|
out += "\033[1m"
|
|
|
|
}
|
2017-09-23 21:55:56 +10:00
|
|
|
if entry.Underline == chroma.Yes {
|
2017-06-06 15:59:48 +10:00
|
|
|
out += "\033[4m"
|
|
|
|
}
|
|
|
|
if entry.Colour.IsSet() {
|
|
|
|
out += fmt.Sprintf("\033[38:2:%d:%d:%dm", entry.Colour.Red(), entry.Colour.Green(), entry.Colour.Blue())
|
|
|
|
}
|
|
|
|
if entry.Background.IsSet() {
|
|
|
|
out += fmt.Sprintf("\033[48:2:%d:%d:%dm", entry.Background.Red(), entry.Background.Green(), entry.Background.Blue())
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, out)
|
|
|
|
}
|
|
|
|
fmt.Fprint(w, token.Value)
|
|
|
|
if !entry.IsZero() {
|
|
|
|
fmt.Fprint(w, "\033[0m")
|
|
|
|
}
|
2017-09-20 22:19:36 +10:00
|
|
|
}
|
|
|
|
return nil
|
2017-06-06 15:59:48 +10:00
|
|
|
}
|