Skip to content

Commit 234552e

Browse files
committed
support colorful
1 parent 2deab52 commit 234552e

File tree

2 files changed

+28
-4
lines changed

2 files changed

+28
-4
lines changed

bat.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,11 +146,11 @@ func main() {
146146
}
147147
if fi.Mode()&os.ModeDevice == os.ModeDevice {
148148
dump := httpreq.DumpRequest()
149-
fmt.Println(string(dump))
149+
fmt.Println(ColorfulRequest(string(dump)))
150150
fmt.Println("")
151-
fmt.Println(Color(res.Proto, Red), Color(res.Status, Green))
151+
fmt.Println(Color(res.Proto, Magenta), Color(res.Status, Green))
152152
for k, v := range res.Header {
153-
fmt.Println(k, ":", strings.Join(v, " "))
153+
fmt.Println(Color(k, Gray), ":", Color(strings.Join(v, " "), Cyan))
154154
}
155155
str, err := httpreq.String()
156156
if err != nil {
@@ -169,7 +169,7 @@ func main() {
169169
}
170170
str = string(bstr)
171171
}
172-
fmt.Println(str)
172+
fmt.Println(ColorfulResponse(str))
173173
} else {
174174
str, err := httpreq.String()
175175
if err != nil {

color.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"fmt"
5+
"strings"
56
)
67

78
const (
@@ -20,3 +21,26 @@ const (
2021
func Color(str string, color uint8) string {
2122
return fmt.Sprintf("\033[%dm%s%s", color, str, EndColor)
2223
}
24+
25+
func ColorfulRequest(str string) string {
26+
lines := strings.Split(str, "\n")
27+
strs := strings.Split(lines[0], " ")
28+
strs[0] = Color(strs[0], Magenta)
29+
strs[1] = Color(strs[1], Cyan)
30+
strs[2] = Color(strs[2], Magenta)
31+
lines[0] = strings.Join(strs, " ")
32+
for i, line := range lines[1:] {
33+
substr := strings.Split(line, ":")
34+
if len(substr) != 2 {
35+
continue
36+
}
37+
substr[0] = Color(substr[0], Gray)
38+
substr[1] = Color(substr[1], Cyan)
39+
lines[i+1] = strings.Join(substr, ":")
40+
}
41+
return strings.Join(lines, "\n")
42+
}
43+
44+
func ColorfulResponse(str string) string {
45+
return Color(str, Green)
46+
}

0 commit comments

Comments
 (0)