代码拉取完成,页面将自动刷新
package main
import (
"fmt"
"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 printV == "A" || printV == "H" {
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 {
if strings.Contains(contenttype, contentJsonRegex) {
str = ColorfulJson(str)
} else {
str = ColorfulHTML(str)
}
return str
}
func ColorfulJson(str string) string {
var rsli []rune
var key, val, startcolor, endcolor, startsemicolon bool
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 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)
}
}
return string(rsli)
}
func ColorfulHTML(str string) string {
return Color(str, Green)
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。