A lightweight Go package for colorful and styled terminal output with ANSI escape sequences support.
- Support for all 16 basic ANSI colors (8 normal + 8 bright) for foreground and background
- Text styling: bold, italic, underline, dim
- Automatic terminal capability detection
- Environment variable support (NO_COLOR, FORCE_COLOR)
- Thread-safe operations with optimized caching
- Cross-platform compatibility (Unix/Linux/macOS, Windows 10+)
- Zero dependencies
go get github.com/haadi-coder/colorpackage main
import (
"fmt"
"github.com/haadi-coder/color"
)
func main() {
fmt.Println(color.Red("Error: file not found"))
fmt.Println(color.Green("Success: operation completed"))
fmt.Println(color.Yellow("Warning: deprecated method"))
fmt.Println(color.Blue("Info: processed 100 files"))
}// Combining multiple styles
fmt.Println(color.Style("Critical Error", color.AttrFgRed, color.AttrBold, color.AttrUnderline))
// Background colors
fmt.Println(color.Style("Alert", color.AttrFgWhite, color.AttrBgRed, color.AttrBold))
// Bright colors
fmt.Println(color.BrightGreen("Tests Passed"))
fmt.Println(color.BrightRed("Critical Issue"))
// Formatted output
fmt.Println(color.Redf("Error: %s", err.Error()))
fmt.Println(color.Greenf("Processed %d of %d files", processed, total))// Global color control
color.NoColor = true // Disable all colors
color.ForceColor = true // Force colors even in non-TTY
// Check terminal capabilities
if color.IsTerminal() && color.SupportsColor() {
fmt.Println(color.Green("Terminal supports colors"))
} else {
fmt.Println("Plain text output")
}Red(text string) string- Red textGreen(text string) string- Green textYellow(text string) string- Yellow textBlue(text string) string- Blue textMagenta(text string) string- Magenta textCyan(text string) string- Cyan textWhite(text string) string- White textBlack(text string) string- Black text
BrightRed(text string) string- Bright red textBrightGreen(text string) string- Bright green textBrightYellow(text string) string- Bright yellow textBrightBlue(text string) string- Bright blue textBrightMagenta(text string) string- Bright magenta textBrightCyan(text string) string- Bright cyan textBrightWhite(text string) string- Bright white textBrightBlack(text string) string- Bright black text
BgRed(text string) string- Red backgroundBgGreen(text string) string- Green backgroundBgYellow(text string) string- Yellow backgroundBgBlue(text string) string- Blue backgroundBgMagenta(text string) string- Magenta backgroundBgCyan(text string) string- Cyan backgroundBgWhite(text string) string- White backgroundBgBlack(text string) string- Black background
BgBrightRed(text string) string- Bright red backgroundBgBrightGreen(text string) string- Bright green backgroundBgBrightYellow(text string) string- Bright yellow backgroundBgBrightBlue(text string) string- Bright blue backgroundBgBrightMagenta(text string) string- Bright magenta backgroundBgBrightCyan(text string) string- Bright cyan backgroundBgBrightWhite(text string) string- Bright white backgroundBgBrightBlack(text string) string- Bright black background
Bold(text string) string- Bold textItalic(text string) string- Italic textUnderline(text string) string- Underlined textDim(text string) string- Dimmed text
Redf(format string, a ...interface{}) string- Red formatted textGreenf(format string, a ...interface{}) string- Green formatted textYellowf(format string, a ...interface{}) string- Yellow formatted textBluef(format string, a ...interface{}) string- Blue formatted textMagentaf(format string, a ...interface{}) string- Magenta formatted textCyanf(format string, a ...interface{}) string- Cyan formatted textWhitef(format string, a ...interface{}) string- White formatted textBlackf(format string, a ...interface{}) string- Black formatted text
Style(text string, attrs ...Attr) string- Apply multiple attributesIsTerminal() bool- Check if output is connected to terminalSupportsColor() bool- Check if terminal supports colors
type Attr int
const (
// Foreground colors
AttrFgBlack, AttrFgRed, AttrFgGreen, AttrFgYellow,
AttrFgBlue, AttrFgMagenta, AttrFgCyan, AttrFgWhite,
// Bright foreground colors
AttrFgBrightBlack, AttrFgBrightRed, AttrFgBrightGreen, AttrFgBrightYellow,
AttrFgBrightBlue, AttrFgBrightMagenta, AttrFgBrightCyan, AttrFgBrightWhite,
// Background colors
AttrBgBlack, AttrBgRed, AttrBgGreen, AttrBgYellow,
AttrBgBlue, AttrBgMagenta, AttrBgCyan, AttrBgWhite,
// Bright background colors
AttrBgBrightBlack, AttrBgBrightRed, AttrBgBrightGreen, AttrBgBrightYellow,
AttrBgBrightBlue, AttrBgBrightMagenta, AttrBgBrightCyan, AttrBgBrightWhite,
// Text styles
AttrBold, AttrItalic, AttrUnderline, AttrDim
)NoColor bool- Disable all colors (also controlled by NO_COLOR env var)ForceColor bool- Force colors even in non-TTY (also controlled by FORCE_COLOR env var)
NO_COLOR=1- Disable all colors (follows no-color.org standard)FORCE_COLOR=1- Force colors even when not in terminalTERM=dumb- Automatically disables colorsCOLORTERM=truecolor- Enables color support
- Unix/Linux/macOS: Full support out of the box
- Windows 10+: Full support with modern terminals
- Windows Terminal: Full support
- Git Bash: Full support
- WSL: Full support as Unix environment
- Older Windows: Colors automatically disabled, use
FORCE_COLOR=1if needed
- ANSI sequences are pre-computed and cached
- Thread-safe operations with optimized locking
- Minimal memory allocations for typical usage
- Efficient string building with
strings.Builder