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 ¶
- Variables
- func Black(format string, a ...any)
- func BlackString(format string, a ...any) string
- func Blue(format string, a ...any)
- func BlueString(format string, a ...any) string
- func Cyan(format string, a ...any)
- func CyanString(format string, a ...any) string
- func Green(format string, a ...any)
- func GreenString(format string, a ...any) string
- func HiBlack(format string, a ...any)
- func HiBlackString(format string, a ...any) string
- func HiBlue(format string, a ...any)
- func HiBlueString(format string, a ...any) string
- func HiCyan(format string, a ...any)
- func HiCyanString(format string, a ...any) string
- func HiGreen(format string, a ...any)
- func HiGreenString(format string, a ...any) string
- func HiMagenta(format string, a ...any)
- func HiMagentaString(format string, a ...any) string
- func HiRed(format string, a ...any)
- func HiRedString(format string, a ...any) string
- func HiWhite(format string, a ...any)
- func HiWhiteString(format string, a ...any) string
- func HiYellow(format string, a ...any)
- func HiYellowString(format string, a ...any) string
- func Magenta(format string, a ...any)
- func MagentaString(format string, a ...any) string
- func Red(format string, a ...any)
- func RedString(format string, a ...any) string
- func Unset()
- func White(format string, a ...any)
- func WhiteString(format string, a ...any) string
- func Yellow(format string, a ...any)
- func YellowString(format string, a ...any) string
- type Attribute
- type Color
- func (c *Color) Add(value ...Attribute) *Color
- func (c *Color) AddBgRGB(r, g, b int) *Color
- func (c *Color) AddRGB(r, g, b int) *Color
- func (c *Color) DisableColor()
- func (c *Color) EnableColor()
- func (c *Color) Equals(c2 *Color) bool
- func (c *Color) Fprint(w io.Writer, a ...any) (n int, err error)
- func (c *Color) FprintFunc() func(w io.Writer, a ...any)
- func (c *Color) Fprintf(w io.Writer, format string, a ...any) (n int, err error)
- func (c *Color) FprintfFunc() func(w io.Writer, format string, a ...any)
- func (c *Color) Fprintln(w io.Writer, a ...any) (n int, err error)
- func (c *Color) FprintlnFunc() func(w io.Writer, a ...any)
- func (c *Color) Print(a ...any) (n int, err error)
- func (c *Color) PrintFunc() func(a ...any)
- func (c *Color) Printf(format string, a ...any) (n int, err error)
- func (c *Color) PrintfFunc() func(format string, a ...any)
- func (c *Color) Println(a ...any) (n int, err error)
- func (c *Color) PrintlnFunc() func(a ...any)
- func (c *Color) SetWriter(w io.Writer) *Color
- func (c *Color) Sprint(a ...any) string
- func (c *Color) SprintFunc() func(a ...any) string
- func (c *Color) Sprintf(format string, a ...any) string
- func (c *Color) SprintfFunc() func(format string, a ...any) string
- func (c *Color) Sprintln(a ...any) string
- func (c *Color) SprintlnFunc() func(a ...any) string
- func (c *Color) UnsetWriter(w io.Writer)
Constants ¶
This section is empty.
Variables ¶
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 BlackString ¶
BlackString returns a string with black foreground.
func BlueString ¶
BlueString returns a string with blue foreground.
func CyanString ¶
CyanString returns a string with cyan foreground.
func GreenString ¶
GreenString returns a string with green foreground.
func HiBlackString ¶
HiBlackString returns a string with hi-intensity black foreground.
func HiBlueString ¶
HiBlueString returns a string with hi-intensity blue foreground.
func HiCyanString ¶
HiCyanString returns a string with hi-intensity cyan foreground.
func HiGreenString ¶
HiGreenString returns a string with hi-intensity green foreground.
func HiMagentaString ¶
HiMagentaString returns a string with hi-intensity magenta foreground.
func HiRedString ¶
HiRedString returns a string with hi-intensity red foreground.
func HiWhiteString ¶
HiWhiteString returns a string with hi-intensity white foreground.
func HiYellowString ¶
HiYellowString returns a string with hi-intensity yellow foreground.
func MagentaString ¶
MagentaString returns a string with magenta foreground.
func WhiteString ¶
WhiteString returns a string with white foreground.
func YellowString ¶
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.
Foreground text colors.
Background 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 Set ¶
Set sets the given SGR attributes on the global Output immediately. Call Unset() when done.
func (*Color) Add ¶
Add appends SGR attributes to an existing Color. Returns the receiver for chaining.
func (*Color) AddBgRGB ¶
AddBgRGB appends a 24-bit background color to an existing Color. Returns the receiver for chaining.
func (*Color) AddRGB ¶
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) Fprint ¶
Fprint formats and writes to w with color applied. Spaces are added between operands when neither is a string.
func (*Color) FprintFunc ¶
FprintFunc returns a function that writes colorized output to w using Fprint.
func (*Color) Fprintf ¶
Fprintf formats according to a format specifier and writes to w with color applied.
func (*Color) FprintfFunc ¶
FprintfFunc returns a function that writes colorized output to w using Fprintf.
func (*Color) Fprintln ¶
Fprintln formats and writes to w with color applied. A newline is always appended.
func (*Color) FprintlnFunc ¶
FprintlnFunc returns a function that writes colorized output to w using Fprintln.
func (*Color) PrintFunc ¶
PrintFunc returns a function that writes colorized output to Output using Print.
func (*Color) Printf ¶
Printf formats according to a format specifier and writes to Output with color applied.
func (*Color) PrintfFunc ¶
PrintfFunc returns a function that writes colorized output to Output using Printf.
func (*Color) Println ¶
Println formats and writes to Output with color applied. A newline is always appended.
func (*Color) PrintlnFunc ¶
PrintlnFunc returns a function that writes colorized output to Output using Println.
func (*Color) SprintFunc ¶
SprintFunc returns a function that returns the colorized string using Sprint.
func (*Color) Sprintf ¶
Sprintf formats according to a format specifier and returns the colorized string.
func (*Color) SprintfFunc ¶
SprintfFunc returns a function that returns the colorized string using Sprintf.
func (*Color) Sprintln ¶
Sprintln formats using the default formats and returns the colorized string with a trailing newline.
func (*Color) SprintlnFunc ¶
SprintlnFunc returns a function that returns the colorized string using Sprintln (with trailing newline).
func (*Color) UnsetWriter ¶
UnsetWriter writes the reset SGR sequence to w.