Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions colorstring.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"io"
"regexp"
"strings"

"github.com/mattn/go-colorable"
)

// Color colorizes your strings using the default settings.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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.
Expand Down
9 changes: 5 additions & 4 deletions colorstring_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package colorstring

import (
"os"
"testing"
)

Expand Down Expand Up @@ -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)
}

Expand Down