Documentation
¶
Overview ¶
Package color is a minimalistic package for coloring terminal output.
Example (Demo) ¶
package main import ( "fmt" "atomicgo.dev/color" ) func main() { // Simple coloring fmt.Println("Hello, " + color.Green("World") + "!") fmt.Println() // blank line // Theme colors - can be customized in init() function if needed fmt.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 colors ansiRed := color.NewStyle(color.ANSIRed, nil).Sprint fmt.Println(ansiRed("This is printed red using an ANSI color code")) // Supports ANSI256 colors ansi256Red := color.NewStyle(color.ANSI256Color(196), nil).Sprint fmt.Println(ansi256Red("This is printed red using an ANSI256 color code")) // Supports RGB colors redRGB := color.NewStyle(color.NewColorFromRGB(255, 0, 0), nil).Sprint fmt.Println(redRGB("This is printed red using a RGB color code")) // Supports hex colors redHex := color.NewStyle(color.NewColorFromHex("#ff0000"), nil).Sprint fmt.Println(redHex("This is printed red using a hex color code")) }
Output:
Index ¶
- Variables
- type ANSI256Color
- type ANSIColor
- type Color
- type Mode
- type Modifier
- type RGBColor
- type 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
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // ANSI colors Black = NewStyle(ANSIBlack, nil).Sprint BrightBlack = NewStyle(ANSIBrightBlack, nil).Sprint Red = NewStyle(ANSIRed, nil).Sprint BrightRed = NewStyle(ANSIBrightRed, nil).Sprint Green = NewStyle(ANSIGreen, nil).Sprint BrightGreen = NewStyle(ANSIBrightGreen, nil).Sprint Yellow = NewStyle(ANSIYellow, nil).Sprint BrightYellow = NewStyle(ANSIBrightYellow, nil).Sprint Blue = NewStyle(ANSIBlue, nil).Sprint BrigthBlue = NewStyle(ANSIBrightBlue, nil).Sprint Magenta = NewStyle(ANSIMagenta, nil).Sprint BrightMagenta = NewStyle(ANSIBrightMagenta, nil).Sprint Cyan = NewStyle(ANSICyan, nil).Sprint BrightCyan = NewStyle(ANSIBrightCyan, nil).Sprint White = NewStyle(ANSIWhite, nil).Sprint BrightWhite = NewStyle(ANSIBrightWhite, nil).Sprint // Special colors Success = NewStyle(ANSIBrightGreen, nil).Sprint Info = NewStyle(ANSIBrightBlue, nil).Sprint Warning = NewStyle(ANSIBrightYellow, nil).Sprint Error = NewStyle(ANSIBrightRed, nil).Sprint Fatal = NewStyle(ANSIBrightRed, nil, Bold).Sprint )
var Writer io.Writer = os.Stdout
Writer is the writer to write colorized output to.
Functions ¶
This section is empty.
Types ¶
type ANSI256Color ¶
type ANSI256Color uint8
ANSI256Color represents a color in the ANSI256 color palette.
func (ANSI256Color) Sequence ¶
func (c ANSI256Color) Sequence(background bool) string
Sequence returns the ANSI escape sequence for the color.
func (ANSI256Color) String ¶
func (c ANSI256Color) String() string
String returns the hex code of the color.
type Color ¶
Color is an interface for colors.
var NoColor Color = noColor{}
func NewColorFromHex ¶
NewColorFromHex creates a new Color from a hex string. If the hex string is invalid, NoColor is returned.
func NewColorFromRGB ¶
NewColorFromRGB creates a new Color from RGB values.
type RGBColor ¶
type RGBColor struct {
R, G, B uint8
}
RGBColor represents a color in the RGB color space.
type Style ¶
Style represents a text style with a foreground and background color and modifiers.
func NewStyle ¶
NewStyle creates a new Style with the given foreground and background colors and modifiers.
func (*Style) AddModifier ¶
AddModifier adds a modifier to the style, if it's not already present.
func (Style) Fprintln ¶
Fprintln formats using the default formats for its operands and writes to w.
func (Style) Print ¶
Print formats using the default formats for its operands and writes to standard output.
func (Style) Printfln ¶
Printfln formats according to a format specifier and writes to standard output.
func (Style) Println ¶
Println formats using the default formats for its operands and writes to standard output.
func (Style) Sprint ¶
Sprint formats using the default formats for its operands and returns the resulting string.
func (Style) Sprintf ¶
Sprintf formats according to a format specifier and returns the resulting string.
func (Style) WithModifier ¶
WithModifier returns a new Style with the given modifier added, if it's not already present.