diff --git a/README.md b/README.md index f872bf6..6db05af 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ Color let you use colorized outputs in terms of [ANSI Escape Codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors). The API can be used in several way, pick one one that suits you. +The package is under ongoing development, checkout for regular updates. ## Install @@ -51,6 +52,19 @@ whiteBackground := red.Add(color.BgWhite) whiteBackground.Println("Red text with White background.") ``` +### Custom print functions + +```go +// Create a custom print function for convenient +red := color.New(color.FgRed).PrintfFunc() +red("warning") +red("error: %s", err) + +// Mix up multiple attributes +notice := color.New(color.Bold, color.FgGreen).PrintlnFunc() +notice("don't forget this...") +``` + ### Plug into existing code ```go @@ -71,8 +85,8 @@ fmt.Println("All text will be now bold magenta.") ## Todo -* Add partial coloring of lines. -* Save/Return previous values +* Save/Return previous values +* Implement SprintXxx functions() ## Credits diff --git a/color.go b/color.go index 26f0870..721ecb6 100644 --- a/color.go +++ b/color.go @@ -128,6 +128,32 @@ func (c *Color) Println(a ...interface{}) (n int, err error) { return fmt.Fprintln(Output, a...) } +func (c *Color) Sprintln() func(a ...interface{}) string { + return func(a ...interface{}) string { + c.Set() + defer Unset() + return fmt.Sprintln(a...) + } +} + +// PrintFunc returns a new function prints the passed arguments as colorized +// with color.Print(). +func (c *Color) PrintFunc() func(a ...interface{}) { + return func(a ...interface{}) { c.Print(a...) } +} + +// PrintfFunc returns a new function prints the passed arguments as colorized +// with color.Printf(). +func (c *Color) PrintfFunc() func(format string, a ...interface{}) { + return func(format string, a ...interface{}) { c.Printf(format, a...) } +} + +// PrintlnFunc returns a new function prints the passed arguments as colorized +// with color.Println(). +func (c *Color) PrintlnFunc() func(a ...interface{}) { + return func(a ...interface{}) { c.Println(a...) } +} + // sequence returns a formated SGR sequence to be plugged into a "\x1b[...m" // an example output might be: "1;36" -> bold cyan func (c *Color) sequence() string { diff --git a/color_test.go b/color_test.go index 222b22a..9f3aa81 100644 --- a/color_test.go +++ b/color_test.go @@ -87,6 +87,7 @@ func TestColor(t *testing.T) { White("white") // Third visual test + fmt.Println() Set(FgBlue) fmt.Println("is this blue?") Unset() @@ -95,4 +96,12 @@ func TestColor(t *testing.T) { fmt.Println("and this magenta?") Unset() + // Fourth Visual test + fmt.Println() + blue := New(FgBlue).PrintlnFunc() + blue("blue text with custom print func") + + red := New(FgRed).PrintfFunc() + red("red text with a printf func: %d\n", 123) + } diff --git a/doc.go b/doc.go index 4876257..afbb8e1 100644 --- a/doc.go +++ b/doc.go @@ -39,6 +39,18 @@ separate color object. whiteBackground.Println("Red text with White background.") +You can create PrintXxx functions to simplify even more: + + // Create a custom print function for convenient + red := color.New(color.FgRed).PrintfFunc() + red("warning") + red("error: %s", err) + + // Mix up multiple attributes + notice := color.New(color.Bold, color.FgGreen).PrintlnFunc() + notice("don't forget this...") + + Using with existing color is possible too. Just use the Set() method to set the standard output to the given parameters. That way a rewrite of an existing code is not required.