diff options
| -rw-r--r-- | README.md | 12 | ||||
| -rw-r--r-- | color.go | 48 | ||||
| -rw-r--r-- | color_test.go | 12 |
3 files changed, 67 insertions, 5 deletions
@@ -25,7 +25,6 @@ color.Blue("Prints %s in blue.", "text") // These are using by default foreground colors. color.Red("We have red") -color.Yellow("Yellow color too!") color.Magenta("And many others ..") ``` @@ -41,7 +40,6 @@ c.Println("Prints cyan text with an underline.") d := color.New(color.FgCyan, color.Bold) d.Printf("This prints bold cyan %s\n", "too!.") - // Mix up foreground and background colors, create new mixes! red := color.New(color.FgRed) @@ -69,12 +67,16 @@ notice("don't forget this...") ```go // Create SprintXxx functions to mix strings with other non-colorized strings: -yellow := New(FgYellow).SprintFunc() -red := New(FgRed).SprintFunc() +yellow := color.New(color.FgYellow).SprintFunc() +red := color.New(color.FgRed).SprintFunc() fmt.Printf("this is a %s and this is %s.\n", yellow("warning"), red("error")) -info := New(FgWhite, BgGreen).SprintFunc() +info := color.New(color.FgWhite, color.BgGreen).SprintFunc() fmt.Printf("this %s rocks!\n", info("package")) + +// use helper functions +fmt.Printf("this", color.RedString("warning"), "should be not neglected.") +fmt.Printf(color.GreenString("Info:"), "an important message." ) ``` ### Plug into existing code @@ -236,3 +236,51 @@ func printColor(format string, p Attribute, a ...interface{}) { c := &Color{params: []Attribute{p}} c.Printf(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...) +} + +// 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...) +} + +// 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...) +} + +// 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...) +} + +// 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...) +} + +// 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...) +} + +// 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...) +} + +// 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...) +} diff --git a/color_test.go b/color_test.go index 172698d..836f4dc 100644 --- a/color_test.go +++ b/color_test.go @@ -112,4 +112,16 @@ func TestColor(t *testing.T) { info := New(FgWhite, BgGreen).SprintFunc() fmt.Printf("this %s rocks!\n", info("package")) + // Fifth Visual Test + fmt.Println() + + fmt.Println(BlackString("black")) + fmt.Println(RedString("red")) + fmt.Println(GreenString("green")) + fmt.Println(YellowString("yellow")) + fmt.Println(BlueString("blue")) + fmt.Println(MagentaString("magenta")) + fmt.Println(CyanString("cyan")) + fmt.Println(WhiteString("white")) + } |
