Documentation | Contributing | Code of Conduct
import"atomicgo.dev/color"Package color is a minimalistic package for coloring terminal output.
package main
import (
"fmt""atomicgo.dev/color"
)
funcmain() {
// Simple coloringfmt.Println("Hello, "+color.Green("World") +"!")
fmt.Println() // blank line// Theme colors - can be customized in init() function if neededfmt.Println(color.Success("Success message"))
fmt.Println(color.Info("Info message"))
fmt.Println(color.Warning("Warning message"))
fmt.Println(color.Error("Error message"))
fmt.Println(color.Fatal("Fatal message"))
fmt.Println() // blank line// Supports ANSI colorsansiRed:=color.NewStyle(color.ANSIRed, nil).Sprintfmt.Println(ansiRed("This is printed red using an ANSI color code"))
// Supports ANSI256 colorsansi256Red:=color.NewStyle(color.ANSI256Color(196), nil).Sprintfmt.Println(ansi256Red("This is printed red using an ANSI256 color code"))
// Supports RGB colorsredRGB:=color.NewStyle(color.NewColorFromRGB(255, 0, 0), nil).Sprintfmt.Println(redRGB("This is printed red using a RGB color code"))
// Supports hex colorsredHex:=color.NewStyle(color.NewColorFromHex("#ff0000"), nil).Sprintfmt.Println(redHex("This is printed red using a hex color code"))
}- Variables
- type ANSI256Color
- type ANSIColor
- type Color
- type Mode
- type Modifier
- type RGBColor
- type Style
- func NewStyle(foregroundColor, backgroundColor Color, modifiers ...Modifier) Style
- func (s *Style) AddModifier(modifier Modifier)
- func (s Style) Fprint(w io.Writer, a ...any) (n int, err error)
- func (s Style) Fprintf(w io.Writer, format string, a ...any) (n int, err error)
- func (s Style) Fprintfln(w io.Writer, format string, a ...any) (n int, err error)
- func (s Style) Fprintln(w io.Writer, a ...any) (n int, err error)
- func (s Style) Print(a ...any)
- func (s Style) Printf(format string, a ...any)
- func (s Style) Printfln(format string, a ...any)
- func (s Style) Println(a ...any)
- func (s Style) Sequence() string
- func (s Style) Sprint(a ...any) string
- func (s Style) Sprintf(format string, a ...any) string
- func (s Style) WithModifier(modifier Modifier) Style
Color shortcut functions apply common ANSI styles to text.
var (
Black=NewStyle(ANSIBlack, nil).SprintBrightBlack=NewStyle(ANSIBrightBlack, nil).SprintRed=NewStyle(ANSIRed, nil).SprintBrightRed=NewStyle(ANSIBrightRed, nil).SprintGreen=NewStyle(ANSIGreen, nil).SprintBrightGreen=NewStyle(ANSIBrightGreen, nil).SprintYellow=NewStyle(ANSIYellow, nil).SprintBrightYellow=NewStyle(ANSIBrightYellow, nil).SprintBlue=NewStyle(ANSIBlue, nil).SprintBrightBlue=NewStyle(ANSIBrightBlue, nil).SprintMagenta=NewStyle(ANSIMagenta, nil).SprintBrightMagenta=NewStyle(ANSIBrightMagenta, nil).SprintCyan=NewStyle(ANSICyan, nil).SprintBrightCyan=NewStyle(ANSIBrightCyan, nil).SprintWhite=NewStyle(ANSIWhite, nil).SprintBrightWhite=NewStyle(ANSIBrightWhite, nil).SprintSuccess=NewStyle(ANSIBrightGreen, nil).SprintInfo=NewStyle(ANSIBrightBlue, nil).SprintWarning=NewStyle(ANSIBrightYellow, nil).SprintError=NewStyle(ANSIBrightRed, nil).SprintFatal=NewStyle(ANSIBrightRed, nil, Bold).Sprint
)Writer is the writer to write colorized output to.
varWriter io.Writer=os.Stdouttype ANSI256Color
ANSI256Color represents a color in the ANSI256 color palette.
typeANSI256Coloruint8func (ANSI256Color) Sequence
func (cANSI256Color) Sequence(backgroundbool) stringSequence returns the ANSI escape sequence for the color.
func (ANSI256Color) String
func (cANSI256Color) String() stringString returns the hex code of the color.
type ANSIColor
ANSIColor represents an ANSI color code.
typeANSIColorintANSI color constants define the standard foreground color palette.
const (
ANSIBlackANSIColor=iotaANSIRedANSIGreenANSIYellowANSIBlueANSIMagentaANSICyanANSIWhiteANSIBrightBlackANSIBrightRedANSIBrightGreenANSIBrightYellowANSIBrightBlueANSIBrightMagentaANSIBrightCyanANSIBrightWhite
)func (ANSIColor) Sequence
func (cANSIColor) Sequence(backgroundbool) stringSequence represents the ANSI escape sequence for the color.
type Color
Color is an interface for colors.
typeColorinterface {
Sequence(backgroundbool) string
}NoColor disables color output for a style component.
varNoColorColor=noColor{}func NewColorFromHex
funcNewColorFromHex(hexstring) ColorNewColorFromHex creates a new Color from a hex string. Accepts both the 6-digit form ("#RRGGBB"/"RRGGBB") and the 3-digit shorthand ("#RGB"/"RGB"). If the hex string is invalid, NoColor is returned.
func NewColorFromRGB
funcNewColorFromRGB(r, g, buint8) ColorNewColorFromRGB creates a new Color from RGB values.
type Mode
Mode represents the color mode used by the terminal.
typeModeintColor modes describe the terminal color capabilities.
const (
TrueColorMode=iotaANSI256ANSIDisabled
)func (Mode) String
func (mMode) String() stringtype Modifier
Modifier type for text modifiers.
typeModifierintconst (
ResetModifier=iotaBoldFaintItalicUnderline
)func (Modifier) Sequence
func (mModifier) Sequence() stringSequence returns the ANSI escape sequence for the modifier.
type RGBColor
RGBColor represents a color in the RGB color space.
typeRGBColorstruct {
R, G, Buint8
}func (RGBColor) Hex
func (cRGBColor) Hex() stringHex returns the hex representation of the color.
func (RGBColor) Sequence
func (cRGBColor) Sequence(backgroundbool) stringSequence returns the ANSI escape sequence for the color.
type Style
Style represents a text style with a foreground and background color and modifiers.
typeStylestruct {
ForegroundColorBackgroundColorModifiers []Modifier
}func NewStyle
funcNewStyle(foregroundColor, backgroundColorColor, modifiers...Modifier) StyleNewStyle creates a new Style with the given foreground and background colors and modifiers.
func (*Style) AddModifier
func (s*Style) AddModifier(modifierModifier)AddModifier adds a modifier to the style, if it's not already present.
func (Style) Fprint
func (sStyle) Fprint(w io.Writer, a...any) (nint, errerror)Fprint formats using the default formats for its operands and writes to w.
func (Style) Fprintf
func (sStyle) Fprintf(w io.Writer, formatstring, a...any) (nint, errerror)Fprintf formats according to a format specifier and writes to w.
func (Style) Fprintfln
func (sStyle) Fprintfln(w io.Writer, formatstring, a...any) (nint, errerror)Fprintfln formats according to a format specifier and writes to w.
func (Style) Fprintln
func (sStyle) Fprintln(w io.Writer, a...any) (nint, errerror)Fprintln formats using the default formats for its operands and writes to w.
func (Style) Print
func (sStyle) Print(a...any)Print formats using the default formats for its operands and writes to standard output.
func (Style) Printf
func (sStyle) Printf(formatstring, a...any)Printf formats according to a format specifier and writes to standard output.
func (Style) Printfln
func (sStyle) Printfln(formatstring, a...any)Printfln formats according to a format specifier and writes to standard output.
func (Style) Println
func (sStyle) Println(a...any)Println formats using the default formats for its operands and writes to standard output.
func (Style) Sequence
func (sStyle) Sequence() stringSequence returns the ANSI escape sequence for the style.
func (Style) Sprint
func (sStyle) Sprint(a...any) stringSprint formats using the default formats for its operands and returns the resulting string.
func (Style) Sprintf
func (sStyle) Sprintf(formatstring, a...any) stringSprintf formats according to a format specifier and returns the resulting string.
func (Style) WithModifier
func (sStyle) WithModifier(modifierModifier) StyleWithModifier returns a new Style with the given modifier added, if it's not already present.
Generated by gomarkdoc
AtomicGo.dev · with ❤️ by @MarvinJWendt | mjw.dev
