Documentation ¶
Overview ¶
Package multicolor is a complement to github.com/fatih/color.
Description ¶
It allows working with colors and attributes by using their string names. It also provides various print functions (Sprint, Sprintf, Sprintln, Print, Printf, Println, ErrorPrint, ErrorPrintf and Println) to let users provides colors and attributes before the actual text they want to print.
Use it as io.Writer ¶
Color objects defined by fatih cannot be used to configure a stream. With multicolor, Color objects can be configured as out stream or error stream.
import ( "github.com/coveooss/multilogger/color" "github.com/fatih/color" ) var ( Println = multicolor.Println ErrorPrintln = multicolor.ErrorPrintln ) func main() { // You can use a color object to write your output. multicolor.New("Yellow", "BgBlue").SetOut() Println("Hello world!") // You can also convert a Color object from fatih package and use it as a stream. multicolor.NewColorWriter(color.New(color.FgRed)).SetError() ErrorPrintln("There is something wrong!") }
Example ¶
Here is a complete example:
import ( "github.com/coveooss/multilogger/color" "github.com/fatih/color" ) var Println = multicolor.Println func main() { // You can use a color object to write your output. c := multicolor.New("Red", "BgWhite") c.Println("Hello world!") // Or you can directly supply your color to the global Print and ErrorPrint functions. // Multiple attributes can be combined, they will be ignored if they are not supported on your OS. multicolor.Println("BgBlue, Underline, CrossedOut, Faint, BlinkSlow, Italic", "The sky is blue.") fmt.Println("Hello, I should be in Hi Green.") // It is also possible to set the colors for all following outputs. The color setting // will then remain until another color is sent to the same output. multicolor.Set("HiGreen") // It is possible to use a mix of strings, multicolor.Attribute or color.Attribute (from fatih) // to configure your color object. // You can configure the default out stream to always print in color. multicolor.New(multicolor.BgGreen, color.FgWhite, "underline").SetOut() Println("I will be printed in color") }
The attributes name are not case significant. Any attributes defined in the constants can be used as string. For simplicity, it is allowed to forget the prefix FG to specify foreground colors.
Blue = FgBlue HiMagenta = FgHiMagenta
Index ¶
- Constants
- Variables
- func ErrorPrint(args ...interface{}) (int, error)
- func ErrorPrintf(args ...interface{}) (int, error)
- func ErrorPrintln(args ...interface{}) (int, error)
- func FormatMessage(args ...interface{}) string
- func Print(args ...interface{}) (int, error)
- func Printf(args ...interface{}) (int, error)
- func Println(args ...interface{}) (int, error)
- func Sprint(args ...interface{}) string
- func Sprintf(args ...interface{}) string
- func Sprintln(args ...interface{}) string
- type Attribute
- type Color
- type ColorWriter
- func New(attributes ...interface{}) *ColorWriter
- func NewColorWriter(c *color.Color) *ColorWriter
- func Set(attributes ...interface{}) *ColorWriter
- func SetError(out io.Writer) *ColorWriter
- func SetOut(out io.Writer) *ColorWriter
- func TryNew(attributes ...interface{}) (*ColorWriter, error)
- func TrySet(attributes ...interface{}) (*ColorWriter, error)
Examples ¶
Constants ¶
const ( Reset attribute = iota Bold Faint Italic Underline BlinkSlow BlinkRapid ReverseVideo Concealed CrossedOut )
The following constant are copied from the color package in order to get the actual string names.
const ( FgBlack attribute = iota + 30 FgRed FgGreen FgYellow FgBlue FgMagenta FgCyan FgWhite )
Foreground attributes
const ( FgHiBlack attribute = iota + 90 FgHiRed FgHiGreen FgHiYellow FgHiBlue FgHiMagenta FgHiCyan FgHiWhite )
Foreground attributes high intensity
const ( BgBlack attribute = iota + 40 BgRed BgGreen BgYellow BgBlue BgMagenta BgCyan BgWhite )
Background attributes
const ( BgHiBlack attribute = iota + 100 BgHiRed BgHiGreen BgHiYellow BgHiBlue BgHiMagenta BgHiCyan BgHiWhite )
Background attributes high intensity
Variables ¶
var ( Black = color.Black Red = color.Red Green = color.Green Yellow = color.Yellow Blue = color.Blue Magenta = color.Magenta Cyan = color.Cyan White = color.White HiBlack = color.HiBlack HiRed = color.HiRed HiGreen = color.HiGreen HiYellow = color.HiYellow HiBlue = color.HiBlue HiMagenta = color.HiMagenta HiCyan = color.HiCyan HiWhite = color.HiWhite BlackString = color.BlackString RedString = color.RedString GreenString = color.GreenString YellowString = color.YellowString BlueString = color.BlueString MagentaString = color.MagentaString CyanString = color.CyanString WhiteString = color.WhiteString HiBlackString = color.HiBlackString HiRedString = color.HiRedString HiGreenString = color.HiGreenString HiYellowString = color.HiYellowString HiBlueString = color.HiBlueString HiMagentaString = color.HiMagentaString HiCyanString = color.HiCyanString HiWhiteString = color.HiWhiteString Unset = color.Unset )
Imports fatih/color functions into the current package.
Functions ¶
func ErrorPrint ¶
ErrorPrint call standard fmt.Printf function but using the color out stream.
Example ¶
// We set the output because go test framework redirect os.Stdout and ignore os.Stderr SetError(os.Stdout) // Print is used to output to a color stream. ErrorPrint("Yellow", "Hello") // It behaves like regular fmt.Print. ErrorPrint("red", "This", "is", "concatenated", 1, 2, 3)
Output: HelloThisisconcatenated1 2 3
func ErrorPrintf ¶
ErrorPrintf call standard fmt.Printf function but using the color out stream.
Example ¶
// We set the output because go test framework redirect os.Stdout and ignore os.Stderr SetError(os.Stdout) // ErrorPrintf is used to output to a color stream. ErrorPrintf("Yellow", "Hello") // It behaves like regular fmt.Printf. // It must have a format string if there is more that one argument after the attributes. ErrorPrintf("red", "Hello %s %d\n", "world", 123) // Otherwise, the result is far from beautiful ;-). ErrorPrintf("red", "Hello", "world!", 1, math.Pi)
Output: HelloHello world 123 Hello%!!(MISSING)(EXTRA string=world!, int=1, float64=3.141592653589793)
func ErrorPrintln ¶
ErrorPrintln call standard fmt.Println function but using the color out stream.
Example ¶
// We set the output because go test framework redirect os.Stdout and ignore os.Stderr SetError(os.Stdout) // ErrorPrintln is used to output to a color stream. ErrorPrintln("Yellow", "Hello") // It behaves like regular fmt.Println. ErrorPrintln("red", "This", "is", "not", "concatenated", 1, 2, 3)
Output: Hello This is not concatenated 1 2 3
func FormatMessage ¶
func FormatMessage(args ...interface{}) string
FormatMessage analyses the arguments to determine if Sprintf or Sprintln should be used.
func Print ¶
Print call standard fmt.Printf function but using the color out stream.
Example ¶
// We set the output because go test framework redirect os.Stdout and ignore os.Stderr SetOut(os.Stdout) // Print is used to output to a color stream. Print("Yellow", "Hello") // It behaves like regular fmt.Print. Print("red", "This", "is", "concatenated", 1, 2, 3)
Output: HelloThisisconcatenated1 2 3
func Printf ¶
Printf behave as Printf, it expects to have a format string after the color attributes.
Example ¶
// We set the output because go test framework redirect os.Stdout and ignore os.Stderr SetOut(os.Stdout) // Printf is used to output to a color stream. Printf("Yellow", "Hello") // It behaves like regular fmt.Printf. // It must have a format string if there is more that one argument after the attributes. Printf("red", "Hello %s %d\n", "world", 123) // Otherwise, the result is far from beautiful ;-). Printf("red", "Hello", "world!", 1, math.Pi)
Output: HelloHello world 123 Hello%!!(MISSING)(EXTRA string=world!, int=1, float64=3.141592653589793)
func Println ¶
Println call standard fmt.Println function but using the color out stream.
Example ¶
// We set the output because go test framework redirect os.Stdout and ignore os.Stderr SetOut(os.Stdout) // Print is used to output to a color stream. Println("Yellow", "Hello") // It behaves like regular fmt.Println. Println("red", "This", "is", "not", "concatenated", 1, 2, 3)
Output: Hello This is not concatenated 1 2 3
func Sprint ¶
func Sprint(args ...interface{}) string
Sprint returns a string formated with attributes that are supplied before.
Example ¶
// Sprint is also very flexible. // You can use Sprint to format a message in color by prefixing the real text by color attributes. fmt.Println(Sprint("Yellow", color.Underline, "Hello")) // As with New, you can mix strings & color attribute. You can also use Sprint like a Sprintf function. // Color attributes following the message are not considered as attributes and are simply printed. fmt.Println(Sprint("yellow+underline", color.BlinkRapid, "color1=%v color2=%s", color.BgBlue, "red")) // If the arguments are not compatible with Sprintf, the result shows the error. fmt.Println(Sprint("HiRed", "Faint", "Wrong format %d %s", color.BgCyan)) // As with fmt.Sprint, strings are concatenated and first element following a string is also concatenated. fmt.Println(Sprint("Red, CrossedOut", color.BlinkRapid, "Hello", color.BgMagenta, "red", "BOLD", 1, 2, 3))
Output: Hello color1=44 color2=red Wrong format %d %s46 Hello45redBOLD1 2 3
func Sprintf ¶
func Sprintf(args ...interface{}) string
Sprintf returns a string formated with attributes that are supplied before.
func Sprintln ¶
func Sprintln(args ...interface{}) string
Sprintln returns a string formated with attributes that are supplied before.
Example ¶
// Sprint is also very flexible. // You can use Sprint to format a message in color by prefixing the real text by color attributes. fmt.Print(Sprintln("Yellow", color.Underline, "Hello")) // As with New, you can mix strings & color attribute. You can also use Sprint like a Sprintf function. // Color attributes following the message are not considered as attributes and are simply printed. fmt.Print(Sprintln("yellow+underline", color.BlinkRapid, "color1=%v color2=%s", color.BgBlue, "red")) // If the format argument appears to have a format, the function behave like a fmt.Sprintf followed by a fmt.Sprintln. fmt.Print(Sprintln("HiRed", "Faint", "Background Hi Magenta is %d", color.BgHiMagenta)) // If the arguments are not compatible with Sprintf, the result shows the error. fmt.Print(Sprintln("HiRed", "Faint", "Wrong format %d %s", color.BgCyan)) // As with fmt.Sprintln, all elements are separated by a space. fmt.Print(Sprintln("Red, CrossedOut", color.BlinkRapid, "Hello", color.BgMagenta, "red", "BOLD", 1, 2, 3))
Output: Hello color1=44 color2=red Background Hi Magenta is 105 Wrong format %d %s46 Hello 45 red BOLD 1 2 3
Types ¶
type Attribute ¶
Attribute is a copy of fatih/Attribute used to generate stringable attributes.
func Attributes ¶
func Attributes(attributes ...interface{}) []Attribute
Attributes convert any object representation to valid color attributes. It will panic if an invalid attribute is provided.
func TryConvertAttributes ¶
TryConvertAttributes tries to convert any object representation to valid color attribute. It returns an error if some parameters cannot be converted to valid attributes.
type ColorWriter ¶
ColorWriter is used as a regular color.Color object, but it is able to be used as io.Writer.
func New ¶
func New(attributes ...interface{}) *ColorWriter
New returns a ColorWriter build from supplied attribute names. This function will panic if invalid attributes are supplied.
Example ¶
// It accepts an enumeration of attributes. writer := New(color.FgGreen, color.Underline) fmt.Printf("%+v", *writer.Color)
Output: {params:[32 4] noColor:<nil>}
Example (Error) ¶
// But it panics if you supplied invalid attribute. func() { defer func() { fmt.Println(recover()) }() color := New("red, invalid | rouge") fmt.Printf("%+v", color) }()
Output: Attribute not found invalid Attribute not found rouge
Example (Mixup) ¶
// It can also mix strings and attributes writer := New(color.BgRed, "crossedout") fmt.Printf("%+v", *writer.Color)
Output: {params:[41 9] noColor:<nil>}
Example (String_separated) ¶
// Or separated by any non letter. Case is also non significant. writer := New("RED | underline, CrossedOUT+BgYellow") fmt.Printf("%+v", *writer.Color)
Output: {params:[31 4 9 43] noColor:<nil>}
Example (With_array) ¶
// It also accepts an array of attributes. writer := New([]color.Attribute{color.FgGreen, color.Underline}) fmt.Printf("%+v", *writer.Color)
Output: {params:[32 4] noColor:<nil>}
Example (With_strings) ¶
// It also accepts a list of string attributes. writer := New("FgHiGreen", "Underline", "BgYellow") fmt.Printf("%+v", *writer.Color)
Output: {params:[92 4 43] noColor:<nil>}
func NewColorWriter ¶
func NewColorWriter(c *color.Color) *ColorWriter
NewColorWriter creates a writeable color from a fatih Color object.
Example ¶
// We set the output because go test framework redirect os.Stdout and ignore os.Stderr SetOut(os.Stdout) c := color.New(color.FgYellow, color.BgHiWhite, color.Italic) SetOut(NewColorWriter(c)) Println("I should be colored")
Output: I should be colored
func Set ¶
func Set(attributes ...interface{}) *ColorWriter
Set changes the current output color for all following output to stdout. This function will panic if invalid attributes are supplied.
Example ¶
// It accepts an enumeration of attributes. Set("Red+BgYellow", color.Underline) fmt.Println("I should be colored") // But it panics if you supplied invalid attribute. func() { defer func() { fmt.Println(recover()) }() Set("red, invalid | rouge") }()
Output: I should be colored Attribute not found invalid Attribute not found rouge
func SetError ¶
func SetError(out io.Writer) *ColorWriter
SetError lets the user redefine the default writer used to print to stderr. This writer will then be used by functions multicolor.ErrorPrint, multicolor.ErrorPrintf and multicolor.ErrorPrintln.
The function returns the result color object if it is a colorable stream, otherwise, it returns nil.
Example ¶
// We set the output because go test framework redirect os.Stdout and ignore os.Stderr SetError(os.Stdout) SetError(New("BgRed+Green")) ErrorPrintln("I should be colored")
Output: I should be colored
func SetOut ¶
func SetOut(out io.Writer) *ColorWriter
SetOut lets the user redefine the default writer used to print to stdout. This writer will then be used by functions multicolor.Print, multicolor.Printf and multicolor.Println.
The function returns the result color object if it is a colorable stream, otherwise, it returns nil.
Example ¶
// We set the output because go test framework redirect os.Stdout and ignore os.Stderr SetOut(os.Stdout) SetOut(New("BgGreen+Yellow")) Println("I should be colored")
Output: I should be colored
func TryNew ¶
func TryNew(attributes ...interface{}) (*ColorWriter, error)
TryNew returns a ColorWriter build from supplied attribute names. If attributes cannot be converted into valid Attribute, an error is returned.
Example ¶
// You can avoid panic by using TryNew. if _, err := TryNew("FgBLUE Another invalid color BGYellow"); err != nil { fmt.Println(err) }
Output: Attribute not found Another Attribute not found invalid Attribute not found color
func TrySet ¶
func TrySet(attributes ...interface{}) (*ColorWriter, error)
TrySet changes the current output color for all following output to stdout. If attributes cannot be converted into valid Attribute, an error is returned.
Example ¶
// You can avoid panic by using TrySet. if _, err := TrySet("FgBLUE Another invalid color BGYellow"); err != nil { fmt.Println(err) }
Output: Attribute not found Another Attribute not found invalid Attribute not found color
func (*ColorWriter) SetError ¶
func (c *ColorWriter) SetError() *ColorWriter
SetError uses the current color as the default stderr for multicolor.ErrorPrint functions.
Example ¶
// We set the output because go test framework redirect os.Stdout and ignore os.Stderr SetOut(os.Stdout) SetError(os.Stdout) // SetOut can be called as a method on a Color object. It can even be used directly // on the same line. New("BgGreen+Yellow").SetError().Println("Hello!") ErrorPrint("I should be colored") Println(", while I shouldn't")
Output: Hello! I should be colored, while I shouldn't
func (*ColorWriter) SetOut ¶
func (c *ColorWriter) SetOut() *ColorWriter
SetOut uses the current color as the default stdout for multicolor.Print functions.
Example ¶
// We set the output because go test framework redirect os.Stdout and ignore os.Stderr SetOut(os.Stdout) // SetOut can be called as a method on a Color object. It can even be used directly // on the same line. New("BgGreen+Yellow").SetOut().Println("Hello!") Println("I should be colored")
Output: Hello! I should be colored
func (*ColorWriter) Write ¶
func (c *ColorWriter) Write(p []byte) (n int, err error)
Writer is the implementation of io.Writer. You should not call directly this function. The function will fail if called directly on a stream that have not been configured as out stream.
Example ¶
// We set the output because go test framework redirect os.Stdout and ignore os.Stderr SetOut(os.Stdout) color := New("BgGreen+Yellow") n, err := color.Write([]byte("Not set yet\n")) fmt.Printf("n=%d, err=%v\n", n, err) color = color.SetOut() n, err = color.Write([]byte("Should be ok\n")) fmt.Printf("n=%d, err=%v\n", n, err)
Output: n=0, err=Color is not configured as writer, call SetOut or SetError before using it Should be ok n=13, err=<nil>