John Olheiser
5e1722bd43
Simplify example Update colors Add wrapping Signed-off-by: jolheiser <john.olheiser@gmail.com> Co-authored-by: jolheiser <john.olheiser@gmail.com> Reviewed-on: https://gitea.com/jolheiser/beaver/pulls/2 |
||
---|---|---|
.. | ||
README.md | ||
attribute.go | ||
color.go | ||
color_test.go |
README.md
Color
Beaver comes with the color
sub-package that can be used even without Beaver calls.
Formatting a string with a single attribute
text := color.FgRed.Format("red")
Formatting a string with a full color
text := color.New(color.BgGreen, color.FgRed, color.Bold).Format("green background, red text, and bold")
Formatting strings with multiple colors
The following are different ways to print This is a color test!
where the word color
is red while everything else is green
.
setup
Assuming each example is preceded by
green := color.New(color.FgGreen)
red := color.New(color.FgRed)
pure fmt.Println
fmt.Println(green.Format("This is a"), red.Format("color"), green.Format("test!"))
color.Wrap
fmt.Println(green.Wrap("This is a #{color} test!", red))
string formatting with fmt.Printf
and color.Wrap
fmt.Printf(green.Wrap("This is a #{%s} test!\n", red), "color")
using a different directive prefix
color.DirectivePrefix('$')
fmt.Printf(green.Wrap("This is a ${%s} test!\n", red), "color")