tty_truecolour.go

 1package formatters
 2
 3import (
 4	"fmt"
 5	"io"
 6	"regexp"
 7
 8	"github.com/alecthomas/chroma/v2"
 9)
10
11// TTY16m is a true-colour terminal formatter.
12var TTY16m = Register("terminal16m", chroma.FormatterFunc(trueColourFormatter))
13
14var crOrCrLf = regexp.MustCompile(`\r?\n`)
15
16// Print the text with the given formatting, resetting the formatting at the end
17// of each line and resuming it on the next line.
18//
19// This way, a pager (like https://github.com/walles/moar for example) can show
20// any line in the output by itself, and it will get the right formatting.
21func writeToken(w io.Writer, formatting string, text string) {
22	if formatting == "" {
23		fmt.Fprint(w, text)
24		return
25	}
26
27	newlineIndices := crOrCrLf.FindAllStringIndex(text, -1)
28
29	afterLastNewline := 0
30	for _, indices := range newlineIndices {
31		newlineStart, afterNewline := indices[0], indices[1]
32		fmt.Fprint(w, formatting)
33		fmt.Fprint(w, text[afterLastNewline:newlineStart])
34		fmt.Fprint(w, "\033[0m")
35		fmt.Fprint(w, text[newlineStart:afterNewline])
36		afterLastNewline = afterNewline
37	}
38
39	if afterLastNewline < len(text) {
40		// Print whatever is left after the last newline
41		fmt.Fprint(w, formatting)
42		fmt.Fprint(w, text[afterLastNewline:])
43		fmt.Fprint(w, "\033[0m")
44	}
45}
46
47func trueColourFormatter(w io.Writer, style *chroma.Style, it chroma.Iterator) error {
48	style = clearBackground(style)
49	for token := it(); token != chroma.EOF; token = it() {
50		entry := style.Get(token.Type)
51		if entry.IsZero() {
52			fmt.Fprint(w, token.Value)
53			continue
54		}
55
56		formatting := ""
57		if entry.Bold == chroma.Yes {
58			formatting += "\033[1m"
59		}
60		if entry.Underline == chroma.Yes {
61			formatting += "\033[4m"
62		}
63		if entry.Italic == chroma.Yes {
64			formatting += "\033[3m"
65		}
66		if entry.Colour.IsSet() {
67			formatting += fmt.Sprintf("\033[38;2;%d;%d;%dm", entry.Colour.Red(), entry.Colour.Green(), entry.Colour.Blue())
68		}
69		if entry.Background.IsSet() {
70			formatting += fmt.Sprintf("\033[48;2;%d;%d;%dm", entry.Background.Red(), entry.Background.Green(), entry.Background.Blue())
71		}
72
73		writeToken(w, formatting, token.Value)
74	}
75	return nil
76}