color

package module
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 26, 2025 License: MIT Imports: 5 Imported by: 0

README

Color

test badge Go Report Card PkgGoDev

A lightweight library for colorizing terminal output.

It is intended to be a more performant alternative to similar libraries, achieved through the optimization of type conversions and string concatenations.

Installation

To install color, you can use go get:

go get github.com/gabefiori/color

Import the gabefiori/color package:

package your

import (
    "fmt"
    "github.com/gabefiori/color"
)

func main() {
    b := color.Blue("blue text!")
    fmt.Println(b)
}

Examples

b1 := color.Blue("returns string with blue text")
b2 := color.BlueBg("returns a string with blue background")

fmt.Println(b1, b2)

// 24-bit colors can be generated.
yellow := color.RGB(255, 255, 50)
gray := color.BgRGB(128, 128, 128)

_ = color.Wrap(yellow, "returns string with yellow text")
_ = color.Wrap(gray, "returns a string with gray background")
Turning Off

Colorization can be disabled globally. By default, it is turned off if the NO_COLOR environment variable is set or when in a non-interactive environment.

color.NoColor = true
fmt.Println(color.Blue("no color"))
Types

Any type is accepted and automatically converted to a string.

_ = color.Red([]byte("hey"))
_ = color.Green(true)

// Integers are converted to their string representation (100 -> "100", not "d")
_ = color.Red(100)
_ = color.Red(-5)
_ = color.Red(uint8(250))
Custom Styles

Custom styles can be created. For more information, check out this article.

redOnWhite := "\x1b[31;47m"
_ = color.Wrap(redOnWhite, "hello")

Documentation

Overview

Package color provides utilities for colorizing terminal output using ANSI escape codes.

Index

Constants

View Source
const (
	// Escape is the escape character used to introduce ANSI escape sequences.
	Escape = "\x1b"

	// Prefix is the string that starts an ANSI escape sequence, indicating the beginning of formatting commands.
	Prefix = Escape + "["

	// PrefixRGB is the prefix used for setting foreground colors using RGB values in ANSI escape sequences.
	PrefixRGB = Prefix + "38;2;"

	// PrefixBgRGB is the prefix used for setting background colors using RGB values in ANSI escape sequences.
	PrefixBgRGB = Prefix + "48;2;"

	// Reset is the ANSI escape sequence that resets all attributes (like color and formatting) in the terminal.
	Reset = Escape + "[0m"
)

Variables

View Source
var NoColor = isNoColor()

NoColor indicates whether colorization is disabled based on the NO_COLOR environment variable.

Functions

func Black

func Black(v any) string

Black returns a colorized string in black. (30)

func BlackBg

func BlackBg(v any) string

BlackBg returns a colorized string with a black background. (40)

func Blue

func Blue(v any) string

Blue returns a colorized string in blue. (34)

func BlueBg

func BlueBg(v any) string

BlueBg returns a colorized string with a blue background. (44)

func BrightBlack added in v0.2.0

func BrightBlack(v any) string

BrightBlack returns a colorized string in bright black. (90)

func BrightBlackBg added in v0.2.0

func BrightBlackBg(v any) string

BrightBlackBg returns a colorized string with a bright black background. (100)

func BrightBlue added in v0.2.0

func BrightBlue(v any) string

BrightBlue returns a colorized string in bright blue. (94)

func BrightBlueBg added in v0.2.0

func BrightBlueBg(v any) string

BrightBlueBg returns a colorized string with a bright blue background. (104)

func BrightCyan added in v0.2.0

func BrightCyan(v any) string

BrightCyan returns a colorized string in bright cyan. (96)

func BrightCyanBg added in v0.2.0

func BrightCyanBg(v any) string

BrightCyanBg returns a colorized string with a bright cyan background. (106)

func BrightGreen added in v0.2.0

func BrightGreen(v any) string

BrightGreen returns a colorized string in bright green. (92)

func BrightGreenBg added in v0.2.0

func BrightGreenBg(v any) string

BrightGreenBg returns a colorized string with a bright green background. (102)

func BrightMagenta added in v0.2.0

func BrightMagenta(v any) string

BrightMagenta returns a colorized string in bright magenta. (95)

func BrightMagentaBg added in v0.2.0

func BrightMagentaBg(v any) string

BrightMagentaBg returns a colorized string with a bright magenta background. (105)

func BrightRed added in v0.2.0

func BrightRed(v any) string

BrightRed returns a colorized string in bright red. (91)

func BrightRedBg added in v0.2.0

func BrightRedBg(v any) string

BrightRedBg returns a colorized string with a bright red background. (101)

func BrightWhite added in v0.2.0

func BrightWhite(v any) string

BrightWhite returns a colorized string in bright white. (97)

func BrightWhiteBg added in v0.2.0

func BrightWhiteBg(v any) string

BrightWhiteBg returns a colorized string with a bright white background. (107)

func BrightYellow added in v0.2.0

func BrightYellow(v any) string

BrightYellow returns a colorized string in bright yellow. (93)

func BrightYellowBg added in v0.2.0

func BrightYellowBg(v any) string

BrightYellowBg returns a colorized string with a bright yellow background. (103)

func Cyan

func Cyan(v any) string

Cyan returns a colorized string in cyan. (36)

func CyanBg

func CyanBg(v any) string

CyanBg returns a colorized string with a cyan background. (46)

func Green

func Green(v any) string

Green returns a colorized string in green. (32)

func GreenBg

func GreenBg(v any) string

GreenBg returns a colorized string with a green background. (42)

func Magenta

func Magenta(v any) string

Magenta returns a colorized string in magenta. (35)

func MagentaBg

func MagentaBg(v any) string

MagentaBg returns a colorized string with a magenta background. (45)

func Red

func Red(v any) string

Red returns a colorized string in red. (31)

func RedBg

func RedBg(v any) string

RedBg returns a colorized string with a red background. (41)

func White

func White(v any) string

White returns a colorized string in white. (37)

func WhiteBg

func WhiteBg(v any) string

WhiteBg returns a colorized string with a white background. (47)

func Wrap

func Wrap(c Color, v any) string

Wrap returns a colorized string representation of any value type.

func Yellow

func Yellow(v any) string

Yellow returns a colorized string in yellow. (33)

func YellowBg

func YellowBg(v any) string

YellowBg returns a colorized string with a yellow background. (43)

Types

type Color

type Color = string

Color represents an ANSI color code based on this [lookup table](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors).

const (
	FgBlack   Color = Prefix + "30m"
	FgRed     Color = Prefix + "31m"
	FgGreen   Color = Prefix + "32m"
	FgYellow  Color = Prefix + "33m"
	FgBlue    Color = Prefix + "34m"
	FgMagenta Color = Prefix + "35m"
	FgCyan    Color = Prefix + "36m"
	FgWhite   Color = Prefix + "37m"

	BgBlack   Color = Prefix + "40m"
	BgRed     Color = Prefix + "41m"
	BgGreen   Color = Prefix + "42m"
	BgYellow  Color = Prefix + "43m"
	BgBlue    Color = Prefix + "44m"
	BgMagenta Color = Prefix + "45m"
	BgCyan    Color = Prefix + "46m"
	BgWhite   Color = Prefix + "47m"

	FgBrightBlack   Color = Prefix + "90m"
	FgBrightRed     Color = Prefix + "91m"
	FgBrightGreen   Color = Prefix + "92m"
	FgBrightYellow  Color = Prefix + "93m"
	FgBrightBlue    Color = Prefix + "94m"
	FgBrightMagenta Color = Prefix + "95m"
	FgBrightCyan    Color = Prefix + "96m"
	FgBrightWhite   Color = Prefix + "97m"

	BgBrightBlack   Color = Prefix + "100m"
	BgBrightRed     Color = Prefix + "101m"
	BgBrightGreen   Color = Prefix + "102m"
	BgBrightYellow  Color = Prefix + "103m"
	BgBrightBlue    Color = Prefix + "104m"
	BgBrightMagenta Color = Prefix + "105m"
	BgBrightCyan    Color = Prefix + "106m"
	BgBrightWhite   Color = Prefix + "107m"
)

func BgRGB added in v0.2.0

func BgRGB(r, g, b int) Color

BgRGB creates a background Color using the specified RGB values.

func RGB added in v0.2.0

func RGB(r, g, b int) Color

RGB creates a foreground Color using the specified RGB values.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL