-
Notifications
You must be signed in to change notification settings - Fork 220
/
color.go
154 lines (145 loc) · 3.1 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package main
import (
"fmt"
"log"
"regexp"
"strings"
)
const (
Gray = uint8(iota + 90)
Red
Green
Yellow
Blue
Magenta
Cyan
White
EndColor = "\033[0m"
)
func Color(str string, color uint8) string {
return fmt.Sprintf("%s%s%s", ColorStart(color), str, EndColor)
}
func ColorStart(color uint8) string {
return fmt.Sprintf("\033[%dm", color)
}
func ColorfulRequest(str string) string {
lines := strings.Split(str, "\n")
if printOption&printReqHeader == printReqHeader {
strs := strings.Split(lines[0], " ")
strs[0] = Color(strs[0], Magenta)
strs[1] = Color(strs[1], Cyan)
strs[2] = Color(strs[2], Magenta)
lines[0] = strings.Join(strs, " ")
}
for i, line := range lines[1:] {
substr := strings.Split(line, ":")
if len(substr) < 2 {
continue
}
substr[0] = Color(substr[0], Gray)
substr[1] = Color(strings.Join(substr[1:], ":"), Cyan)
lines[i+1] = strings.Join(substr[:2], ":")
}
return strings.Join(lines, "\n")
}
func ColorfulResponse(str, contenttype string) string {
match, err := regexp.MatchString(contentJsonRegex, contenttype)
if err != nil {
log.Fatalln("failed to compile regex", err)
}
if match {
str = ColorfulJson(str)
} else {
str = ColorfulHTML(str)
}
return str
}
func ColorfulJson(str string) string {
var rsli []rune
var key, val, startcolor, endcolor, startsemicolon bool
var prev rune
for _, char := range []rune(str) {
switch char {
case ' ':
rsli = append(rsli, char)
case '{':
startcolor = true
key = true
val = false
rsli = append(rsli, char)
case '}':
startcolor = false
endcolor = false
key = false
val = false
rsli = append(rsli, char)
case '"':
if startsemicolon && prev == '\\' {
rsli = append(rsli, char)
} else {
if startcolor {
rsli = append(rsli, char)
if key {
rsli = append(rsli, []rune(ColorStart(Magenta))...)
} else if val {
rsli = append(rsli, []rune(ColorStart(Cyan))...)
}
startsemicolon = true
key = false
val = false
startcolor = false
} else {
rsli = append(rsli, []rune(EndColor)...)
rsli = append(rsli, char)
endcolor = true
startsemicolon = false
}
}
case ',':
if !startsemicolon {
startcolor = true
key = true
val = false
if !endcolor {
rsli = append(rsli, []rune(EndColor)...)
endcolor = true
}
}
rsli = append(rsli, char)
case ':':
if !startsemicolon {
key = false
val = true
startcolor = true
if !endcolor {
rsli = append(rsli, []rune(EndColor)...)
endcolor = true
}
}
rsli = append(rsli, char)
case '\n', '\r', '[', ']':
rsli = append(rsli, char)
default:
if !startsemicolon {
if key && startcolor {
rsli = append(rsli, []rune(ColorStart(Magenta))...)
key = false
startcolor = false
endcolor = false
}
if val && startcolor {
rsli = append(rsli, []rune(ColorStart(Cyan))...)
val = false
startcolor = false
endcolor = false
}
}
rsli = append(rsli, char)
}
prev = char
}
return string(rsli)
}
func ColorfulHTML(str string) string {
return Color(str, Green)
}