forked from helm-unittest/helm-unittest
/
printer.go
101 lines (85 loc) · 2.49 KB
/
printer.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
package unittest
import (
"fmt"
"io"
"github.com/fatih/color"
)
// NewPrinter create a Printer with Writer to print and colored config
func NewPrinter(writer io.Writer, colored *bool) *Printer {
p := &Printer{
Writer: writer,
Colored: colored,
}
p.colors.Success = color.New(color.FgGreen)
p.setupColor(p.colors.Success)
p.colors.SuccessBg = color.New(color.BgGreen, color.FgBlack)
p.setupColor(p.colors.SuccessBg)
p.colors.Danger = color.New(color.FgRed)
p.setupColor(p.colors.Danger)
p.colors.DangerBg = color.New(color.BgRed, color.FgWhite)
p.setupColor(p.colors.DangerBg)
p.colors.Warning = color.New(color.FgYellow)
p.setupColor(p.colors.Warning)
p.colors.WarningBg = color.New(color.BgYellow, color.FgBlack)
p.setupColor(p.colors.WarningBg)
p.colors.Highlight = color.New(color.Bold)
p.setupColor(p.colors.Highlight)
p.colors.Faint = color.New(color.Faint)
p.setupColor(p.colors.Faint)
return p
}
// Printer simple printing implement
type Printer struct {
Writer io.Writer
Colored *bool
colors struct {
Success *color.Color
SuccessBg *color.Color
Warning *color.Color
WarningBg *color.Color
Danger *color.Color
DangerBg *color.Color
Highlight *color.Color
Faint *color.Color
}
}
func (p *Printer) setupColor(color *color.Color) {
if p.Colored != nil {
if *p.Colored {
color.EnableColor()
} else {
color.DisableColor()
}
}
}
func (p *Printer) println(content string, indentLevel int) {
var indent string
for i := 0; i < indentLevel; i++ {
indent += "\t"
}
fmt.Fprintln(p.Writer, indent+content)
}
func (p *Printer) success(format string, a ...interface{}) string {
return p.colors.Success.Sprintf(format, a...)
}
func (p *Printer) successLabel(format string, a ...interface{}) string {
return p.colors.SuccessBg.Sprintf(format, a...)
}
func (p *Printer) danger(format string, a ...interface{}) string {
return p.colors.Danger.Sprintf(format, a...)
}
func (p *Printer) dangerLabel(format string, a ...interface{}) string {
return p.colors.DangerBg.Sprintf(format, a...)
}
func (p *Printer) warning(format string, a ...interface{}) string {
return p.colors.Warning.Sprintf(format, a...)
}
func (p *Printer) warningLabel(format string, a ...interface{}) string {
return p.colors.WarningBg.Sprintf(format, a...)
}
func (p *Printer) highlight(format string, a ...interface{}) string {
return p.colors.Highlight.Sprintf(format, a...)
}
func (p *Printer) faint(format string, a ...interface{}) string {
return p.colors.Faint.Sprintf(format, a...)
}