-
Notifications
You must be signed in to change notification settings - Fork 3
/
dump.go
109 lines (91 loc) · 2.53 KB
/
dump.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Package dump like fmt.Println but more pretty and beautiful print Go values.
package dump
import (
"bytes"
"io"
"os"
"github.com/gookit/color"
)
// These flags define which print caller information
const (
Fnopos = 1 << iota // no position
Ffunc
Ffile
Ffname
Fline
)
var (
// valid flag for print caller info
callerFlags = []int{Ffunc, Ffile, Ffname, Fline}
// default theme
defaultTheme = Theme{
"caller": "magenta",
"field": "green", // field name color of the map, struct.
"value": "normal",
// special type
"msType": "green", // for keywords map, struct type
"lenTip": "gray", // tips comments for string, slice, map len
"string": "green",
"integer": "lightBlue",
}
// std dumper
std = NewDumper(os.Stdout, 3)
)
// Theme color code/tag map for dump
type Theme map[string]string
func (ct Theme) caller(s string) string { return ct.wrap("caller", s) }
func (ct Theme) field(s string) string { return ct.wrap("field", s) }
func (ct Theme) value(s string) string { return ct.wrap("value", s) }
func (ct Theme) msType(s string) string { return ct.wrap("msType", s) }
func (ct Theme) lenTip(s string) string { return ct.wrap("lenTip", s) }
func (ct Theme) string(s string) string { return ct.wrap("string", s) }
func (ct Theme) integer(s string) string { return ct.wrap("integer", s) }
// wrap color tag.
func (ct Theme) wrap(key string, s string) string {
if tag := ct[key]; tag != "" {
return color.WrapTag(s, tag)
}
return s
}
// Std dumper
func Std() *Dumper {
return std
}
// Reset std dumper
func Reset() {
std = NewDumper(os.Stdout, 3)
}
// Config std dumper
func Config(fn func(opts *Options)) {
std.WithOptions(fn)
}
// V like fmt.Println, but the output is clearer and more beautiful
func V(vs ...interface{}) {
std.Dump(vs...)
}
// P like fmt.Println, but the output is clearer and more beautiful
func P(vs ...interface{}) {
std.Print(vs...)
}
// Print like fmt.Println, but the output is clearer and more beautiful
func Print(vs ...interface{}) {
std.Print(vs...)
}
// Println like fmt.Println, but the output is clearer and more beautiful
func Println(vs ...interface{}) {
std.Println(vs...)
}
// Fprint like fmt.Println, but the output is clearer and more beautiful
func Fprint(w io.Writer, vs ...interface{}) {
std.Fprint(w, vs...)
}
// Format like fmt.Println, but the output is clearer and more beautiful
func Format(vs ...interface{}) string {
w := &bytes.Buffer{}
NewDumper(w, 3).
WithOptions(func(opts *Options) {
opts.ShowFlag = Fnopos
}).
Println(vs...)
return w.String()
}