color

package module
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 9, 2023 License: MIT Imports: 2 Imported by: 215

README

go-color

test Go Report Card Go version Go Reference Follow TwiN

An extremely lightweight cross-platform package to colorize text in terminals.

This is not meant for maximal compatibility, nor is it meant to handle a plethora of scenarios. It will simply wrap a message with the necessary characters, if the OS handles it.

Usage

go get github.com/TwiN/go-color
Using Functions

You can use the color.Colorize(color, str), the color.Ize(color, str), or the color.With(color, str) function in conjunction with a variable like so:

package main

import "github.com/TwiN/go-color"

func main() {
    // These all have the same effect:
    println(color.With(color.Red, "This is red"))
    println(color.Ize(color.Red, "This is red"))
    println(color.Colorize(color.Red, "This is red"))
    println(color.InRed("This is red"))
}

Because I felt reading color.With()/color.Ize() to be more visually pleasant than color.Colorize(), I included Ize() and With() as aliases for Colorize().

I'm not usually a big fan of having two methods doing the same thing, but since this package doesn't have much room for growth (its only purpose is to colorize terminal outputs, after all, and there aren't hundreds of ways to go about it), I believe it's acceptable to have both.

Alternatively, you can use color-specific functions:

package main

import "github.com/TwiN/go-color"

func main() {
    // Special
    println(color.InBold("This is bold"))
    println(color.InUnderline("This is underlined"))
    // Text colors
    println(color.InBlack("This is in black"))
    println(color.InRed("This is in red"))
    println(color.InGreen("This is in green"))
    println(color.InYellow("This is in yellow"))
    println(color.InBlue("This is in blue"))
    println(color.InPurple("This is in purple"))
    println(color.InCyan("This is in cyan"))
    println(color.InGray("This is in gray"))
    println(color.InWhite("This is in white"))
    // Background colors
    println(color.OverBlack("This is over a black background"))
    println(color.OverRed("This is over a red background"))
    println(color.OverGreen("This is over a green background"))
    println(color.OverYellow("This is over a yellow background"))
    println(color.OverBlue("This is over a blue background"))
    println(color.OverPurple("This is over a purple background"))
    println(color.OverCyan("This is over a cyan background"))
    println(color.OverGray("This is over a gray background"))
    println(color.OverWhite("This is over a white background"))
}

If you wish, you can also combine text and background colors by using the In<color>Over<color> functions, e.g.:

color.InWhiteOverBlack("This is white text on a black background")
color.InRedOverYellow("This is red text on a yellow background")
color.InGreenOverBlue("This is green text on a blue background")

Note that the example above is not exhaustive.

Automatic coloring

WARNING: This is an experimental feature and may be removed depending on user feedback. If you have any feedback, please comment on poll: Keep or Remove color.Autof?.

You may use color.Autof(string, args...) as a replacement to fmt.Sprintf(string, args...) but with colors for each argument:

// 20 will be in red while "cookie jar" will be in green.
println(color.Autof("I have $%.02f in my %s", 20, "cookie jar"))
Using Variables

WARNING: By using this approach, you will not be able to disable colors with color.Toggle(false). Consider using the function approach instead for maximal cross-library compatibility.

Unlike using the aforementioned approach, directly using the color variables will require you to manually prepend color.Reset at the end of your string.

You can directly use the variables like so:

package main

import "github.com/TwiN/go-color"

func main() {
    println(color.Bold + "This is bold" + color.Reset)
    println(color.Underline + "This is underlined" + color.Reset)
    println(color.Black + "This is black" + color.Reset)
    println(color.Red + "This is red" + color.Reset)
    println(color.Green + "This is green" + color.Reset)
    println(color.Yellow + "This is yellow" + color.Reset)
    println(color.Blue + "This is blue" + color.Reset)
    println(color.Purple + "This is purple" + color.Reset)
    println(color.Cyan + "This is cyan" + color.Reset)
    println(color.Gray + "This is gray" + color.Reset)
    println(color.White + "This is white" + color.Reset)
}

NOTE: If you're going to use this approach, don't forget to prepend your string with color.Reset, otherwise everything else in your terminal will be that color until the color is reset or overridden.

Disabling Colors

You can disable colors by using color.Toggle(false), which will cause all functions to not colorize the text.

As mentioned in Using Variables, disabling colors will have no effect on the variables, so make sure you are using functions if you have a need to toggle colors.

Examples
println("My name is " + color.InGreen("John Doe") + " and I have " + color.InRed(32) + " apples.")

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Reset = "\033[0m"

	Bold      = "\033[1m"
	Underline = "\033[4m"

	Black  = "\033[30m"
	Red    = "\033[31m"
	Green  = "\033[32m"
	Yellow = "\033[33m"
	Blue   = "\033[34m"
	Purple = "\033[35m"
	Cyan   = "\033[36m"
	Gray   = "\033[37m"
	White  = "\033[97m"

	BlackBackground  = "\033[40m"
	RedBackground    = "\033[41m"
	GreenBackground  = "\033[42m"
	YellowBackground = "\033[43m"
	BlueBackground   = "\033[44m"
	PurpleBackground = "\033[45m"
	CyanBackground   = "\033[46m"
	GrayBackground   = "\033[47m"
	WhiteBackground  = "\033[107m"
)

Functions

func Autof added in v1.4.0

func Autof(format string, a ...any) string

Autof automatically colors the arguments given for a specific format. Works exactly like fmt.Sprintf.

WARNING: THIS IS AN EXPERIMENTAL FEATURE AND IT MAY BE REMOVED BASED ON USER FEEDBACK. IF YOU USE THIS FEATURE, PLEASE PROVIDE FEEDBACK ON https://github.com/TwiN/go-color/discussions/13

Example:

println(Autof("Hello, %s.", "world"))

func Colorize

func Colorize(color string, s any) string

Colorize wraps a given message in a given color.

Example:

println(color.Colorize(color.Red, "This is red"))

func InBlack added in v1.2.0

func InBlack(s any) string

InBlack wraps the given string s in Black

Example:

println(color.InBlack("This is black"))

func InBlackOverBlack added in v1.3.0

func InBlackOverBlack(s any) string

func InBlackOverBlue added in v1.3.0

func InBlackOverBlue(s any) string

func InBlackOverCyan added in v1.3.0

func InBlackOverCyan(s any) string

func InBlackOverGray added in v1.3.0

func InBlackOverGray(s any) string

func InBlackOverGreen added in v1.3.0

func InBlackOverGreen(s any) string

func InBlackOverPurple added in v1.3.0

func InBlackOverPurple(s any) string

func InBlackOverRed added in v1.3.0

func InBlackOverRed(s any) string

func InBlackOverWhite added in v1.3.0

func InBlackOverWhite(s any) string

func InBlackOverYellow added in v1.3.0

func InBlackOverYellow(s any) string

func InBlue added in v1.1.0

func InBlue(s any) string

InBlue wraps the given string s in Blue

Example:

println(color.InBlue("This is blue"))

func InBlueOverBlack added in v1.3.0

func InBlueOverBlack(s any) string

func InBlueOverBlue added in v1.3.0

func InBlueOverBlue(s any) string

func InBlueOverCyan added in v1.3.0

func InBlueOverCyan(s any) string

func InBlueOverGray added in v1.3.0

func InBlueOverGray(s any) string

func InBlueOverGreen added in v1.3.0

func InBlueOverGreen(s any) string

func InBlueOverPurple added in v1.3.0

func InBlueOverPurple(s any) string

func InBlueOverRed added in v1.3.0

func InBlueOverRed(s any) string

func InBlueOverWhite added in v1.3.0

func InBlueOverWhite(s any) string

func InBlueOverYellow added in v1.3.0

func InBlueOverYellow(s any) string

func InBold added in v1.1.0

func InBold(s any) string

InBold wraps the given string s in Bold

Example:

println(color.InBold("This is bold"))

func InCyan added in v1.1.0

func InCyan(s any) string

InCyan wraps the given string s in Cyan

Example:

println(color.InCyan("This is cyan"))

func InCyanOverBlack added in v1.3.0

func InCyanOverBlack(s any) string

func InCyanOverBlue added in v1.3.0

func InCyanOverBlue(s any) string

func InCyanOverCyan added in v1.3.0

func InCyanOverCyan(s any) string

func InCyanOverGray added in v1.3.0

func InCyanOverGray(s any) string

func InCyanOverGreen added in v1.3.0

func InCyanOverGreen(s any) string

func InCyanOverPurple added in v1.3.0

func InCyanOverPurple(s any) string

func InCyanOverRed added in v1.3.0

func InCyanOverRed(s any) string

func InCyanOverWhite added in v1.3.0

func InCyanOverWhite(s any) string

func InCyanOverYellow added in v1.3.0

func InCyanOverYellow(s any) string

func InGray added in v1.1.0

func InGray(s any) string

InGray wraps the given string s in Gray

Example:

println(color.InGray("This is gray"))

func InGrayOverBlack added in v1.3.0

func InGrayOverBlack(s any) string

func InGrayOverBlue added in v1.3.0

func InGrayOverBlue(s any) string

func InGrayOverCyan added in v1.3.0

func InGrayOverCyan(s any) string

func InGrayOverGray added in v1.3.0

func InGrayOverGray(s any) string

func InGrayOverGreen added in v1.3.0

func InGrayOverGreen(s any) string

func InGrayOverPurple added in v1.3.0

func InGrayOverPurple(s any) string

func InGrayOverRed added in v1.3.0

func InGrayOverRed(s any) string

func InGrayOverWhite added in v1.3.0

func InGrayOverWhite(s any) string

func InGrayOverYellow added in v1.3.0

func InGrayOverYellow(s any) string

func InGreen added in v1.1.0

func InGreen(s any) string

InGreen wraps the given string s in Green

Example:

println(color.InGreen("This is green"))

func InGreenOverBlack added in v1.3.0

func InGreenOverBlack(s any) string

func InGreenOverBlue added in v1.3.0

func InGreenOverBlue(s any) string

func InGreenOverCyan added in v1.3.0

func InGreenOverCyan(s any) string

func InGreenOverGray added in v1.3.0

func InGreenOverGray(s any) string

func InGreenOverGreen added in v1.3.0

func InGreenOverGreen(s any) string

func InGreenOverPurple added in v1.3.0

func InGreenOverPurple(s any) string

func InGreenOverRed added in v1.3.0

func InGreenOverRed(s any) string

func InGreenOverWhite added in v1.3.0

func InGreenOverWhite(s any) string

func InGreenOverYellow added in v1.3.0

func InGreenOverYellow(s any) string

func InPurple added in v1.1.0

func InPurple(s any) string

InPurple wraps the given string s in Purple

Example:

println(color.InPurple("This is purple"))

func InPurpleOverBlack added in v1.3.0

func InPurpleOverBlack(s any) string

func InPurpleOverBlue added in v1.3.0

func InPurpleOverBlue(s any) string

func InPurpleOverCyan added in v1.3.0

func InPurpleOverCyan(s any) string

func InPurpleOverGray added in v1.3.0

func InPurpleOverGray(s any) string

func InPurpleOverGreen added in v1.3.0

func InPurpleOverGreen(s any) string

func InPurpleOverPurple added in v1.3.0

func InPurpleOverPurple(s any) string

func InPurpleOverRed added in v1.3.0

func InPurpleOverRed(s any) string

func InPurpleOverWhite added in v1.3.0

func InPurpleOverWhite(s any) string

func InPurpleOverYellow added in v1.3.0

func InPurpleOverYellow(s any) string

func InRed added in v1.1.0

func InRed(s any) string

InRed wraps the given string s in Red

Example:

println(color.InRed("This is red"))

func InRedOverBlack added in v1.3.0

func InRedOverBlack(s any) string

func InRedOverBlue added in v1.3.0

func InRedOverBlue(s any) string

func InRedOverCyan added in v1.3.0

func InRedOverCyan(s any) string

func InRedOverGray added in v1.3.0

func InRedOverGray(s any) string

func InRedOverGreen added in v1.3.0

func InRedOverGreen(s any) string

func InRedOverPurple added in v1.3.0

func InRedOverPurple(s any) string

func InRedOverRed added in v1.3.0

func InRedOverRed(s any) string

func InRedOverWhite added in v1.3.0

func InRedOverWhite(s any) string

func InRedOverYellow added in v1.3.0

func InRedOverYellow(s any) string

func InUnderline added in v1.2.0

func InUnderline(s any) string

InUnderline wraps the given string s in Underline

Example:

println(color.InUnderline("This is underlined"))

func InWhite added in v1.1.0

func InWhite(s any) string

InWhite wraps the given string s in White

Example:

println(color.InWhite("This is white"))

func InWhiteOverBlack added in v1.3.0

func InWhiteOverBlack(s any) string

func InWhiteOverBlue added in v1.3.0

func InWhiteOverBlue(s any) string

func InWhiteOverCyan added in v1.3.0

func InWhiteOverCyan(s any) string

func InWhiteOverGray added in v1.3.0

func InWhiteOverGray(s any) string

func InWhiteOverGreen added in v1.3.0

func InWhiteOverGreen(s any) string

func InWhiteOverPurple added in v1.3.0

func InWhiteOverPurple(s any) string

func InWhiteOverRed added in v1.3.0

func InWhiteOverRed(s any) string

func InWhiteOverWhite added in v1.3.0

func InWhiteOverWhite(s any) string

func InWhiteOverYellow added in v1.3.0

func InWhiteOverYellow(s any) string

func InYellow added in v1.1.0

func InYellow(s any) string

InYellow wraps the given string s in Yellow

Example:

println(color.InYellow("This is yellow"))

func InYellowOverBlack added in v1.3.0

func InYellowOverBlack(s any) string

func InYellowOverBlue added in v1.3.0

func InYellowOverBlue(s any) string

func InYellowOverCyan added in v1.3.0

func InYellowOverCyan(s any) string

func InYellowOverGray added in v1.3.0

func InYellowOverGray(s any) string

func InYellowOverGreen added in v1.3.0

func InYellowOverGreen(s any) string

func InYellowOverPurple added in v1.3.0

func InYellowOverPurple(s any) string

func InYellowOverRed added in v1.3.0

func InYellowOverRed(s any) string

func InYellowOverWhite added in v1.3.0

func InYellowOverWhite(s any) string

func InYellowOverYellow added in v1.3.0

func InYellowOverYellow(s any) string

func Ize

func Ize(color string, s any) string

Ize is an alias for the Colorize function

Example:

println(color.Ize(color.Red, "This is red"))

func OverBlack added in v1.3.0

func OverBlack(s any) string

OverBlack wraps the given string s in BlackBackground

Example:

println(color.OverBlack("This is over black"))

func OverBlue added in v1.3.0

func OverBlue(s any) string

OverBlue wraps the given string s in BlueBackground

Example:

println(color.OverBlue("This is over blue"))

func OverCyan added in v1.3.0

func OverCyan(s any) string

OverCyan wraps the given string s in CyanBackground

Example:

println(color.OverCyan("This is over cyan"))

func OverGray added in v1.3.0

func OverGray(s any) string

OverGray wraps the given string s in GrayBackground

Example:

println(color.OverGray("This is over gray"))

func OverGreen added in v1.3.0

func OverGreen(s any) string

OverGreen wraps the given string s in GreenBackground

Example:

println(color.OverGreen("This is over green"))

func OverPurple added in v1.3.0

func OverPurple(s any) string

OverPurple wraps the given string s in PurpleBackground

Example:

println(color.OverPurple("This is over purple"))

func OverRed added in v1.3.0

func OverRed(s any) string

OverRed wraps the given string s in RedBackground

Example:

println(color.OverRed("This is over red"))

func OverWhite added in v1.3.0

func OverWhite(s any) string

OverWhite wraps the given string s in WhiteBackground

Example:

println(color.OverWhite("This is over white"))

func OverYellow added in v1.3.0

func OverYellow(s any) string

OverYellow wraps the given string s in YellowBackground

Example:

println(color.OverYellow("This is over yellow"))

func Toggle added in v1.4.0

func Toggle(enable bool)

Toggle enables or disables color output

Note that this does not apply if you are coloring by concatenating strings with color variables directly (e.g. color.Red+"Hello"+color.Reset). Enabling/disabling coloring only applies to all functions (Colorize, Ize, With, InRed, OverRed, etc.)

func With added in v1.3.0

func With(color string, s any) string

With is an alias for the Colorize function

Example:

println(color.With(color.Red, "This is red"))

Types

This section is empty.

Jump to

Keyboard shortcuts

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