-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
print.go
124 lines (106 loc) · 3.21 KB
/
print.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 out
import (
"context"
"fmt"
"io"
"os"
"runtime"
"strings"
"github.com/gopasspw/gopass/pkg/ctxutil"
"github.com/fatih/color"
)
var (
// Stdout is exported for tests
Stdout io.Writer = os.Stdout
// Stderr is exported for tests
Stderr io.Writer = os.Stderr
)
func newline(ctx context.Context) string {
if HasNewline(ctx) {
return "\n"
}
return ""
}
// Print formats and prints the given string
func Print(ctx context.Context, format string, args ...interface{}) {
if IsHidden(ctx) && !ctxutil.IsDebug(ctx) {
return
}
fmt.Fprintf(Stdout, Prefix(ctx)+format+newline(ctx), args...)
}
// Debug prints the given string if the debug flag is set
func Debug(ctx context.Context, format string, args ...interface{}) {
if !ctxutil.IsDebug(ctx) {
return
}
var loc string
if _, file, line, ok := runtime.Caller(1); ok {
file = file[strings.Index(file, "pkg/"):]
file = strings.TrimPrefix(file, "pkg/")
loc = fmt.Sprintf("%s:%d ", file, line)
}
fmt.Fprintf(Stdout, Prefix(ctx)+"[DEBUG] "+loc+format+newline(ctx), args...)
}
// Black prints the string in black
func Black(ctx context.Context, format string, args ...interface{}) {
if IsHidden(ctx) && !ctxutil.IsDebug(ctx) {
return
}
fmt.Fprint(Stdout, color.BlackString(Prefix(ctx)+format+newline(ctx), args...))
}
// Blue prints the string in blue
func Blue(ctx context.Context, format string, args ...interface{}) {
if IsHidden(ctx) && !ctxutil.IsDebug(ctx) {
return
}
fmt.Fprint(Stdout, color.BlueString(Prefix(ctx)+format+newline(ctx), args...))
}
// Cyan prints the string in cyan
func Cyan(ctx context.Context, format string, args ...interface{}) {
if IsHidden(ctx) && !ctxutil.IsDebug(ctx) {
return
}
fmt.Fprint(Stdout, color.CyanString(Prefix(ctx)+format+newline(ctx), args...))
}
// Green prints the string in green
func Green(ctx context.Context, format string, args ...interface{}) {
if IsHidden(ctx) && !ctxutil.IsDebug(ctx) {
return
}
fmt.Fprint(Stdout, color.GreenString(Prefix(ctx)+format+newline(ctx), args...))
}
// Magenta prints the string in magenta
func Magenta(ctx context.Context, format string, args ...interface{}) {
if IsHidden(ctx) && !ctxutil.IsDebug(ctx) {
return
}
fmt.Fprint(Stdout, color.MagentaString(Prefix(ctx)+format+newline(ctx), args...))
}
// Red prints the string in red
func Red(ctx context.Context, format string, args ...interface{}) {
if IsHidden(ctx) && !ctxutil.IsDebug(ctx) {
return
}
fmt.Fprint(Stdout, color.RedString(Prefix(ctx)+format+newline(ctx), args...))
}
// Error prints the string in red to stderr
func Error(ctx context.Context, format string, args ...interface{}) {
if IsHidden(ctx) && !ctxutil.IsDebug(ctx) {
return
}
fmt.Fprint(Stderr, color.RedString(Prefix(ctx)+format+newline(ctx), args...))
}
// White prints the string in white
func White(ctx context.Context, format string, args ...interface{}) {
if IsHidden(ctx) && !ctxutil.IsDebug(ctx) {
return
}
fmt.Fprint(Stdout, color.WhiteString(Prefix(ctx)+format+newline(ctx), args...))
}
// Yellow prints the string in yellow
func Yellow(ctx context.Context, format string, args ...interface{}) {
if IsHidden(ctx) && !ctxutil.IsDebug(ctx) {
return
}
fmt.Fprint(Stdout, color.YellowString(Prefix(ctx)+format+newline(ctx), args...))
}