-
Notifications
You must be signed in to change notification settings - Fork 0
/
utilities.go
198 lines (166 loc) · 4.15 KB
/
utilities.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package jzon
import (
"fmt"
"os"
"strings"
)
// *nix TTY colors
const (
BLACK = "\033[0;30m"
RED = "\033[0;31m"
GREEN = "\033[0;32m"
YELLOW = "\033[0;33m"
BLUE = "\033[0;34m"
PURPLE = "\033[0;35m"
SKY = "\033[0;36m"
WHITE = "\033[0;37m"
RESET = "\033[0m"
HIGHLIGHT = "\033[1m"
UNDERLINE = "\033[4m"
BLINK = "\033[5m"
REVERSE = "\033[7m"
FADEOUT = "\033[8m"
)
// Format converts raw compact JSON to human-reading text
func Format(compact string) (formatted string, err error) {
jz, err := Parse([]byte(compact))
if err != nil {
return
}
return jz.Format(0, 2), nil
}
// Compact converts formatted JSON to compact text
func Compact(formatted string) (compact string, err error) {
jz, err := Parse([]byte(formatted))
if err != nil {
return
}
return jz.Compact(), nil
}
// Format generates human-readable text
func (jz *Jzon) Format(indent int, step int) string {
return jz.render(indent, step, false, false)
}
// Compact generates compact text
func (jz *Jzon) Compact() string {
switch jz.Type {
case JzTypeArr:
var ss []string
as, _ := jz.AMap(func(v *Jzon) Any { return v.Compact() })
for _, a := range as {
ss = append(ss, a.(string))
}
return "[" + strings.Join(ss, ",") + "]"
case JzTypeObj:
var ss []string
as, _ := jz.OMap(func(k string, v *Jzon) Any { return "\"" + k + "\"" + ":" + v.Compact() })
for _, a := range as {
ss = append(ss, a.(string))
}
return "{" + strings.Join(ss, ",") + "}"
case JzTypeStr: // FIXME: escaped characters
s, _ := jz.String()
var buf []byte
for _, ch := range []byte(s) {
switch ch {
case '\n':
buf = append(buf, '\\', 'n')
case '\b':
buf = append(buf, '\\', 'b')
case '\f':
buf = append(buf, '\\', 'f')
case '\t':
buf = append(buf, '\\', 't')
case '\r':
buf = append(buf, '\\', 'r')
case '\\':
buf = append(buf, '\\', '\\')
case '"':
buf = append(buf, '\\', '"')
case '/':
buf = append(buf, '\\', '/')
default:
buf = append(buf, byte(ch))
}
}
return "\"" + string(buf) + "\""
case JzTypeInt:
n, _ := jz.Integer()
return fmt.Sprintf("%d", n)
case JzTypeFlt:
f, _ := jz.Float()
return fmt.Sprintf("%f", f)
case JzTypeBol:
b, _ := jz.Bool()
if b {
return "true"
}
return "false"
case JzTypeNul:
return "null"
}
return ""
}
// GoString implements the `GoString` interface
func (jz *Jzon) GoString() string {
return jz.Compact();
}
// Formatter implements the `Formmater` interface
func (jz *Jzon) Formmater(f fmt.State, c rune) {
}
// Print prints human-reading JSON text to writer
func (jz *Jzon) Print() {
fmt.Print(jz.Compact())
}
// Coloring prints colored and formatted JSON text on the terminal. if it's not
// a terminal or doesn't support colors, it just prints raw but formatted text
func (jz *Jzon) Coloring(file *os.File) {
if file != os.Stdout {
fmt.Fprint(file, jz.Format(0, 2))
}
fmt.Fprint(file, jz.render(0, 2, false, true))
}
func (jz *Jzon) render(indent int, step int, useTab bool, useColor bool) string {
colorify := func(c string, s string) string {
if useColor {
return c + s + RESET
}
return s
}
indentf := func(i, s int) string {
if useTab {
return strings.Repeat("\t", i/s)
}
return strings.Repeat(" ", i)
}
switch jz.Type {
case JzTypeArr:
var ss []string
vs, _ := jz.AMap(func(v *Jzon) Any { return v.render(indent+step, step, useTab, useColor) })
for _, v := range vs {
ss = append(ss, v.(string))
}
return "[" + strings.Join(ss, ", ") + "]"
case JzTypeObj:
if l, _ := jz.Length(); l == 0 {
return "{}"
}
vs, _ := jz.OMap(func(k string, v *Jzon) Any {
return indentf(indent+step, step) + YELLOW + "\"" + k + "\": " + RESET + v.render(indent+step, step, useTab, useColor)
})
var ss []string
for _, v := range vs {
ss = append(ss, v.(string))
}
return "{\n" + strings.Join(ss, ",\n") + "\n" + indentf(indent, step) + "}"
case JzTypeNul:
return colorify(RED, jz.Compact())
case JzTypeBol:
return colorify(GREEN, jz.Compact())
case JzTypeFlt, JzTypeInt:
return colorify(BLUE, jz.Compact())
case JzTypeStr:
return colorify(PURPLE, jz.Compact())
}
return ""
}