Skip to content

Commit 4e12282

Browse files
committed
support json response colorful fix astaxie#12
1 parent c5cf828 commit 4e12282

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

bat.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ func main() {
192192
}
193193
if printV == "A" || printV == "b" {
194194
body := formatResponseBody(res, httpreq, pretty)
195-
fmt.Println(ColorfulResponse(body))
195+
fmt.Println(ColorfulResponse(body, res.Header.Get("Content-Type")))
196196
}
197197
} else {
198198
body := formatResponseBody(res, httpreq, pretty)

color.go

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@ const (
1919
)
2020

2121
func Color(str string, color uint8) string {
22-
return fmt.Sprintf("\033[%dm%s%s", color, str, EndColor)
22+
return fmt.Sprintf("%s%s%s", ColorStart(color), str, EndColor)
23+
}
24+
25+
func ColorStart(color uint8) string {
26+
return fmt.Sprintf("\033[%dm", color)
2327
}
2428

2529
func ColorfulRequest(str string) string {
@@ -43,6 +47,96 @@ func ColorfulRequest(str string) string {
4347
return strings.Join(lines, "\n")
4448
}
4549

46-
func ColorfulResponse(str string) string {
50+
func ColorfulResponse(str, contenttype string) string {
51+
if strings.Contains(contenttype, contentJsonRegex) {
52+
str = ColorfulJson(str)
53+
} else {
54+
str = ColorfulHTML(str)
55+
}
56+
return str
57+
}
58+
59+
func ColorfulJson(str string) string {
60+
var rsli []rune
61+
var key, val, startcolor, endcolor, startsemicolon bool
62+
for _, char := range []rune(str) {
63+
switch char {
64+
case ' ':
65+
rsli = append(rsli, char)
66+
case '{':
67+
startcolor = true
68+
key = true
69+
val = false
70+
rsli = append(rsli, char)
71+
case '}':
72+
startcolor = false
73+
endcolor = false
74+
key = false
75+
val = false
76+
rsli = append(rsli, char)
77+
case '"':
78+
if startcolor {
79+
rsli = append(rsli, char)
80+
if key {
81+
rsli = append(rsli, []rune(ColorStart(Magenta))...)
82+
} else if val {
83+
rsli = append(rsli, []rune(ColorStart(Cyan))...)
84+
}
85+
startsemicolon = true
86+
key = false
87+
val = false
88+
startcolor = false
89+
} else {
90+
rsli = append(rsli, []rune(EndColor)...)
91+
rsli = append(rsli, char)
92+
endcolor = true
93+
startsemicolon = false
94+
}
95+
case ',':
96+
if !startsemicolon {
97+
startcolor = true
98+
key = true
99+
val = false
100+
if !endcolor {
101+
rsli = append(rsli, []rune(EndColor)...)
102+
endcolor = true
103+
}
104+
}
105+
rsli = append(rsli, char)
106+
case ':':
107+
if !startsemicolon {
108+
key = false
109+
val = true
110+
startcolor = true
111+
if !endcolor {
112+
rsli = append(rsli, []rune(EndColor)...)
113+
endcolor = true
114+
}
115+
}
116+
rsli = append(rsli, char)
117+
case '\n', '\r', '[', ']':
118+
rsli = append(rsli, char)
119+
default:
120+
if !startsemicolon {
121+
if key && startcolor {
122+
rsli = append(rsli, []rune(ColorStart(Magenta))...)
123+
key = false
124+
startcolor = false
125+
endcolor = false
126+
}
127+
if val && startcolor {
128+
rsli = append(rsli, []rune(ColorStart(Cyan))...)
129+
val = false
130+
startcolor = false
131+
endcolor = false
132+
}
133+
}
134+
rsli = append(rsli, char)
135+
}
136+
}
137+
return string(rsli)
138+
}
139+
140+
func ColorfulHTML(str string) string {
47141
return Color(str, Green)
48142
}

0 commit comments

Comments
 (0)