https://github.com/jwalton/gchalk is a library heavily inspired by chalk, the popular Node.js terminal color library, and using go ports of supports-color and ansi-styles.
Features
- Expressive API
- Highly performant
- Ability to nest styles
- 256/Truecolor color support with automatic conversion if not supported
- Auto-detects color support
- Painless Windows 10 support
Feature Comparison
Feature | gchalk | aurora | fatih/color | mgutz/ansi |
---|---|---|---|---|
TTY Detection | ✅ | ❌ | ✅ (1) | ❌ |
Color Detection | ✅ | ❌ | ❌ | ❌ |
Windows 10 | ✅ | ❌ | ✅ (2) | ❌ |
Nested Styles | ✅ | ✅ (3) | ❌ | ❌ |
256 Color Support | ✅ | ✅ (4) | ❌ | ✅ (4) |
16.7m Color Support | ✅ | ❌ | ❌ | ❌ |
Speed | 70ns | 196ns | 420ns | 40ns |
- fatih/color supports automatic TTY detection, but assumes that if stdout is not a TTY, then stderr is also not a TTY, which may not be true.
- fatih/color supports Windows 10, but you need to write to a special stream.
- aurora supports nested styles via its custom
Sprintf()
, but you can’t convert things to a string first – need to keep everything as auroraValue
s. - aurora and mgutz/ansi both support 256 color output, but they don’t detect whether the terminal supports it or not, and won’t automatically convert 256 color output to 16 color output if it doesn’t.
Ussage
package main
import (
"fmt"
"github.com/jwalton/gchalk"
)
func main() {
fmt.Println(gchalk.Blue("This line is blue"))
fmt.Println(gchalk.Blue("Hello") + " World" + gchalk.Red("!"))
fmt.Println(gchalk.WithBlue().WithBgRed().Bold("Hello world!"))
fmt.Println(gchalk.WithRGB(123, 45, 67).Underline("Underlined reddish color"))
fmt.Println(gchalk.WihHex("#DEADED").Bold("Bold gray!"))
fmt.Println(gchalk.StyleMust("blue")("Hello World!"))
os.Stderr.WriteString(gchalk.Stderr.Red("Ohs noes!\n"))
var error = gchalk.WithBold().Red
var warning = gchalk.Yellow
fmt.Println(error("Error!"))
fmt.Println(warning("Warning!"))
}