Skip to content

Repository files navigation

AtomicGo



AtomicGo | color

DownloadsLatest ReleaseTestsCoverageUnit test countLicense: MITGo report


Documentation | Contributing | Code of Conduct


go get atomicgo.dev/color

color

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"))
}

Index

Variables

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.Stdout

ANSI256Color represents a color in the ANSI256 color palette.

typeANSI256Coloruint8

func (ANSI256Color) Sequence

func (cANSI256Color) Sequence(backgroundbool) string

Sequence returns the ANSI escape sequence for the color.

func (ANSI256Color) String

func (cANSI256Color) String() string

String returns the hex code of the color.

ANSIColor represents an ANSI color code.

typeANSIColorint

ANSI color constants define the standard foreground color palette.

const (
ANSIBlackANSIColor=iotaANSIRedANSIGreenANSIYellowANSIBlueANSIMagentaANSICyanANSIWhiteANSIBrightBlackANSIBrightRedANSIBrightGreenANSIBrightYellowANSIBrightBlueANSIBrightMagentaANSIBrightCyanANSIBrightWhite
)

func (ANSIColor) Sequence

func (cANSIColor) Sequence(backgroundbool) string

Sequence 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{}

funcNewColorFromHex(hexstring) Color

NewColorFromHex 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.

funcNewColorFromRGB(r, g, buint8) Color

NewColorFromRGB creates a new Color from RGB values.

type Mode

Mode represents the color mode used by the terminal.

typeModeint

Color modes describe the terminal color capabilities.

const (
TrueColorMode=iotaANSI256ANSIDisabled
)

func (Mode) String

func (mMode) String() string

Modifier type for text modifiers.

typeModifierint

Modifiers

const (
ResetModifier=iotaBoldFaintItalicUnderline
)

func (Modifier) Sequence

func (mModifier) Sequence() string

Sequence returns the ANSI escape sequence for the modifier.

RGBColor represents a color in the RGB color space.

typeRGBColorstruct {
R, G, Buint8
}

func (RGBColor) Hex

func (cRGBColor) Hex() string

Hex returns the hex representation of the color.

func (RGBColor) Sequence

func (cRGBColor) Sequence(backgroundbool) string

Sequence 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
}

funcNewStyle(foregroundColor, backgroundColorColor, modifiers...Modifier) Style

NewStyle 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() string

Sequence returns the ANSI escape sequence for the style.

func (Style) Sprint

func (sStyle) Sprint(a...any) string

Sprint formats using the default formats for its operands and returns the resulting string.

func (Style) Sprintf

func (sStyle) Sprintf(formatstring, a...any) string

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

func (Style) WithModifier

func (sStyle) WithModifier(modifierModifier) Style

WithModifier 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

About

🎨 Simple colors for your terminal in Go

Topics

Resources

Code of conduct

Stars

4 stars

Watchers

1 watching

Forks

Releases

Sponsor this project

Packages

Used by

Contributors

Languages

Generated from atomicgo/template