forked from exoscale/egoscale
/
printjson.go
74 lines (62 loc) · 1.27 KB
/
printjson.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
// +build linux darwin openbsd freebsd
package main
import (
"fmt"
"io"
"log"
"os"
"syscall"
"github.com/alecthomas/chroma"
"github.com/alecthomas/chroma/formatters"
"github.com/alecthomas/chroma/lexers"
"github.com/alecthomas/chroma/styles"
"golang.org/x/crypto/ssh/terminal"
)
const nocolorsTheme = "nocolors"
func highlight(w io.Writer, source, style string) error {
l := lexers.Get("json")
if l == nil {
l = lexers.Fallback
}
l = chroma.Coalesce(l)
f := formatters.Get("terminal16m")
if f == nil {
f = formatters.Fallback
}
s := styles.Get(style)
if s == nil {
s = styles.Fallback
}
sb := chroma.NewStyleBuilder("my" + style)
for _, tt := range s.Types() {
se := s.Get(tt)
se.Background = 0
if style == nocolorsTheme {
se.Colour = 0
}
sb.AddEntry(tt, se)
}
s, err := sb.Build()
if err != nil {
return err
}
it, err := l.Tokenise(nil, source)
if err != nil {
return err
}
return f.Format(w, s, it)
}
func printJSON(out, theme string) {
if terminal.IsTerminal(syscall.Stdout) {
if err := highlight(os.Stdout, out, theme); err != nil {
log.Fatal(err)
}
if _, err := fmt.Fprintln(os.Stdout, ""); err != nil {
log.Fatal(err)
}
} else {
if _, err := fmt.Fprintln(os.Stdout, out); err != nil {
log.Fatal(err)
}
}
}