forked from astaxie/bat
/
color.go
70 lines (58 loc) · 1.29 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
package main
import (
"fmt"
"strings"
"github.com/bingoohuang/jj"
)
const (
Gray = uint8(iota + 90)
_ // Red
Green
Yellow
_ // Blue
Magenta
Cyan
_ // White
EndColor = "\033[0m"
)
var Color = func(str string, color uint8) string {
if hasStdoutDevice {
return fmt.Sprintf("%s%s%s", ColorStart(color), str, EndColor)
}
return str
}
func ColorStart(color uint8) string {
return fmt.Sprintf("\033[%dm", color)
}
func ColorfulRequest(str string) string {
if len(str) == 0 {
return str
}
lines := strings.Split(str, "\n")
if HasPrintOption(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 string, isJSON bool) string {
if isJSON {
return string(jj.Color([]byte(str), nil, &jj.ColorOption{CountEntries: countingItems}))
}
return ColorfulHTML(str)
}
func ColorfulHTML(str string) string {
return Color(str, Green)
}