color

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jan 25, 2025 License: MIT Imports: 4 Imported by: 0

README

Color

test badge Go Report Card PkgGoDev

A lightweight library that provides utilities for colorizing terminal output using ANSI escape codes.

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

// Background colors are also available.
b2 := color.BlueBg("returns a string with blue background")

fmt.Println(b1, b2)

// Colorization can be disabled.
// By default, it will be disabled if the "NO_COLOR" environment variable is set.
color.NoColor = true
fmt.Println(color.Blue("no color..."))

color.NoColor = false

// Any type is accepted, but some types, like string, []byte, or any kind of number (e.g., int, int8, uint32...)
// are optimized.
_ = color.Red(100)
_ = color.Red(uint(100))
_ = color.Red([]byte("hey"))

// Only 16 styles are available, but you can create your own.
brightRed := "\x1b[91m"
redOnWhite := "\x1b[31;47m"
rgb := "\x1b[38;2;50;200;100m"

_ = color.Wrap(brightRed, "hello")
_ = color.Wrap(redOnWhite, "hello")
_ = color.Wrap(rgb, "hello")

Tests

To run the tests, you can run:

go test ./...

Benchmarks

To run the benchmarks, you can run:

go test ./... -bench=. -benchmem

Documentation

Overview

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

Index

Constants

View Source
const (
	Reset     = "\x1b[0m"
	FgBlack   = "\x1b[30m"
	FgRed     = "\x1b[31m"
	FgGreen   = "\x1b[32m"
	FgYellow  = "\x1b[33m"
	FgBlue    = "\x1b[34m"
	FgMagenta = "\x1b[35m"
	FgCyan    = "\x1b[36m"
	FgWhite   = "\x1b[37m"

	BgBlack   = "\x1b[40m"
	BgRed     = "\x1b[41m"
	BgGreen   = "\x1b[42m"
	BgYellow  = "\x1b[43m"
	BgBlue    = "\x1b[44m"
	BgMagenta = "\x1b[45m"
	BgCyan    = "\x1b[46m"
	BgWhite   = "\x1b[47m"
)

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 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 UnsafeString

func UnsafeString(b []byte) string

UnsafeString returns a unsafe string from a []byte

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

Jump to

Keyboard shortcuts

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