generated from broadinstitute/golang-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pretty.go
85 lines (72 loc) · 2.2 KB
/
pretty.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package format
import (
"bytes"
"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"github.com/muesli/termenv"
"github.com/rs/zerolog/log"
"io"
)
// Name of the Chroma YAML lexer
const chromaYamlLexer = "YAML"
// Chroma styles for light and dark terminal themes
// see https://swapoff.org/chroma/playground/ for full list
const chromaStyleLight = "friendly"
const chromaStyleDark = "dracula"
func formatPrettyYaml(data interface{}, w io.Writer) error {
return formatPrettyYamlWithOptions(data, w, chooseStyle(), chooseFormatter())
}
func formatPrettyYamlWithOptions(data interface{}, w io.Writer, styleName string, formatterName string) error {
// first, convert to YAML (chroma won't do that for us)
var b bytes.Buffer
if err := formatYaml(data, &b); err != nil {
return err
}
content := b.Bytes()
// following chroma docs
// https://github.com/alecthomas/chroma#formatting-the-output
lexer := lexers.Get(chromaYamlLexer)
if lexer == nil {
log.Warn().Msgf("Couldn't load chroma lexer %q, falling back to plain yaml format", chromaYamlLexer)
return formatYaml(data, w)
}
style := styles.Get(styleName)
if style == nil {
log.Warn().Msgf("Couldn't load chroma style %q, falling back to plain yaml format", styleName)
return formatYaml(data, w)
}
formatter := formatters.Get(formatterName)
if formatter == nil {
log.Warn().Msgf("Couldn't load chroma formatter %q, falling back to plain YAML format", formatterName)
return formatYaml(data, w)
}
lexer = chroma.Coalesce(lexer)
iterator, err := lexer.Tokenise(nil, string(content))
if err != nil {
return err
}
return formatter.Format(w, style, iterator)
}
func chooseFormatter() string {
// available Chroma formatters described here:
// https://pkg.go.dev/github.com/alecthomas/chroma/formatters
switch termenv.ColorProfile() {
case termenv.ANSI:
return "terminal16"
case termenv.ANSI256:
return "terminal256"
case termenv.TrueColor:
return "terminal16m"
default:
return "" // fall back to plain YAML
}
}
func chooseStyle() string {
if termenv.HasDarkBackground() {
return chromaStyleDark
} else {
return chromaStyleLight
}
}