123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- 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)
- }
|