Skip to content

Commit

Permalink
Properly handle single arg calls to helper funcs (#40)
Browse files Browse the repository at this point in the history
* Properly handle single arg calls to helper funcs
  • Loading branch information
moorereason authored and fatih committed Sep 27, 2016
1 parent 87d4004 commit 0016e26
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 22 deletions.
43 changes: 21 additions & 22 deletions color.go
Expand Up @@ -345,6 +345,11 @@ func Cyan(format string, a ...interface{}) { printColor(format, FgCyan, a...) }
func White(format string, a ...interface{}) { printColor(format, FgWhite, a...) }

func printColor(format string, p Attribute, a ...interface{}) {
if len(a) == 0 {
a = append(a, format)
format = "%s"
}

if !strings.HasSuffix(format, "\n") {
format += "\n"
}
Expand All @@ -353,50 +358,44 @@ func printColor(format string, p Attribute, a ...interface{}) {
c.Printf(format, a...)
}

func printString(format string, p Attribute, a ...interface{}) string {
if len(a) == 0 {
a = append(a, format)
format = "%s"
}
return New(p).SprintfFunc()(format, a...)
}

// BlackString is an convenient helper function to return a string with black
// foreground.
func BlackString(format string, a ...interface{}) string {
return New(FgBlack).SprintfFunc()(format, a...)
}
func BlackString(format string, a ...interface{}) string { return printString(format, FgBlack, a...) }

// RedString is an convenient helper function to return a string with red
// foreground.
func RedString(format string, a ...interface{}) string {
return New(FgRed).SprintfFunc()(format, a...)
}
func RedString(format string, a ...interface{}) string { return printString(format, FgRed, a...) }

// GreenString is an convenient helper function to return a string with green
// foreground.
func GreenString(format string, a ...interface{}) string {
return New(FgGreen).SprintfFunc()(format, a...)
}
func GreenString(format string, a ...interface{}) string { return printString(format, FgGreen, a...) }

// YellowString is an convenient helper function to return a string with yellow
// foreground.
func YellowString(format string, a ...interface{}) string {
return New(FgYellow).SprintfFunc()(format, a...)
}
func YellowString(format string, a ...interface{}) string { return printString(format, FgYellow, a...) }

// BlueString is an convenient helper function to return a string with blue
// foreground.
func BlueString(format string, a ...interface{}) string {
return New(FgBlue).SprintfFunc()(format, a...)
}
func BlueString(format string, a ...interface{}) string { return printString(format, FgBlue, a...) }

// MagentaString is an convenient helper function to return a string with magenta
// foreground.
func MagentaString(format string, a ...interface{}) string {
return New(FgMagenta).SprintfFunc()(format, a...)
return printString(format, FgMagenta, a...)
}

// CyanString is an convenient helper function to return a string with cyan
// foreground.
func CyanString(format string, a ...interface{}) string {
return New(FgCyan).SprintfFunc()(format, a...)
}
func CyanString(format string, a ...interface{}) string { return printString(format, FgCyan, a...) }

// WhiteString is an convenient helper function to return a string with white
// foreground.
func WhiteString(format string, a ...interface{}) string {
return New(FgWhite).SprintfFunc()(format, a...)
}
func WhiteString(format string, a ...interface{}) string { return printString(format, FgWhite, a...) }
51 changes: 51 additions & 0 deletions color_test.go
Expand Up @@ -224,3 +224,54 @@ func TestColorVisual(t *testing.T) {
fmt.Fprintln(Output, CyanString("cyan"))
fmt.Fprintln(Output, WhiteString("white"))
}

func TestNoFormat(t *testing.T) {
fmt.Printf("%s %%s = ", BlackString("Black"))
Black("%s")

fmt.Printf("%s %%s = ", RedString("Red"))
Red("%s")

fmt.Printf("%s %%s = ", GreenString("Green"))
Green("%s")

fmt.Printf("%s %%s = ", YellowString("Yellow"))
Yellow("%s")

fmt.Printf("%s %%s = ", BlueString("Blue"))
Blue("%s")

fmt.Printf("%s %%s = ", MagentaString("Magenta"))
Magenta("%s")

fmt.Printf("%s %%s = ", CyanString("Cyan"))
Cyan("%s")

fmt.Printf("%s %%s = ", WhiteString("White"))
White("%s")
}

func TestNoFormatString(t *testing.T) {
tests := []struct {
f func(string, ...interface{}) string
format string
args []interface{}
want string
}{
{BlackString, "%s", nil, "\x1b[30m%s\x1b[0m"},
{RedString, "%s", nil, "\x1b[31m%s\x1b[0m"},
{GreenString, "%s", nil, "\x1b[32m%s\x1b[0m"},
{YellowString, "%s", nil, "\x1b[33m%s\x1b[0m"},
{BlueString, "%s", nil, "\x1b[34m%s\x1b[0m"},
{MagentaString, "%s", nil, "\x1b[35m%s\x1b[0m"},
{CyanString, "%s", nil, "\x1b[36m%s\x1b[0m"},
{WhiteString, "%s", nil, "\x1b[37m%s\x1b[0m"},
}

for i, test := range tests {
s := fmt.Sprintf("%s", test.f(test.format, test.args...))
if s != test.want {
t.Errorf("[%d] want: %q, got: %q", i, test.want, s)
}
}
}

0 comments on commit 0016e26

Please sign in to comment.