-
-
Notifications
You must be signed in to change notification settings - Fork 479
/
context.go
153 lines (122 loc) · 3.62 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
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
149
150
151
152
153
package action
import "context"
type contextKey int
const (
ctxKeyClip contextKey = iota
ctxKeyPasswordOnly
ctxKeyPrintQR
ctxKeyRevision
ctxKeyKey
ctxKeyOnlyClip
ctxKeyAlsoClip
ctxKeyPrintChars
)
// WithClip returns a context with the value for clip (for copy to clipboard)
// set.
func WithClip(ctx context.Context, clip bool) context.Context {
return context.WithValue(ctx, ctxKeyClip, clip)
}
// IsClip returns the value of clip or the default (false).
func IsClip(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyClip).(bool)
if !ok {
return false
}
return bv
}
// WithAlsoClip returns a context with the value for alsoclip (copy to
// clipboard and print to stdout) set.
func WithAlsoClip(ctx context.Context, clip bool) context.Context {
return context.WithValue(ctx, ctxKeyAlsoClip, clip)
}
// IsAlsoClip returns the value for alsoclip of the dfeault (false).
func IsAlsoClip(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyAlsoClip).(bool)
if !ok {
return false
}
return bv
}
// WithOnlyClip returns a context with the value for clip (for copy to clipboard)
// set.
func WithOnlyClip(ctx context.Context, clip bool) context.Context {
return context.WithValue(ctx, ctxKeyOnlyClip, clip)
}
// IsOnlyClip returns the value of clip or the default (false).
func IsOnlyClip(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyOnlyClip).(bool)
if !ok {
return false
}
return bv
}
// WithPasswordOnly returns a context with the value of password only set.
func WithPasswordOnly(ctx context.Context, pw bool) context.Context {
return context.WithValue(ctx, ctxKeyPasswordOnly, pw)
}
// IsPasswordOnly returns the value of password only or the default (false).
func IsPasswordOnly(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyPasswordOnly).(bool)
if !ok {
return false
}
return bv
}
// WithPrintQR returns a context with the value of print QR set.
func WithPrintQR(ctx context.Context, qr bool) context.Context {
return context.WithValue(ctx, ctxKeyPrintQR, qr)
}
// IsPrintQR returns the value of print QR or the default (false).
func IsPrintQR(ctx context.Context) bool {
bv, ok := ctx.Value(ctxKeyPrintQR).(bool)
if !ok {
return false
}
return bv
}
// WithRevision returns a context withe the value of revision set.
func WithRevision(ctx context.Context, rev string) context.Context {
return context.WithValue(ctx, ctxKeyRevision, rev)
}
// HasRevision returns true if a value for revision was set in this context.
func HasRevision(ctx context.Context) bool {
sv, ok := ctx.Value(ctxKeyRevision).(string)
return ok && sv != ""
}
// GetRevision returns the revison set in this context or an empty string.
func GetRevision(ctx context.Context) string {
sv, ok := ctx.Value(ctxKeyRevision).(string)
if !ok {
return ""
}
return sv
}
// WithKey returns a context with the key set.
func WithKey(ctx context.Context, sv string) context.Context {
return context.WithValue(ctx, ctxKeyKey, sv)
}
// HasKey returns true if the key is set.
func HasKey(ctx context.Context) bool {
_, ok := ctx.Value(ctxKeyKey).(string)
return ok
}
// GetKey returns the value of key or the default (empty string).
func GetKey(ctx context.Context) string {
sv, ok := ctx.Value(ctxKeyKey).(string)
if !ok {
return ""
}
return sv
}
// WithPrintChars returns the context with the print chars set.
func WithPrintChars(ctx context.Context, c []int) context.Context {
return context.WithValue(ctx, ctxKeyPrintChars, c)
}
// GetPrintChars returns a map of all character positions to print.
func GetPrintChars(ctx context.Context) []int {
mv, ok := ctx.Value(ctxKeyPrintChars).([]int)
if !ok {
return nil
}
return mv
}