Palette is a simple library to add ANSI colors to command line output.
Under the hood, Palette uses fmt.Sprintf function to format input, bytes.Buffer struct to concatenate strings, and bytes.Buffer.WriteTo method to write output. It does not use traditional fmt functions to write.
See documentation for more information.
- Includes 5 text styles and 32 text colors including foreground and background.
- Includes 5 pre-defined printers.
- You can set output writer. Writer must implements io.Writer interface. Default writer is os.Stdout.
- You can chain everything in one line using setters.
You can add Palette to your code using the go get
command.
go get github.com/enindu/palette
Here's an example of how to use Palette.
package main
import (
"os"
"github.com/enindu/palette"
)
func main() {
p := palette.NewPrinter(palette.FgRed, palette.BgRegular, palette.StBold, palette.StUnderline).SetWriter(os.Stderr)
p.Print("hello world!\n")
}
Note that Palette is an experimental library. It is not even close to competing with other well-designed libraries. My sole aim was to create a lightweight library with minimal allocations and faster execution for use in hot paths.
$ go test -bench . -benchtime 10s -benchmem
goos: linux
goarch: amd64
pkg: github.com/enindu/palette
cpu: AMD Ryzen 5 5600H with Radeon Graphics
BenchmarkEninduPalette-12 20581165 582.0 ns/op 216 B/op 6 allocs/op
BenchmarkFatihColor-12 7810953 1535.0 ns/op 272 B/op 10 allocs/op
BenchmarkGookitColor-12 15036194 777.3 ns/op 300 B/op 9 allocs/op
PASS
ok github.com/enindu/palette 38.638s