@@ -22,7 +22,7 @@ type OutputOption = func(*Output)
22
22
23
23
// Output is a terminal output.
24
24
type Output struct {
25
- Profile
25
+ profile Profile
26
26
tty io.Writer
27
27
environ Environ
28
28
@@ -33,6 +33,8 @@ type Output struct {
33
33
fgColor Color
34
34
bgSync * sync.Once
35
35
bgColor Color
36
+
37
+ mtx sync.RWMutex
36
38
}
37
39
38
40
// Environ is an interface for getting environment variables.
@@ -66,7 +68,7 @@ func NewOutput(tty io.Writer, opts ...OutputOption) *Output {
66
68
o := & Output {
67
69
tty : tty ,
68
70
environ : & osEnviron {},
69
- Profile : - 1 ,
71
+ profile : - 1 ,
70
72
fgSync : & sync.Once {},
71
73
fgColor : NoColor {},
72
74
bgSync : & sync.Once {},
@@ -79,8 +81,8 @@ func NewOutput(tty io.Writer, opts ...OutputOption) *Output {
79
81
for _ , opt := range opts {
80
82
opt (o )
81
83
}
82
- if o .Profile < 0 {
83
- o .Profile = o .EnvColorProfile ()
84
+ if o .profile < 0 {
85
+ o .profile = o .EnvColorProfile ()
84
86
}
85
87
86
88
return o
@@ -96,7 +98,7 @@ func WithEnvironment(environ Environ) OutputOption {
96
98
// WithProfile returns a new OutputOption for the given profile.
97
99
func WithProfile (profile Profile ) OutputOption {
98
100
return func (o * Output ) {
99
- o .Profile = profile
101
+ o .profile = profile
100
102
}
101
103
}
102
104
@@ -135,7 +137,16 @@ func WithUnsafe() OutputOption {
135
137
136
138
// ColorProfile returns the supported color profile:
137
139
func (o Output ) ColorProfile () Profile {
138
- return o .Profile
140
+ o .mtx .RLock ()
141
+ defer o .mtx .RUnlock ()
142
+ return o .profile
143
+ }
144
+
145
+ // SetColorProfile sets the color profile.
146
+ func (o * Output ) SetColorProfile (profile Profile ) {
147
+ o .mtx .Lock ()
148
+ defer o .mtx .Unlock ()
149
+ o .profile = profile
139
150
}
140
151
141
152
// ForegroundColor returns the terminal's default foreground color.
@@ -145,7 +156,9 @@ func (o *Output) ForegroundColor() Color {
145
156
return
146
157
}
147
158
159
+ o .mtx .Lock ()
148
160
o .fgColor = o .foregroundColor ()
161
+ o .mtx .Unlock ()
149
162
}
150
163
151
164
if o .cache {
@@ -154,6 +167,8 @@ func (o *Output) ForegroundColor() Color {
154
167
f ()
155
168
}
156
169
170
+ o .mtx .RLock ()
171
+ defer o .mtx .RUnlock ()
157
172
return o .fgColor
158
173
}
159
174
@@ -164,7 +179,9 @@ func (o *Output) BackgroundColor() Color {
164
179
return
165
180
}
166
181
182
+ o .mtx .Lock ()
167
183
o .bgColor = o .backgroundColor ()
184
+ o .mtx .Unlock ()
168
185
}
169
186
170
187
if o .cache {
@@ -173,6 +190,8 @@ func (o *Output) BackgroundColor() Color {
173
190
f ()
174
191
}
175
192
193
+ o .mtx .RLock ()
194
+ defer o .mtx .RUnlock ()
176
195
return o .bgColor
177
196
}
178
197
@@ -185,18 +204,18 @@ func (o *Output) HasDarkBackground() bool {
185
204
186
205
// TTY returns the terminal's file descriptor. This may be nil if the output is
187
206
// not a terminal.
188
- func (o Output ) TTY () File {
207
+ func (o * Output ) TTY () File {
189
208
if f , ok := o .tty .(File ); ok {
190
209
return f
191
210
}
192
211
return nil
193
212
}
194
213
195
- func (o Output ) Write (p []byte ) (int , error ) {
214
+ func (o * Output ) Write (p []byte ) (int , error ) {
196
215
return o .tty .Write (p )
197
216
}
198
217
199
218
// WriteString writes the given string to the output.
200
- func (o Output ) WriteString (s string ) (int , error ) {
219
+ func (o * Output ) WriteString (s string ) (int , error ) {
201
220
return o .Write ([]byte (s ))
202
221
}
0 commit comments