Skip to content

Commit 212f8c5

Browse files
authored
Merge pull request #182 from fatih/expose-set-writers
color: expose `SetWriter` and `UnsetWriter`
2 parents 6378f62 + 7310c74 commit 212f8c5

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

Diff for: color.go

+13-8
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,10 @@ func (c *Color) unset() {
163163
Unset()
164164
}
165165

166-
func (c *Color) setWriter(w io.Writer) *Color {
166+
// SetWriter is used to set the SGR sequence with the given io.Writer. This is
167+
// a low-level function, and users should use the higher-level functions, such
168+
// as color.Fprint, color.Print, etc.
169+
func (c *Color) SetWriter(w io.Writer) *Color {
167170
if c.isNoColorSet() {
168171
return c
169172
}
@@ -172,7 +175,9 @@ func (c *Color) setWriter(w io.Writer) *Color {
172175
return c
173176
}
174177

175-
func (c *Color) unsetWriter(w io.Writer) {
178+
// UnsetWriter resets all escape attributes and clears the output with the give
179+
// io.Writer. Usually should be called after SetWriter().
180+
func (c *Color) UnsetWriter(w io.Writer) {
176181
if c.isNoColorSet() {
177182
return
178183
}
@@ -197,8 +202,8 @@ func (c *Color) Add(value ...Attribute) *Color {
197202
// On Windows, users should wrap w with colorable.NewColorable() if w is of
198203
// type *os.File.
199204
func (c *Color) Fprint(w io.Writer, a ...interface{}) (n int, err error) {
200-
c.setWriter(w)
201-
defer c.unsetWriter(w)
205+
c.SetWriter(w)
206+
defer c.UnsetWriter(w)
202207

203208
return fmt.Fprint(w, a...)
204209
}
@@ -220,8 +225,8 @@ func (c *Color) Print(a ...interface{}) (n int, err error) {
220225
// On Windows, users should wrap w with colorable.NewColorable() if w is of
221226
// type *os.File.
222227
func (c *Color) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error) {
223-
c.setWriter(w)
224-
defer c.unsetWriter(w)
228+
c.SetWriter(w)
229+
defer c.UnsetWriter(w)
225230

226231
return fmt.Fprintf(w, format, a...)
227232
}
@@ -241,8 +246,8 @@ func (c *Color) Printf(format string, a ...interface{}) (n int, err error) {
241246
// On Windows, users should wrap w with colorable.NewColorable() if w is of
242247
// type *os.File.
243248
func (c *Color) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
244-
c.setWriter(w)
245-
defer c.unsetWriter(w)
249+
c.SetWriter(w)
250+
defer c.UnsetWriter(w)
246251

247252
return fmt.Fprintln(w, a...)
248253
}

0 commit comments

Comments
 (0)