color

package module
v0.0.0-...-4b60f46 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2023 License: MIT Imports: 2 Imported by: 0

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 All

func All(color string, strs ...any) string

All wraps a given messages in a given color.

Example:

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

func Autof

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

func InBlack(s any) string

InBlack wraps the given string s in Black

Example:

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

func InBlackOverBlack

func InBlackOverBlack(s any) string

func InBlackOverBlue

func InBlackOverBlue(s any) string

func InBlackOverCyan

func InBlackOverCyan(s any) string

func InBlackOverGray

func InBlackOverGray(s any) string

func InBlackOverGreen

func InBlackOverGreen(s any) string

func InBlackOverPurple

func InBlackOverPurple(s any) string

func InBlackOverRed

func InBlackOverRed(s any) string

func InBlackOverWhite

func InBlackOverWhite(s any) string

func InBlackOverYellow

func InBlackOverYellow(s any) string

func InBlue

func InBlue(s any) string

InBlue wraps the given string s in Blue

Example:

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

func InBlueOverBlack

func InBlueOverBlack(s any) string

func InBlueOverBlue

func InBlueOverBlue(s any) string

func InBlueOverCyan

func InBlueOverCyan(s any) string

func InBlueOverGray

func InBlueOverGray(s any) string

func InBlueOverGreen

func InBlueOverGreen(s any) string

func InBlueOverPurple

func InBlueOverPurple(s any) string

func InBlueOverRed

func InBlueOverRed(s any) string

func InBlueOverWhite

func InBlueOverWhite(s any) string

func InBlueOverYellow

func InBlueOverYellow(s any) string

func InBold

func InBold(s any) string

InBold wraps the given string s in Bold

Example:

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

func InCyan

func InCyan(s any) string

InCyan wraps the given string s in Cyan

Example:

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

func InCyanOverBlack

func InCyanOverBlack(s any) string

func InCyanOverBlue

func InCyanOverBlue(s any) string

func InCyanOverCyan

func InCyanOverCyan(s any) string

func InCyanOverGray

func InCyanOverGray(s any) string

func InCyanOverGreen

func InCyanOverGreen(s any) string

func InCyanOverPurple

func InCyanOverPurple(s any) string

func InCyanOverRed

func InCyanOverRed(s any) string

func InCyanOverWhite

func InCyanOverWhite(s any) string

func InCyanOverYellow

func InCyanOverYellow(s any) string

func InGray

func InGray(s any) string

InGray wraps the given string s in Gray

Example:

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

func InGrayOverBlack

func InGrayOverBlack(s any) string

func InGrayOverBlue

func InGrayOverBlue(s any) string

func InGrayOverCyan

func InGrayOverCyan(s any) string

func InGrayOverGray

func InGrayOverGray(s any) string

func InGrayOverGreen

func InGrayOverGreen(s any) string

func InGrayOverPurple

func InGrayOverPurple(s any) string

func InGrayOverRed

func InGrayOverRed(s any) string

func InGrayOverWhite

func InGrayOverWhite(s any) string

func InGrayOverYellow

func InGrayOverYellow(s any) string

func InGreen

func InGreen(s any) string

InGreen wraps the given string s in Green

Example:

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

func InGreenOverBlack

func InGreenOverBlack(s any) string

func InGreenOverBlue

func InGreenOverBlue(s any) string

func InGreenOverCyan

func InGreenOverCyan(s any) string

func InGreenOverGray

func InGreenOverGray(s any) string

func InGreenOverGreen

func InGreenOverGreen(s any) string

func InGreenOverPurple

func InGreenOverPurple(s any) string

func InGreenOverRed

func InGreenOverRed(s any) string

func InGreenOverWhite

func InGreenOverWhite(s any) string

func InGreenOverYellow

func InGreenOverYellow(s any) string

func InPurple

func InPurple(s any) string

InPurple wraps the given string s in Purple

Example:

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

func InPurpleOverBlack

func InPurpleOverBlack(s any) string

func InPurpleOverBlue

func InPurpleOverBlue(s any) string

func InPurpleOverCyan

func InPurpleOverCyan(s any) string

func InPurpleOverGray

func InPurpleOverGray(s any) string

func InPurpleOverGreen

func InPurpleOverGreen(s any) string

func InPurpleOverPurple

func InPurpleOverPurple(s any) string

func InPurpleOverRed

func InPurpleOverRed(s any) string

func InPurpleOverWhite

func InPurpleOverWhite(s any) string

func InPurpleOverYellow

func InPurpleOverYellow(s any) string

func InRed

func InRed(s any) string

InRed wraps the given string s in Red

Example:

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

func InRedOverBlack

func InRedOverBlack(s any) string

func InRedOverBlue

func InRedOverBlue(s any) string

func InRedOverCyan

func InRedOverCyan(s any) string

func InRedOverGray

func InRedOverGray(s any) string

func InRedOverGreen

func InRedOverGreen(s any) string

func InRedOverPurple

func InRedOverPurple(s any) string

func InRedOverRed

func InRedOverRed(s any) string

func InRedOverWhite

func InRedOverWhite(s any) string

func InRedOverYellow

func InRedOverYellow(s any) string

func InUnderline

func InUnderline(s any) string

InUnderline wraps the given string s in Underline

Example:

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

func InWhite

func InWhite(s any) string

InWhite wraps the given string s in White

Example:

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

func InWhiteOverBlack

func InWhiteOverBlack(s any) string

func InWhiteOverBlue

func InWhiteOverBlue(s any) string

func InWhiteOverCyan

func InWhiteOverCyan(s any) string

func InWhiteOverGray

func InWhiteOverGray(s any) string

func InWhiteOverGreen

func InWhiteOverGreen(s any) string

func InWhiteOverPurple

func InWhiteOverPurple(s any) string

func InWhiteOverRed

func InWhiteOverRed(s any) string

func InWhiteOverWhite

func InWhiteOverWhite(s any) string

func InWhiteOverYellow

func InWhiteOverYellow(s any) string

func InYellow

func InYellow(s any) string

InYellow wraps the given string s in Yellow

Example:

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

func InYellowOverBlack

func InYellowOverBlack(s any) string

func InYellowOverBlue

func InYellowOverBlue(s any) string

func InYellowOverCyan

func InYellowOverCyan(s any) string

func InYellowOverGray

func InYellowOverGray(s any) string

func InYellowOverGreen

func InYellowOverGreen(s any) string

func InYellowOverPurple

func InYellowOverPurple(s any) string

func InYellowOverRed

func InYellowOverRed(s any) string

func InYellowOverWhite

func InYellowOverWhite(s any) string

func InYellowOverYellow

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

func OverBlack(s any) string

OverBlack wraps the given string s in BlackBackground

Example:

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

func OverBlue

func OverBlue(s any) string

OverBlue wraps the given string s in BlueBackground

Example:

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

func OverCyan

func OverCyan(s any) string

OverCyan wraps the given string s in CyanBackground

Example:

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

func OverGray

func OverGray(s any) string

OverGray wraps the given string s in GrayBackground

Example:

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

func OverGreen

func OverGreen(s any) string

OverGreen wraps the given string s in GreenBackground

Example:

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

func OverPurple

func OverPurple(s any) string

OverPurple wraps the given string s in PurpleBackground

Example:

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

func OverRed

func OverRed(s any) string

OverRed wraps the given string s in RedBackground

Example:

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

func OverWhite

func OverWhite(s any) string

OverWhite wraps the given string s in WhiteBackground

Example:

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

func OverYellow

func OverYellow(s any) string

OverYellow wraps the given string s in YellowBackground

Example:

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

func Toggle

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

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