diff --git a/color.go b/color.go index e3e9972..d00bdbd 100644 --- a/color.go +++ b/color.go @@ -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" } @@ -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...) } diff --git a/color_test.go b/color_test.go index 8028535..a88c404 100644 --- a/color_test.go +++ b/color_test.go @@ -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) + } + } +}