forked from gopasspw/gopass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
context.go
66 lines (56 loc) · 1.52 KB
/
context.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
package out
import "context"
type contextKey int
const (
ctxKeyPrefix contextKey = iota
ctxKeyHidden
ctxKeyNewline
)
// WithPrefix returns a context with the given prefix set
func WithPrefix(ctx context.Context, prefix string) context.Context {
return context.WithValue(ctx, ctxKeyPrefix, prefix)
}
// AddPrefix returns a context with the given prefix added to end of the
// existing prefix
func AddPrefix(ctx context.Context, prefix string) context.Context {
if prefix == "" {
return ctx
}
pfx := Prefix(ctx)
if pfx == "" {
return WithPrefix(ctx, prefix)
}
return WithPrefix(ctx, pfx+prefix)
}
// Prefix returns the prefix or an empty string
func Prefix(ctx context.Context) string {
sv, ok := ctx.Value(ctxKeyPrefix).(string)
if !ok {
return ""
}
return sv
}
// WithHidden returns a context with the flag value for hidden set
func WithHidden(ctx context.Context, hidden bool) context.Context {
return context.WithValue(ctx, ctxKeyHidden, hidden)
}
// IsHidden returns true if any output should be hidden in this context
func IsHidden(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyHidden).(bool)
if !ok {
return false
}
return bv
}
// WithNewline returns a context with the flag value for newline set
func WithNewline(ctx context.Context, nl bool) context.Context {
return context.WithValue(ctx, ctxKeyNewline, nl)
}
// HasNewline returns the value of newline or the default (true)
func HasNewline(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyNewline).(bool)
if !ok {
return true
}
return bv
}