diff options
| -rw-r--r-- | README.md | 10 | ||||
| -rw-r--r-- | color.go | 54 | ||||
| -rw-r--r-- | doc.go | 15 |
3 files changed, 42 insertions, 37 deletions
@@ -1,7 +1,7 @@ # Color [](http://godoc.org/github.com/fatih/color) Color let you use colorized outputs in terms of [ASCI Escape -Codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors). The api can be +Codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors). The API can be used in several way, pick one one that suits you. @@ -18,7 +18,9 @@ go get github.com/fatih/color ```go // Print with default helper functions color.Cyan("Prints text in cyan.") -color.Blue("Prints %s in blue.", "text") // a newline will be appended automatically + +// a newline will be appended automatically +color.Blue("Prints %s in blue.", "text") // These are using by default foreground colors. color.Red("We have red") @@ -54,8 +56,10 @@ whiteBackground.Println("Red text with White background.") ```go // Use handy standard colors. color.Set(collor.FgYellow) -fmt.Println("Existing text in your codebase will be now in Yellow") + +fmt.Println("Existing text will be now in Yellow") fmt.Printf("This one %s\n", "too") + color.Unset() // don't forget to unset // You can mix up parameters @@ -10,17 +10,17 @@ import ( // Color defines a custom color object which is defined by SGR parameters. type Color struct { - params []Parameter + params []Attribute } -// Parameter defines a single SGR Code -type Parameter int +// Attribute defines a single SGR Code +type Attribute int const escape = "\x1b" -// Base paramaters +// Base attributes const ( - Reset Parameter = iota + Reset Attribute = iota Bold Faint Italic @@ -34,7 +34,7 @@ const ( // Foreground text colors const ( - FgBlack Parameter = iota + 30 + FgBlack Attribute = iota + 30 FgRed FgGreen FgYellow @@ -46,7 +46,7 @@ const ( // Background text colors const ( - BgBlack Parameter = iota + 40 + BgBlack Attribute = iota + 40 BgRed BgGreen BgYellow @@ -57,34 +57,34 @@ const ( ) // New returns a newly created color object. -func New(value ...Parameter) *Color { - c := &Color{params: make([]Parameter, 0)} +func New(value ...Attribute) *Color { + c := &Color{params: make([]Attribute, 0)} c.Add(value...) return c } // Set sets the given parameters immediately. It will change the color of // output with the given SGR parameters until color.Unset() is called. -func Set(p ...Parameter) *Color { +func Set(p ...Attribute) *Color { c := New(p...) c.Set() return c } -// Unset resets all escape attributes and clears the output. Usualy should +// Unset resets all escape attributes and clears the output. Usually should // be called after Set(). func Unset() { fmt.Fprintf(Output, "%s[%dm", escape, Reset) } -// Add is used to chain SGR parameters. Use as many as paramters to combine +// Add is used to chain SGR parameters. Use as many as parameters to combine // and create custom color objects. Example: Add(color.FgRed, color.Underline). -func (c *Color) Add(value ...Parameter) *Color { +func (c *Color) Add(value ...Attribute) *Color { c.params = append(c.params, value...) return c } -func (c *Color) prepend(value Parameter) { +func (c *Color) prepend(value Attribute) { c.params = append(c.params, 0) copy(c.params[1:], c.params[0:]) c.params[0] = value @@ -94,16 +94,6 @@ func (c *Color) prepend(value Parameter) { // os.Stdout is used. var Output io.Writer = os.Stdout -// Printf formats according to a format specifier and writes to standard output. -// It returns the number of bytes written and any write error encountered. -// This is the standard fmt.Printf() method wrapped with the given color. -func (c *Color) Printf(format string, a ...interface{}) (n int, err error) { - c.Set() - defer Unset() - - return fmt.Fprintf(Output, format, a...) -} - // Print formats using the default formats for its operands and writes to // standard output. Spaces are added between operands when neither is a // string. It returns the number of bytes written and any write error @@ -116,6 +106,16 @@ func (c *Color) Print(a ...interface{}) (n int, err error) { return fmt.Fprint(Output, a...) } +// Printf formats according to a format specifier and writes to standard output. +// It returns the number of bytes written and any write error encountered. +// This is the standard fmt.Printf() method wrapped with the given color. +func (c *Color) Printf(format string, a ...interface{}) (n int, err error) { + c.Set() + defer Unset() + + return fmt.Fprintf(Output, format, a...) +} + // Println formats using the default formats for its operands and writes to // standard output. Spaces are always added between operands and a newline is // appended. It returns the number of bytes written and any write error @@ -157,7 +157,7 @@ func Red(format string, a ...interface{}) { printColor(format, FgRed, a...) } // newline is appended to format by default. func Green(format string, a ...interface{}) { printColor(format, FgGreen, a...) } -// Yellow is an convenient helper function to print with yello foreground. +// Yellow is an convenient helper function to print with yellow foreground. // A newline is appended to format by default. func Yellow(format string, a ...interface{}) { printColor(format, FgYellow, a...) } @@ -177,11 +177,11 @@ func Cyan(format string, a ...interface{}) { printColor(format, FgCyan, a...) } // newline is appended to format by default. func White(format string, a ...interface{}) { printColor(format, FgWhite, a...) } -func printColor(format string, p Parameter, a ...interface{}) { +func printColor(format string, p Attribute, a ...interface{}) { if !strings.HasSuffix(format, "\n") { format += "\n" } - c := &Color{params: []Parameter{p}} + c := &Color{params: []Attribute{p}} c.Printf(format, a...) } @@ -1,14 +1,14 @@ /* Package color is an ANSI color package to output colorized or SGR defined output to the standard output. The api can be used in several way, pick one -one that suits you. +that suits you. Use simple and default helper functions with predefined foreground colors: color.Cyan("Prints text in cyan.") color.Blue("Prints %s in blue.", "text") // a newline will be appended automatically - // These are using by default foreground colors. + // More default foreground colors.. color.Red("We have red") color.Yellow("Yellow color too!") color.Magenta("And many others ..") @@ -16,7 +16,7 @@ Use simple and default helper functions with predefined foreground colors: However there are times where custom color mixes are required. Below are some examples to create custom color objects and use the print functions of each -seperate color object. +separate color object. // Create a new color object c := color.New(color.FgCyan).Add(color.Underline) @@ -38,13 +38,15 @@ seperate color object. Using with existing color is possible too. Just use the Set() method to set -the standart output to the given parameters. That way a rewrite of an existing +the standard output to the given parameters. That way a rewrite of an existing code is not required. // Use handy standard colors. - color.Set(collor.FgYellow) - fmt.Println("Existing text in your codebase will be now in Yellow") + color.Set(color.FgYellow) + + fmt.Println("Existing text will be now in Yellow") fmt.Printf("This one %s\n", "too") + color.Unset() // don't forget to unset // You can mix up parameters @@ -53,5 +55,4 @@ code is not required. fmt.Println("All text will be now bold magenta.") */ - package color |
