color

package
v1.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 22, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package color provides ANSI color formatting for terminal output. It supports short color codes like [?GN] (green), [?RD] (red), [?BOLD], etc. Coloring can be globally disabled by setting color.ColorEnabled = false.

Index

Constants

This section is empty.

Variables

View Source
var ColorEnabled = true

ColorEnabled globally enables or disables ANSI color output. When false, all color codes are stripped from the output.

View Source
var Colors = map[string]string{
	"BOLD": bold, "BD": bold,
	"UNDERLINE": underline, "UE": underline,
	"RESET": reset, "RT": reset,

	"BLACK": black, "BK": black,
	"RED": red, "RD": red,
	"GREEN": green, "GN": green,
	"YELLOW": yellow, "YW": yellow,
	"BLUE": blue, "BE": blue,
	"MAGENTA": magenta, "MA": magenta,
	"CYAN": cyan, "CN": cyan,

	"BBLACK": brightBlack, "BBK": brightBlack,
	"BRED": brightRed, "BRD": brightRed,
	"BGREEN": brightGreen, "BGN": brightGreen,
	"BYELLOW": brightYellow, "BYW": brightYellow,
	"BBLUE": brightBlue, "BBE": brightBlue,
	"BMAGENTA": brightMagenta, "BMA": brightMagenta,
	"BCYAN": brightCyan, "BCN": brightCyan,

	"BACKBLACK": blackBg, "BKBK": blackBg,
	"BACKRED": redBg, "BKRD": redBg,
	"BACKGREEN": greenBg, "BKGN": greenBg,
	"BACKYELLOW": yellowBg, "BKYW": yellowBg,
	"BACKBLUE": blueBg, "BKBE": blueBg,
	"BACKMAGENTA": magentaBg, "BKMA": magentaBg,
	"BACKCYAN": cyanBg, "BKCN": cyanBg,

	"BACKBBLACK": brightBlackBg, "BKBBK": brightBlackBg,
	"BACKBRED": brightRedBg, "BKBRD": brightRedBg,
	"BACKBGREEN": brightGreenBg, "BKBGN": brightGreenBg,
	"BACKBYELLOW": brightYellowBg, "BKBYW": brightYellowBg,
	"BACKBBLUE": brightBlueBg, "BKBBE": brightBlueBg,
	"BACKBMAGENTA": brightMagentaBg, "BKBMA": brightMagentaBg,
	"BACKBCYAN": brightCyanBg, "BKBCN": brightCyanBg,
}

Colors maps short color codes (uppercase) to ANSI escape sequences. Supported codes:

  • Text foreground: BOLD/BD, UNDERLINE/UE, RESET/RT, basic colours (BLACK/BK, RED/RD, GREEN/GN, YELLOW/YW, BLUE/BE, MAGENTA/MA, CYAN/CN), bright variants (BBLACK/BBK, BRED/BRD, BGREEN/BGN, BYELLOW/BYW, BBLUE/BBE, BMAGENTA/BMA, BCYAN/BCN)
  • Background colours (full name: BACK<COLOUR>, short: BK<SHORT>): BACKBLACK/BKBK, BACKRED/BKRD, BACKGREEN/BKGN, BACKYELLOW/BKYW, BACKBLUE/BKBE, BACKMAGENTA/BKMA, BACKCYAN/BKCN, and bright variants: BACKBBLACK/BKBBK, BACKBRED/BKBRD, BACKBGREEN/BKBGN, BACKBYELLOW/BKBYW, BACKBBLUE/BKBBE, BACKBMAGENTA/BKBMA, BACKBCYAN/BKBCN.

Functions

func ConvertColored

func ConvertColored(text ...string) []string

ConvertColored applies color formatting to each string in the slice using Set and returns a new slice with the colored strings.

func FormColorPlaceholder added in v1.1.5

func FormColorPlaceholder(code string) string

Place color code (like 'RD', 'RED', etc.) to form for replace to color Like 'rd' -> '[?RD]'

func PrintColored

func PrintColored(format string, args ...interface{})

PrintColored formats the string using fmt.Sprintf, replaces color codes, and writes the result to stdout without a trailing newline.

func PrintlnColored

func PrintlnColored(format string, args ...interface{})

PrintlnColored formats the string using fmt.Sprintf, replaces color codes, and writes the result to stdout followed by a newline.

func ReplaceColors added in v1.1.5

func ReplaceColors(text string) string

Replace color codes

func Set

func Set(text string) string

Set replaces short color codes like [?RED] or [?GN] in the input string with the corresponding ANSI escape sequences. If ColorEnabled is false, the codes are removed entirely. The ANSI reset code is automatically appended at the end when colors are enabled.

Write 'BACK' (or '<') code to append last colors.

Example:

"[?BE][?UE]test [?BD]bold [?RT][?<]string" equal to "[?BE]test [?BD]bold [?RT][?BE][?UE]string"

Types

This section is empty.

Jump to

Keyboard shortcuts

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