color

package module
v0.1.0 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

A lightweight package 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

func BlackBg

func BlackBg(v any) string

func Blue

func Blue(v any) string

func BlueBg

func BlueBg(v any) string

func Cyan

func Cyan(v any) string

func CyanBg

func CyanBg(v any) string

func Green

func Green(v any) string

func GreenBg

func GreenBg(v any) string

func Magenta

func Magenta(v any) string

func MagentaBg

func MagentaBg(v any) string

func Red

func Red(v any) string

func RedBg

func RedBg(v any) string

func UnsafeString

func UnsafeString(b []byte) string

UnsafeString returns a unsafe string from a []byte

func White

func White(v any) string

func WhiteBg

func WhiteBg(v any) string

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

func YellowBg

func YellowBg(v any) string

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