@@ -220,22 +220,30 @@ func (c *Color) unset() {
220220// a low-level function, and users should use the higher-level functions, such
221221// as color.Fprint, color.Print, etc.
222222func (c * Color ) SetWriter (w io.Writer ) * Color {
223+ _ , _ = c .setWriter (w )
224+ return c
225+ }
226+
227+ func (c * Color ) setWriter (w io.Writer ) (int , error ) {
223228 if c .isNoColorSet () {
224- return c
229+ return 0 , nil
225230 }
226231
227- fmt .Fprint (w , c .format ())
228- return c
232+ return fmt .Fprint (w , c .format ())
229233}
230234
231235// UnsetWriter resets all escape attributes and clears the output with the give
232236// io.Writer. Usually should be called after SetWriter().
233237func (c * Color ) UnsetWriter (w io.Writer ) {
238+ _ , _ = c .unsetWriter (w )
239+ }
240+
241+ func (c * Color ) unsetWriter (w io.Writer ) (int , error ) {
234242 if c .isNoColorSet () {
235- return
243+ return 0 , nil
236244 }
237245
238- fmt .Fprintf (w , "%s[%dm" , escape , Reset )
246+ return fmt .Fprintf (w , "%s[%dm" , escape , Reset )
239247}
240248
241249// Add is used to chain SGR parameters. Use as many as parameters to combine
@@ -251,10 +259,20 @@ func (c *Color) Add(value ...Attribute) *Color {
251259// On Windows, users should wrap w with colorable.NewColorable() if w is of
252260// type *os.File.
253261func (c * Color ) Fprint (w io.Writer , a ... interface {}) (n int , err error ) {
254- c .SetWriter (w )
255- defer c .UnsetWriter (w )
262+ n , err = c .setWriter (w )
263+ if err != nil {
264+ return n , err
265+ }
266+
267+ nn , err := fmt .Fprint (w , a ... )
268+ n += nn
269+ if err != nil {
270+ return
271+ }
256272
257- return fmt .Fprint (w , a ... )
273+ nn , err = c .unsetWriter (w )
274+ n += nn
275+ return n , err
258276}
259277
260278// Print formats using the default formats for its operands and writes to
@@ -274,10 +292,20 @@ func (c *Color) Print(a ...interface{}) (n int, err error) {
274292// On Windows, users should wrap w with colorable.NewColorable() if w is of
275293// type *os.File.
276294func (c * Color ) Fprintf (w io.Writer , format string , a ... interface {}) (n int , err error ) {
277- c .SetWriter (w )
278- defer c .UnsetWriter (w )
295+ n , err = c .setWriter (w )
296+ if err != nil {
297+ return n , err
298+ }
299+
300+ nn , err := fmt .Fprintf (w , format , a ... )
301+ n += nn
302+ if err != nil {
303+ return
304+ }
279305
280- return fmt .Fprintf (w , format , a ... )
306+ nn , err = c .unsetWriter (w )
307+ n += nn
308+ return n , err
281309}
282310
283311// Printf formats according to a format specifier and writes to standard output.
0 commit comments