File: pp.go

package info (click to toggle)
golang-github-k0kubun-pp 2.3.0-4.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 96 kB
  • sloc: makefile: 2
file content (148 lines) | stat: -rw-r--r-- 3,687 bytes parent folder | download | duplicates (2)
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package pp

import (
	"errors"
	"fmt"
	"io"
	"os"
	"runtime"
	"sync"

	"github.com/mattn/go-colorable"
)

var (
	out     io.Writer
	outLock sync.Mutex

	defaultOut = colorable.NewColorableStdout()

	currentScheme ColorScheme
	// WithLineInfo add file name and line information to output
	// call this function with care, because getting stack has performance penalty
	WithLineInfo = false
)

func init() {
	out = defaultOut
	currentScheme = defaultScheme
}

// 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()
}

// SetColorScheme takes a colorscheme used by all future Print calls.
func SetColorScheme(scheme ColorScheme) {
	scheme.fixColors()
	currentScheme = scheme
}

// ResetColorScheme resets colorscheme to default.
func ResetColorScheme() {
	currentScheme = defaultScheme
}

func formatAll(objects []interface{}) []interface{} {
	results := []interface{}{}

	if WithLineInfo {
		_, fn, line, _ := runtime.Caller(2) // 2 because current Caller is pp itself
		results = append(results, fmt.Sprintf("%s:%d\n", fn, line))
	}

	for _, object := range objects {
		results = append(results, format(object))
	}
	return results
}