-
Notifications
You must be signed in to change notification settings - Fork 0
/
color.go
140 lines (122 loc) · 2.99 KB
/
color.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package qjson
import (
"fmt"
"strings"
"github.com/fatih/color"
)
var (
colorFuncs = []func(a ...interface{}) string{
func(e ...interface{}) string {
var s string
for _, v := range e {
s += fmt.Sprint(v)
}
return s
},
color.New(color.FgYellow, color.Bold).SprintFunc(),
color.New(color.FgCyan, color.Bold).SprintFunc(),
color.New(color.FgGreen, color.Bold).SprintFunc(),
color.New(color.FgMagenta, color.Bold).SprintFunc(),
color.New(color.FgBlue, color.Bold).SprintFunc(),
color.New(color.FgRed, color.Bold).SprintFunc(),
color.New(color.FgWhite, color.BgBlack, color.Bold).SprintFunc(),
color.New(color.FgBlack, color.BgWhite, color.Bold).SprintFunc(),
}
)
// Formatter json with indent
type Formatter struct {
Indent int
}
// NewFormatter returns a new formatter with following default values.
func NewFormatter() *Formatter {
return &Formatter{
Indent: 2,
}
}
// Format JSONTree
func (f *Formatter) Format(v *JSONTree) []byte {
if v == nil || v.Root == nil {
return nil
}
s := f.pretty(v.Root, 1)
return []byte(s)
}
func (f *Formatter) pretty(node *Node, depth int) string {
if node == nil {
return ""
}
fn := f.getColorFuncByDepth(depth)
switch node.Type {
case String, Bool, Float, Integer:
return fn(node.Value)
case Null:
return fn(nullVal)
case Object:
return f.processMap(node, depth)
case Array:
return f.processArray(node, depth)
}
return ""
}
func (f *Formatter) processMap(m *Node, depth int) string {
if m == nil {
return ""
}
currentIndent := f.generateIndent(depth - 1)
nextIndent := f.generateIndent(depth)
rows := []string{}
if len(m.ObjectValues) == 0 {
return "{}"
}
fn := f.getColorFuncByDepth(depth)
for _, elem := range m.ObjectValues {
k := fn(elem.Key.Value)
v := f.pretty(elem.Value, depth+1)
var row string
if f.isNoIndent() {
row = fmt.Sprintf("%s:%s", k, v)
} else {
row = fmt.Sprintf("%s%s: %s", nextIndent, k, v)
}
rows = append(rows, row)
}
if f.isNoIndent() {
return fmt.Sprintf("{%s}", strings.Join(rows, ","))
}
return fmt.Sprintf("{\n%s\n%s}", strings.Join(rows, ",\n"), currentIndent)
}
func (f *Formatter) processArray(a *Node, depth int) string {
if a == nil {
return ""
}
currentIndent := f.generateIndent(depth - 1)
nextIndent := f.generateIndent(depth)
rows := []string{}
if len(a.ArrayValues) == 0 {
return "[]"
}
for _, val := range a.ArrayValues {
c := f.pretty(val, depth+1)
var row string
if f.isNoIndent() {
row = c
} else {
row = nextIndent + c
}
rows = append(rows, row)
}
if f.isNoIndent() {
return fmt.Sprintf("[%s]", strings.Join(rows, ","))
}
return fmt.Sprintf("[\n%s\n%s]", strings.Join(rows, ",\n"), currentIndent)
}
func (f *Formatter) generateIndent(depth int) string {
return strings.Join(make([]string, f.Indent*depth+1), " ")
}
func (f *Formatter) isNoIndent() bool {
return f.Indent == 0
}
func (f *Formatter) getColorFuncByDepth(depth int) func(...interface{}) string {
idx := depth % len(colorFuncs)
return colorFuncs[idx]
}