diff --git a/colorstring.go b/colorstring.go index 3de5b24..95b1423 100644 --- a/colorstring.go +++ b/colorstring.go @@ -8,6 +8,8 @@ import ( "io" "regexp" "strings" + + "github.com/mattn/go-colorable" ) // Color colorizes your strings using the default settings. @@ -115,6 +117,9 @@ func (c *Colorize) ColorPrefix(v string) string { // color will be used for the background color. var DefaultColors map[string]string +// DefaultOutput are the default writer used when print. +var DefaultOutput = colorable.NewColorableStdout() + func init() { DefaultColors = map[string]string{ // Default foreground/background colors @@ -189,7 +194,7 @@ var prefixRe = regexp.MustCompile(`^(?i)(` + parseReRaw + `)+`) // operands when neither is a string. It returns the number of bytes written // and any write error encountered. func Print(a string) (n int, err error) { - return fmt.Print(Color(a)) + return fmt.Fprint(DefaultOutput, Color(a)) } // Println is a convenience wrapper for fmt.Println with support for color @@ -200,7 +205,7 @@ func Print(a string) (n int, err error) { // between operands and a newline is appended. It returns the number of bytes // written and any write error encountered. func Println(a string) (n int, err error) { - return fmt.Println(Color(a)) + return fmt.Fprintln(DefaultOutput, Color(a)) } // Printf is a convenience wrapper for fmt.Printf with support for color codes. @@ -209,7 +214,7 @@ func Println(a string) (n int, err error) { // with support for color codes. It returns the number of bytes written and any // write error encountered. func Printf(format string, a ...interface{}) (n int, err error) { - return fmt.Printf(Color(format), a...) + return fmt.Fprintf(DefaultOutput, Color(format), a...) } // Fprint is a convenience wrapper for fmt.Fprint with support for color codes. diff --git a/colorstring_test.go b/colorstring_test.go index 3e76abe..52ac433 100644 --- a/colorstring_test.go +++ b/colorstring_test.go @@ -1,7 +1,6 @@ package colorstring import ( - "os" "testing" ) @@ -162,16 +161,18 @@ func TestConvenienceWrappers(t *testing.T) { length, _ = Printf(printfInput) assertOutputLength(t, printfInput, 59, length) + stdout := DefaultOutput + // colorstring.Fprint - length, _ = Fprint(os.Stdout, fprintInput) + length, _ = Fprint(stdout, fprintInput) assertOutputLength(t, fprintInput, 59, length) // colorstring.Fprintln - length, _ = Fprintln(os.Stdout, fprintlnInput) + length, _ = Fprintln(stdout, fprintlnInput) assertOutputLength(t, fprintlnInput, 60, length) // colorstring.Fprintf - length, _ = Fprintf(os.Stdout, fprintfInput) + length, _ = Fprintf(stdout, fprintfInput) assertOutputLength(t, fprintfInput, 59, length) }