forked from k0kubun/pp
/
pp.go
124 lines (102 loc) · 2.95 KB
/
pp.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package pp
import (
"errors"
"fmt"
"io"
"os"
"sync"
"github.com/mattn/go-colorable"
)
var (
out io.Writer
outLock sync.Mutex
defaultOut = colorable.NewColorableStdout()
)
func init() {
out = defaultOut
}
// Print prints given arguments.
func Print(a ...interface{}) (n int, err error) {
return fmt.Fprint(out, formatAll(a)...)
}
// Printf prints a given format.
func Printf(format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(out, format, formatAll(a)...)
}
// Println prints given arguments with newline.
func Println(a ...interface{}) (n int, err error) {
return fmt.Fprintln(out, formatAll(a)...)
}
// Sprint formats given arguemnts and returns the result as string.
func Sprint(a ...interface{}) string {
return fmt.Sprint(formatAll(a)...)
}
// Sprintf formats with pretty print and returns the result as string.
func Sprintf(format string, a ...interface{}) string {
return fmt.Sprintf(format, formatAll(a)...)
}
// Sprintln formats given arguemnts with newline and returns the result as string.
func Sprintln(a ...interface{}) string {
return fmt.Sprintln(formatAll(a)...)
}
// Fprint prints given arguments to a given writer.
func Fprint(w io.Writer, a ...interface{}) (n int, err error) {
return fmt.Fprint(w, formatAll(a)...)
}
// Fprintf prints format to a given writer.
func Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(w, format, formatAll(a)...)
}
// Fprintln prints given arguments to a given writer with newline.
func Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
return fmt.Fprintln(w, formatAll(a)...)
}
// Errorf formats given arguments and returns it as error type.
func Errorf(format string, a ...interface{}) error {
return errors.New(Sprintf(format, a...))
}
// Fatal prints given arguments and finishes execution with exit status 1.
func Fatal(a ...interface{}) {
fmt.Fprint(out, formatAll(a)...)
os.Exit(1)
}
// Fatalf prints a given format and finishes execution with exit status 1.
func Fatalf(format string, a ...interface{}) {
fmt.Fprintf(out, format, formatAll(a)...)
os.Exit(1)
}
// Fatalln prints given arguments with newline and finishes execution with exit status 1.
func Fatalln(a ...interface{}) {
fmt.Fprintln(out, formatAll(a)...)
os.Exit(1)
}
// Change Print* functions' output to a given writer.
// For example, you can limit output by ENV.
//
// func init() {
// if os.Getenv("DEBUG") == "" {
// pp.SetDefaultOutput(ioutil.Discard)
// }
// }
func SetDefaultOutput(o io.Writer) {
outLock.Lock()
out = o
outLock.Unlock()
}
// GetDefaultOutput returns pp's default output.
func GetDefaultOutput() io.Writer {
return out
}
// Change Print* functions' output to default one.
func ResetDefaultOutput() {
outLock.Lock()
out = defaultOut
outLock.Unlock()
}
func formatAll(objects []interface{}) []interface{} {
results := []interface{}{}
for _, object := range objects {
results = append(results, format(object))
}
return results
}