color

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 1, 2025 License: MIT Imports: 5 Imported by: 0

README

AtomicGo | color

Downloads Latest Release Tests Coverage Unit test count Go report


Documentation | Contributing | Code of Conduct


AtomicGo

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

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

Index

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
)

Writer is the writer to write colorized output to.

var Writer io.Writer = os.Stdout

type ANSI256Color

ANSI256Color represents a color in the ANSI256 color palette.

type ANSI256Color uint8

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 ANSIColor

ANSIColor represents an ANSI color code.

type ANSIColor int

const (
    ANSIBlack ANSIColor = iota
    ANSIRed
    ANSIGreen
    ANSIYellow
    ANSIBlue
    ANSIMagenta
    ANSICyan
    ANSIWhite
    ANSIBrightBlack
    ANSIBrightRed
    ANSIBrightGreen
    ANSIBrightYellow
    ANSIBrightBlue
    ANSIBrightMagenta
    ANSIBrightCyan
    ANSIBrightWhite
)

func (ANSIColor) Sequence
func (c ANSIColor) Sequence(background bool) string

Sequence represents the ANSI escape sequence for the color.

type Color

Color is an interface for colors.

type Color interface {
    Sequence(background bool) string
}

var NoColor Color = noColor{}

func NewColorFromHex
func NewColorFromHex(hex string) Color

NewColorFromHex creates a new Color from a hex string. If the hex string is invalid, NoColor is returned.

func NewColorFromRGB
func NewColorFromRGB(r, g, b uint8) Color

NewColorFromRGB creates a new Color from RGB values.

type Mode

Mode represents the color mode used by the terminal.

type Mode int

const (
    TrueColor Mode = iota
    ANSI256
    ANSI

    Disabled
)

func (Mode) String
func (m Mode) String() string

type Modifier

Modifier type for text modifiers.

type Modifier int

Modifiers

const (
    Reset Modifier = iota
    Bold
    Faint
    Italic
    Underline
)

func (Modifier) Sequence
func (m Modifier) Sequence() string

Sequence returns the ANSI escape sequence for the modifier.

type RGBColor

RGBColor represents a color in the RGB color space.

type RGBColor struct {
    R, G, B uint8
}

func (RGBColor) Hex
func (c RGBColor) Hex() string

Hex returns the hex representation of the color.

func (RGBColor) Sequence
func (c RGBColor) Sequence(background bool) 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.

type Style struct {
    Foreground Color
    Background Color

    Modifiers []Modifier
}

func NewStyle
func NewStyle(foregroundColor, backgroundColor Color, modifiers ...Modifier) Style

NewStyle creates a new Style with the given foreground and background colors and modifiers.

func (*Style) AddModifier
func (s *Style) AddModifier(modifier Modifier)

AddModifier adds a modifier to the style, if it's not already present.

func (Style) Fprint
func (s Style) Fprint(w io.Writer, a ...any) (n int, err error)

Fprint formats using the default formats for its operands and writes to w.

func (Style) Fprintf
func (s Style) Fprintf(w io.Writer, format string, a ...any) (n int, err error)

Fprintf formats according to a format specifier and writes to w.

func (Style) Fprintfln
func (s Style) Fprintfln(w io.Writer, format string, a ...any) (n int, err error)

Fprintfln formats according to a format specifier and writes to w.

func (Style) Fprintln
func (s Style) Fprintln(w io.Writer, a ...any) (n int, err error)

Fprintln formats using the default formats for its operands and writes to w.

func (Style) Print
func (s Style) Print(a ...any)

Print formats using the default formats for its operands and writes to standard output.

func (Style) Printf
func (s Style) Printf(format string, a ...any)

Printf formats according to a format specifier and writes to standard output.

func (Style) Printfln
func (s Style) Printfln(format string, a ...any)

Printfln formats according to a format specifier and writes to standard output.

func (Style) Println
func (s Style) Println(a ...any)

Println formats using the default formats for its operands and writes to standard output.

func (Style) Sequence
func (s Style) Sequence() string

Sequence returns the ANSI escape sequence for the style.

func (Style) Sprint
func (s Style) Sprint(a ...any) string

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

func (Style) Sprintf
func (s Style) Sprintf(format string, a ...any) string

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

func (Style) WithModifier
func (s Style) WithModifier(modifier Modifier) 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 | MarvinJWendt.com

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

Examples

Constants

This section is empty.

Variables

View Source
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
)
View Source
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 ANSIColor

type ANSIColor int

ANSIColor represents an ANSI color code.

const (
	ANSIBlack ANSIColor = iota
	ANSIRed
	ANSIGreen
	ANSIYellow
	ANSIBlue
	ANSIMagenta
	ANSICyan
	ANSIWhite
	ANSIBrightBlack
	ANSIBrightRed
	ANSIBrightGreen
	ANSIBrightYellow
	ANSIBrightBlue
	ANSIBrightMagenta
	ANSIBrightCyan
	ANSIBrightWhite
)

func (ANSIColor) Sequence

func (c ANSIColor) Sequence(background bool) string

Sequence represents the ANSI escape sequence for the color.

type Color

type Color interface {
	Sequence(background bool) string
}

Color is an interface for colors.

var NoColor Color = noColor{}

func NewColorFromHex

func NewColorFromHex(hex string) Color

NewColorFromHex creates a new Color from a hex string. If the hex string is invalid, NoColor is returned.

func NewColorFromRGB

func NewColorFromRGB(r, g, b uint8) Color

NewColorFromRGB creates a new Color from RGB values.

type Mode

type Mode int

Mode represents the color mode used by the terminal.

const (
	TrueColor Mode = iota
	ANSI256
	ANSI

	Disabled
)

func (Mode) String

func (m Mode) String() string

type Modifier

type Modifier int

Modifier type for text modifiers.

const (
	Reset Modifier = iota
	Bold
	Faint
	Italic
	Underline
)

Modifiers

func (Modifier) Sequence

func (m Modifier) Sequence() string

Sequence returns the ANSI escape sequence for the modifier.

type RGBColor

type RGBColor struct {
	R, G, B uint8
}

RGBColor represents a color in the RGB color space.

func (RGBColor) Hex

func (c RGBColor) Hex() string

Hex returns the hex representation of the color.

func (RGBColor) Sequence

func (c RGBColor) Sequence(background bool) string

Sequence returns the ANSI escape sequence for the color.

type Style

type Style struct {
	Foreground Color
	Background Color

	Modifiers []Modifier
}

Style represents a text style with a foreground and background color and modifiers.

func NewStyle

func NewStyle(foregroundColor, backgroundColor Color, modifiers ...Modifier) Style

NewStyle creates a new Style with the given foreground and background colors and modifiers.

func (*Style) AddModifier

func (s *Style) AddModifier(modifier Modifier)

AddModifier adds a modifier to the style, if it's not already present.

func (Style) Fprint

func (s Style) Fprint(w io.Writer, a ...any) (n int, err error)

Fprint formats using the default formats for its operands and writes to w.

func (Style) Fprintf

func (s Style) Fprintf(w io.Writer, format string, a ...any) (n int, err error)

Fprintf formats according to a format specifier and writes to w.

func (Style) Fprintfln

func (s Style) Fprintfln(w io.Writer, format string, a ...any) (n int, err error)

Fprintfln formats according to a format specifier and writes to w.

func (Style) Fprintln

func (s Style) Fprintln(w io.Writer, a ...any) (n int, err error)

Fprintln formats using the default formats for its operands and writes to w.

func (Style) Print

func (s Style) Print(a ...any)

Print formats using the default formats for its operands and writes to standard output.

func (Style) Printf

func (s Style) Printf(format string, a ...any)

Printf formats according to a format specifier and writes to standard output.

func (Style) Printfln

func (s Style) Printfln(format string, a ...any)

Printfln formats according to a format specifier and writes to standard output.

func (Style) Println

func (s Style) Println(a ...any)

Println formats using the default formats for its operands and writes to standard output.

func (Style) Sequence

func (s Style) Sequence() string

Sequence returns the ANSI escape sequence for the style.

func (Style) Sprint

func (s Style) Sprint(a ...any) string

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

func (Style) Sprintf

func (s Style) Sprintf(format string, a ...any) string

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

func (Style) WithModifier

func (s Style) WithModifier(modifier Modifier) Style

WithModifier returns a new Style with the given modifier added, if it's not already present.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳