mirror of
https://github.com/alecthomas/chroma.git
synced 2025-01-14 02:23:16 +02:00
cc0e4a59ab
This is to solve an issue where writers returned by the Formatter were often stateful, but this fact was not obvious to the API consumer, and failed in interesting ways.
39 lines
935 B
Go
39 lines
935 B
Go
package formatters
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
|
|
"github.com/alecthomas/chroma"
|
|
)
|
|
|
|
// TTY16m is a true-colour terminal formatter.
|
|
var TTY16m = Register("terminal16m", chroma.FormatterFunc(trueColourFormatter))
|
|
|
|
func trueColourFormatter(w io.Writer, style *chroma.Style, it chroma.Iterator) error {
|
|
for token := it(); token != nil; token = it() {
|
|
entry := style.Get(token.Type)
|
|
if !entry.IsZero() {
|
|
out := ""
|
|
if entry.Bold {
|
|
out += "\033[1m"
|
|
}
|
|
if entry.Underline {
|
|
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")
|
|
}
|
|
}
|
|
return nil
|
|
}
|