-
Notifications
You must be signed in to change notification settings - Fork 8
/
error_color.go
61 lines (50 loc) · 2.05 KB
/
error_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
package forest
import (
"fmt"
"os"
"testing"
"github.com/wsxiaoys/terminal/color"
)
// TerminalColorsEnabled can be changed to disable the use of terminal coloring.
// One usecase is to add a command line flag to your test that controls its value.
//
// func init() {
// flag.BoolVar(&forest.TerminalColorsEnabled, "color", true, "want colors?")
// }
//
// go test -color=false
var TerminalColorsEnabled = true
// Check for presence of the TERMCOLORS environment variable to set the TerminalColorsEnabled setting.
func init() {
TerminalColorsEnabled = os.Getenv("TERMCOLORS") != "false"
}
// ErrorColorSyntaxCode requires the syntax defined on https://github.com/wsxiaoys/terminal/blob/master/color/color.go .
// Set to an empty string to disable coloring.
var ErrorColorSyntaxCode = "@{wR}"
// FatalColorSyntaxCode requires the syntax defined on https://github.com/wsxiaoys/terminal/blob/master/color/color.go .
// Set to an empty string to disable coloring.
var FatalColorSyntaxCode = "@{wR}"
func serrorf(format string, args ...interface{}) string {
return Scolorf(ErrorColorSyntaxCode, format, args...)
}
func sfatalf(format string, args ...interface{}) string {
return Scolorf(FatalColorSyntaxCode, format, args...)
}
// Scolorf returns a string colorized for terminal output using the syntaxCode (unless that's empty).
// Requires the syntax defined on https://github.com/wsxiaoys/terminal/blob/master/color/color.go .
func Scolorf(syntaxCode string, format string, args ...interface{}) string {
plainFormatted := fmt.Sprintf(format, args...)
if len(syntaxCode) > 0 && TerminalColorsEnabled {
// cannot pass the code as a string param
return color.Sprintf(syntaxCode+"\n %s", plainFormatted)
}
return plainFormatted
}
// Errorf calls Error on t with a colorized message
func Errorf(t *testing.T, format string, args ...interface{}) {
t.Error(Scolorf(ErrorColorSyntaxCode, format, args...))
}
// Fatalf calls Fatal on t with a colorized message
func Fatalf(t *testing.T, format string, args ...interface{}) {
t.Fatal(Scolorf(FatalColorSyntaxCode, format, args...))
}