color

package module
v0.0.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2026 License: MIT Imports: 8 Imported by: 0

README

Color

Color provides colorized terminal output in Go using ANSI escape codes.

Install

go get github.com/xogas/color

Examples

Standard colors

color.Cyan("Prints text in cyan.")
color.Blue("Prints %s in blue.", "text")
color.Red("We have red")
color.Yellow("Yellow color too!")
color.Magenta("And many others ..")

// Hi-intensity variants
color.HiGreen("Bright green color.")
color.HiBlack("Bright black means gray..")
color.HiWhite("Shiny white color!")

RGB colors

color.RGB(255, 128, 0).Println("foreground orange")
color.RGB(230, 42, 42).Println("foreground red")

color.BgRGB(255, 128, 0).Println("background orange")
color.BgRGB(230, 42, 42).Println("background red")

Func-returning variants

red := color.New(color.FgRed).PrintfFunc()
red("Error: %s\n", err)

// Mix up multiple attributes
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
notice("Don't forget this...")

Writing to an io.Writer

color.New(color.FgBlue).Fprintln(myWriter, "blue color!")

blue := color.New(color.FgBlue).FprintFunc()
blue(myWriter, "blue notice")

Plug into existing code

color.Set(color.FgYellow)
fmt.Println("This line is yellow")
color.Unset()

Disable / Enable color

color.NoColor = true  // disable globally

c := color.New(color.FgCyan)
c.DisableColor()
c.Println("plain text")
c.EnableColor()
c.Println("cyan again")

Licence

The MIT License (MIT) - see license for more details.

Documentation

Overview

Package color provides ANSI escape code–based colored terminal output for Go. The API can be used in several ways; pick the style that suits you.

Simple helper functions

Use predefined foreground-color helpers that automatically append a newline:

color.Cyan("Prints text in cyan.")
color.Blue("Prints %s in blue.", "text")
color.Red("We have red")
color.Yellow("Yellow color too!")
color.Magenta("And many others ..")

// Hi-intensity variants
color.HiGreen("Bright green color.")
color.HiBlack("Bright black means gray..")
color.HiWhite("Shiny white color!")

Custom Color objects

For reusable, mixed-attribute styles:

c := color.New(color.FgCyan).Add(color.Underline)
c.Println("Prints cyan text with an underline.")

// Or chain attributes directly in New()
c = color.New(color.Bold, color.FgGreen)
c.Printf("Bold green: %s\n", "hello")

24-bit RGB colors

When your terminal supports true color:

color.RGB(255, 128, 0).Println("foreground orange")
color.BgRGB(0, 0, 128).Println("dark blue background")

Func-returning variants (for reuse / embedding)

red := color.New(color.FgRed).PrintfFunc()
red("Error: %s\n", err)

yellow := color.New(color.FgYellow).SprintFunc()
fmt.Printf("this is a %s\n", yellow("warning"))

Writing to an io.Writer

blue := color.New(color.FgBlue).FprintFunc()
blue(myWriter, "blue notice")

Plug into existing code (Set / Unset)

color.Set(color.FgYellow)
fmt.Println("This line is yellow")
color.Unset()

Disable / Enable color

Color output is automatically disabled when stdout is not a terminal, when TERM=dumb, or when the NO_COLOR environment variable is set (see https://no-color.org). You can also control it programmatically:

color.NoColor = true  // disable globally

c := color.New(color.FgCyan)
c.DisableColor()
c.Println("plain text")
c.EnableColor()
c.Println("cyan again")

Index

Constants

This section is empty.

Variables

View Source
var (
	// NoColor controls whether color output is enabled. It is automatically set
	// to true when stdout is not a terminal, when TERM=dumb or when NO_COLOR is
	// set. Override it at any time to force-enable or force-disable colors.
	NoColor = noColorIsSet() || os.Getenv("TERM") == "dumb" || !isTerminal()

	// Output is the writer used by all package-level Print functions. Defaults
	// to os.Stdout.
	Output io.Writer = os.Stdout

	// Error is a color-supporting writer for os.Stderr.
	Error io.Writer = os.Stderr
)

Functions

func Black

func Black(format string, a ...any)

Black prints with black foreground; a newline is appended automatically.

func BlackString

func BlackString(format string, a ...any) string

BlackString returns a string with black foreground.

func Blue

func Blue(format string, a ...any)

Blue prints with blue foreground; a newline is appended automatically.

func BlueString

func BlueString(format string, a ...any) string

BlueString returns a string with blue foreground.

func Cyan

func Cyan(format string, a ...any)

Cyan prints with cyan foreground; a newline is appended automatically.

func CyanString

func CyanString(format string, a ...any) string

CyanString returns a string with cyan foreground.

func Green

func Green(format string, a ...any)

Green prints with green foreground; a newline is appended automatically.

func GreenString

func GreenString(format string, a ...any) string

GreenString returns a string with green foreground.

func HiBlack

func HiBlack(format string, a ...any)

HiBlack prints with hi-intensity black foreground; a newline is appended.

func HiBlackString

func HiBlackString(format string, a ...any) string

HiBlackString returns a string with hi-intensity black foreground.

func HiBlue

func HiBlue(format string, a ...any)

HiBlue prints with hi-intensity blue foreground; a newline is appended.

func HiBlueString

func HiBlueString(format string, a ...any) string

HiBlueString returns a string with hi-intensity blue foreground.

func HiCyan

func HiCyan(format string, a ...any)

HiCyan prints with hi-intensity cyan foreground; a newline is appended.

func HiCyanString

func HiCyanString(format string, a ...any) string

HiCyanString returns a string with hi-intensity cyan foreground.

func HiGreen

func HiGreen(format string, a ...any)

HiGreen prints with hi-intensity green foreground; a newline is appended.

func HiGreenString

func HiGreenString(format string, a ...any) string

HiGreenString returns a string with hi-intensity green foreground.

func HiMagenta

func HiMagenta(format string, a ...any)

HiMagenta prints with hi-intensity magenta foreground; a newline is appended.

func HiMagentaString

func HiMagentaString(format string, a ...any) string

HiMagentaString returns a string with hi-intensity magenta foreground.

func HiRed

func HiRed(format string, a ...any)

HiRed prints with hi-intensity red foreground; a newline is appended.

func HiRedString

func HiRedString(format string, a ...any) string

HiRedString returns a string with hi-intensity red foreground.

func HiWhite

func HiWhite(format string, a ...any)

HiWhite prints with hi-intensity white foreground; a newline is appended.

func HiWhiteString

func HiWhiteString(format string, a ...any) string

HiWhiteString returns a string with hi-intensity white foreground.

func HiYellow

func HiYellow(format string, a ...any)

HiYellow prints with hi-intensity yellow foreground; a newline is appended.

func HiYellowString

func HiYellowString(format string, a ...any) string

HiYellowString returns a string with hi-intensity yellow foreground.

func Magenta

func Magenta(format string, a ...any)

Magenta prints with magenta foreground; a newline is appended automatically.

func MagentaString

func MagentaString(format string, a ...any) string

MagentaString returns a string with magenta foreground.

func Red

func Red(format string, a ...any)

Red prints with red foreground; a newline is appended automatically.

func RedString

func RedString(format string, a ...any) string

RedString returns a string with red foreground.

func Unset

func Unset()

Unset resets all SGR attributes on the global Output.

func White

func White(format string, a ...any)

White prints with white foreground; a newline is appended automatically.

func WhiteString

func WhiteString(format string, a ...any) string

WhiteString returns a string with white foreground.

func Yellow

func Yellow(format string, a ...any)

Yellow prints with yellow foreground; a newline is appended automatically.

func YellowString

func YellowString(format string, a ...any) string

YellowString returns a string with yellow foreground.

Types

type Attribute

type Attribute int

Attribute defines a single SGR (Select Graphic Rendition) code.

const (
	Reset Attribute = iota
	Bold
	Faint
	Italic
	Underline
	BlinkSlow
	BlinkRapid
	ReverseVideo
	Concealed
	CrossedOut
)

Base attributes.

const (
	ResetBold Attribute = iota + 22
	ResetItalic
	ResetUnderline
	ResetBlinking

	ResetReversed
	ResetConcealed
	ResetCrossedOut
)

Reset attributes matching each base attribute.

const (
	FgBlack Attribute = iota + 30
	FgRed
	FgGreen
	FgYellow
	FgBlue
	FgMagenta
	FgCyan
	FgWhite
)

Foreground text colors.

const (
	BgBlack Attribute = iota + 40
	BgRed
	BgGreen
	BgYellow
	BgBlue
	BgMagenta
	BgCyan
	BgWhite
)

Background text colors.

const (
	FgHiBlack Attribute = iota + 90
	FgHiRed
	FgHiGreen
	FgHiYellow
	FgHiBlue
	FgHiMagenta
	FgHiCyan
	FgHiWhite
)

Foreground hi-intensity text colors.

const (
	BgHiBlack Attribute = iota + 100
	BgHiRed
	BgHiGreen
	BgHiYellow
	BgHiBlue
	BgHiMagenta
	BgHiCyan
	BgHiWhite
)

Background hi-intensity text colors.

type Color

type Color struct {
	// contains filtered or unexported fields
}

Color defines a custom color object composed of one or more SGR parameters.

func BgRGB

func BgRGB(r, g, b int) *Color

BgRGB returns a new background color using 24-bit true color (r, g, b in 0-255).

func New

func New(value ...Attribute) *Color

New returns a new Color with the given SGR attributes.

func RGB

func RGB(r, g, b int) *Color

RGB returns a new foreground color using 24-bit true color (r, g, b in 0-255).

func Set

func Set(p ...Attribute) *Color

Set sets the given SGR attributes on the global Output immediately. Call Unset() when done.

func (*Color) Add

func (c *Color) Add(value ...Attribute) *Color

Add appends SGR attributes to an existing Color. Returns the receiver for chaining.

func (*Color) AddBgRGB

func (c *Color) AddBgRGB(r, g, b int) *Color

AddBgRGB appends a 24-bit background color to an existing Color. Returns the receiver for chaining.

func (*Color) AddRGB

func (c *Color) AddRGB(r, g, b int) *Color

AddRGB appends a 24-bit foreground color to an existing Color. Returns the receiver for chaining.

func (*Color) DisableColor

func (c *Color) DisableColor()

DisableColor disables color output for this Color instance.

func (*Color) EnableColor

func (c *Color) EnableColor()

EnableColor re-enables color output for this Color instance.

func (*Color) Equals

func (c *Color) Equals(c2 *Color) bool

Equals reports whether two Colors have identical SGR parameters.

func (*Color) Fprint

func (c *Color) Fprint(w io.Writer, a ...any) (n int, err error)

Fprint formats and writes to w with color applied. Spaces are added between operands when neither is a string.

func (*Color) FprintFunc

func (c *Color) FprintFunc() func(w io.Writer, a ...any)

FprintFunc returns a function that writes colorized output to w using Fprint.

func (*Color) Fprintf

func (c *Color) Fprintf(w io.Writer, format string, a ...any) (n int, err error)

Fprintf formats according to a format specifier and writes to w with color applied.

func (*Color) FprintfFunc

func (c *Color) FprintfFunc() func(w io.Writer, format string, a ...any)

FprintfFunc returns a function that writes colorized output to w using Fprintf.

func (*Color) Fprintln

func (c *Color) Fprintln(w io.Writer, a ...any) (n int, err error)

Fprintln formats and writes to w with color applied. A newline is always appended.

func (*Color) FprintlnFunc

func (c *Color) FprintlnFunc() func(w io.Writer, a ...any)

FprintlnFunc returns a function that writes colorized output to w using Fprintln.

func (*Color) Print

func (c *Color) Print(a ...any) (n int, err error)

Print formats and writes to Output with color applied.

func (*Color) PrintFunc

func (c *Color) PrintFunc() func(a ...any)

PrintFunc returns a function that writes colorized output to Output using Print.

func (*Color) Printf

func (c *Color) Printf(format string, a ...any) (n int, err error)

Printf formats according to a format specifier and writes to Output with color applied.

func (*Color) PrintfFunc

func (c *Color) PrintfFunc() func(format string, a ...any)

PrintfFunc returns a function that writes colorized output to Output using Printf.

func (*Color) Println

func (c *Color) Println(a ...any) (n int, err error)

Println formats and writes to Output with color applied. A newline is always appended.

func (*Color) PrintlnFunc

func (c *Color) PrintlnFunc() func(a ...any)

PrintlnFunc returns a function that writes colorized output to Output using Println.

func (*Color) SetWriter

func (c *Color) SetWriter(w io.Writer) *Color

SetWriter writes the opening SGR sequence to w.

func (*Color) Sprint

func (c *Color) Sprint(a ...any) string

Sprint formats using the default formats and returns the colorized string.

func (*Color) SprintFunc

func (c *Color) SprintFunc() func(a ...any) string

SprintFunc returns a function that returns the colorized string using Sprint.

func (*Color) Sprintf

func (c *Color) Sprintf(format string, a ...any) string

Sprintf formats according to a format specifier and returns the colorized string.

func (*Color) SprintfFunc

func (c *Color) SprintfFunc() func(format string, a ...any) string

SprintfFunc returns a function that returns the colorized string using Sprintf.

func (*Color) Sprintln

func (c *Color) Sprintln(a ...any) string

Sprintln formats using the default formats and returns the colorized string with a trailing newline.

func (*Color) SprintlnFunc

func (c *Color) SprintlnFunc() func(a ...any) string

SprintlnFunc returns a function that returns the colorized string using Sprintln (with trailing newline).

func (*Color) UnsetWriter

func (c *Color) UnsetWriter(w io.Writer)

UnsetWriter writes the reset SGR sequence to w.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL