color.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "regexp"
  6. "strings"
  7. )
  8. const (
  9. Gray = uint8(iota + 90)
  10. Red
  11. Green
  12. Yellow
  13. Blue
  14. Magenta
  15. Cyan
  16. White
  17. EndColor = "\033[0m"
  18. )
  19. func Color(str string, color uint8) string {
  20. return fmt.Sprintf("%s%s%s", ColorStart(color), str, EndColor)
  21. }
  22. func ColorStart(color uint8) string {
  23. return fmt.Sprintf("\033[%dm", color)
  24. }
  25. func ColorfulRequest(str string) string {
  26. lines := strings.Split(str, "\n")
  27. if printOption&printReqHeader == printReqHeader {
  28. strs := strings.Split(lines[0], " ")
  29. strs[0] = Color(strs[0], Magenta)
  30. strs[1] = Color(strs[1], Cyan)
  31. strs[2] = Color(strs[2], Magenta)
  32. lines[0] = strings.Join(strs, " ")
  33. }
  34. for i, line := range lines[1:] {
  35. substr := strings.Split(line, ":")
  36. if len(substr) < 2 {
  37. continue
  38. }
  39. substr[0] = Color(substr[0], Gray)
  40. substr[1] = Color(strings.Join(substr[1:], ":"), Cyan)
  41. lines[i+1] = strings.Join(substr[:2], ":")
  42. }
  43. return strings.Join(lines, "\n")
  44. }
  45. func ColorfulResponse(str, contenttype string) string {
  46. match, err := regexp.MatchString(contentJsonRegex, contenttype)
  47. if err != nil {
  48. log.Fatalln("failed to compile regex", err)
  49. }
  50. if match {
  51. str = ColorfulJson(str)
  52. } else {
  53. str = ColorfulHTML(str)
  54. }
  55. return str
  56. }
  57. func ColorfulJson(str string) string {
  58. var rsli []rune
  59. var key, val, startcolor, endcolor, startsemicolon bool
  60. var prev rune
  61. for _, char := range []rune(str) {
  62. switch char {
  63. case ' ':
  64. rsli = append(rsli, char)
  65. case '{':
  66. startcolor = true
  67. key = true
  68. val = false
  69. rsli = append(rsli, char)
  70. case '}':
  71. startcolor = false
  72. endcolor = false
  73. key = false
  74. val = false
  75. rsli = append(rsli, char)
  76. case '"':
  77. if startsemicolon && prev == '\\' {
  78. rsli = append(rsli, char)
  79. } else {
  80. if startcolor {
  81. rsli = append(rsli, char)
  82. if key {
  83. rsli = append(rsli, []rune(ColorStart(Magenta))...)
  84. } else if val {
  85. rsli = append(rsli, []rune(ColorStart(Cyan))...)
  86. }
  87. startsemicolon = true
  88. key = false
  89. val = false
  90. startcolor = false
  91. } else {
  92. rsli = append(rsli, []rune(EndColor)...)
  93. rsli = append(rsli, char)
  94. endcolor = true
  95. startsemicolon = false
  96. }
  97. }
  98. case ',':
  99. if !startsemicolon {
  100. startcolor = true
  101. key = true
  102. val = false
  103. if !endcolor {
  104. rsli = append(rsli, []rune(EndColor)...)
  105. endcolor = true
  106. }
  107. }
  108. rsli = append(rsli, char)
  109. case ':':
  110. if !startsemicolon {
  111. key = false
  112. val = true
  113. startcolor = true
  114. if !endcolor {
  115. rsli = append(rsli, []rune(EndColor)...)
  116. endcolor = true
  117. }
  118. }
  119. rsli = append(rsli, char)
  120. case '\n', '\r', '[', ']':
  121. rsli = append(rsli, char)
  122. default:
  123. if !startsemicolon {
  124. if key && startcolor {
  125. rsli = append(rsli, []rune(ColorStart(Magenta))...)
  126. key = false
  127. startcolor = false
  128. endcolor = false
  129. }
  130. if val && startcolor {
  131. rsli = append(rsli, []rune(ColorStart(Cyan))...)
  132. val = false
  133. startcolor = false
  134. endcolor = false
  135. }
  136. }
  137. rsli = append(rsli, char)
  138. }
  139. prev = char
  140. }
  141. return string(rsli)
  142. }
  143. func ColorfulHTML(str string) string {
  144. return Color(str, Green)
  145. }