Skip to content

ttkzw/go-color

Repository files navigation

go-color

This is a Go library for colorizing strings.

Usage

To give a color name, write as follows:

package main

import (
	"fmt"
	"log"

	"github.com/ttkzw/go-color"
)

func main() {
	c, err := color.NewColor("Green")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(c.Colorize("OK"))
}

To use built-in constants, write as follows:

package main

import (
	"fmt"

	"github.com/ttkzw/go-color"
)

func main() {
	fmt.Println(color.Green.Colorize("OK"))
}

To use a background color, write as follows:

package main

import (
	"fmt"

	"github.com/ttkzw/go-color"
)

func main() {
	fmt.Println(color.Colorize("OK", color.White, color.GreenBackground))
}

If you want to support Windows, use go-colorable.

package main

import (
	"fmt"

	"github.com/mattn/go-colorable"
	"github.com/ttkzw/go-color"
)

func main() {
	var output = colorable.NewColorableStdout()

	fmt.Fprintln(output, color.Green.Colorize("OK"))
}

Supported colors

Installation

$ go get github.com/ttkzw/go-color

License

BSD 3-Clause License

Auther

Takashi Takizawa