color

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Nov 6, 2021 License: Apache-2.0 Imports: 1 Imported by: 1

README

go-color

Forked from TwiN/go-color. The main change is an explicit Init() function that "activates" Add(). This allows calling code to determine whether or not colors are used. To keep the API surface small, I've also removed the Colorize() function in favor of Add() Many thanks to @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.

There are many cases in which this would not work, such as the output being redirected to something other than a terminal (such as a file, i.e. executable >> file.txt)

Usage

Function

You can use the color.Add(color, message) function in conjunction with a variable like so:

package main

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

func main() {
    Init()
    println(color.Add(color.Red, "This is red"))
}
Variables only

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

You can either directly use the variables like so:

package main

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

func main() {
    println(color.Red + "This is red" + color.Reset)
    println(color.Green + "This is green" + color.Reset)
    println(color.Blue + "This is blue" + color.Reset)
    println(color.Purple + "This is purple" + color.Reset)
    println(color.Yellow + "This is yellow" + 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 append your string with color.Reset, otherwise everything else in your terminal will be that color until the color is reset or overridden.

Installation

go get github.com/bbkane/go-color

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Default - Returns all attributes to the default state prior to modification
	Default = ""

	// Bold - Applies brightness/intensity flag to foreground color
	Bold = ""

	// Nobold - Removes brightness/intensity flag from foreground color
	Nobold = ""

	// Underline - Adds underline
	Underline = ""

	// Nounderline - Removes underline
	Nounderline = ""

	// Negative - Swaps foreground and background colors
	Negative = ""

	// Positive - Returns foreground/background to normal
	Positive = ""

	// ForegroundBlack - Applies non-bold/bright black to foreground
	ForegroundBlack = ""

	// ForegroundRed - Applies non-bold/bright red to foreground
	ForegroundRed = ""

	// ForegroundGreen - Applies non-bold/bright green to foreground
	ForegroundGreen = ""

	// ForegroundYellow - Applies non-bold/bright yellow to foreground
	ForegroundYellow = ""

	// ForegroundBlue - Applies non-bold/bright blue to foreground
	ForegroundBlue = ""

	// ForegroundMagenta - Applies non-bold/bright magenta to foreground
	ForegroundMagenta = ""

	// ForegroundCyan - Applies non-bold/bright cyan to foreground
	ForegroundCyan = ""

	// ForegroundWhite - Applies non-bold/bright white to foreground
	ForegroundWhite = ""

	// ForegroundExtended - Applies extended color value to the foreground (see details below)
	ForegroundExtended = ""

	// ForegroundDefault - Applies only the foreground portion of the defaults (see 0)
	ForegroundDefault = ""

	// BackgroundBlack - Applies non-bold/bright black to background
	BackgroundBlack = ""

	// BackgroundRed - Applies non-bold/bright red to background
	BackgroundRed = ""

	// BackgroundGreen - Applies non-bold/bright green to background
	BackgroundGreen = ""

	// BackgroundYellow - Applies non-bold/bright yellow to background
	BackgroundYellow = ""

	// BackgroundBlue - Applies non-bold/bright blue to background
	BackgroundBlue = ""

	// BackgroundMagenta - Applies non-bold/bright magenta to background
	BackgroundMagenta = ""

	// BackgroundCyan - Applies non-bold/bright cyan to background
	BackgroundCyan = ""

	// BackgroundWhite - Applies non-bold/bright white to background
	BackgroundWhite = ""

	// BackgroundExtended - Applies extended color value to the background (see details below)
	BackgroundExtended = ""

	// BackgroundDefault - Applies only the background portion of the defaults (see 0)
	BackgroundDefault = ""

	// BrightForegroundBlack - Applies bold/bright black to foreground
	BrightForegroundBlack = ""

	// BrightForegroundRed - Applies bold/bright red to foreground
	BrightForegroundRed = ""

	// BrightForegroundGreen - Applies bold/bright green to foreground
	BrightForegroundGreen = ""

	// BrightForegroundYellow - Applies bold/bright yellow to foreground
	BrightForegroundYellow = ""

	// BrightForegroundBlue - Applies bold/bright blue to foreground
	BrightForegroundBlue = ""

	// BrightForegroundMagenta - Applies bold/bright magenta to foreground
	BrightForegroundMagenta = ""

	// BrightForegroundCyan - Applies bold/bright cyan to foreground
	BrightForegroundCyan = ""

	// BrightForegroundWhite - Applies bold/bright white to foreground
	BrightForegroundWhite = ""

	// BrightBackgroundBlack - Applies bold/bright black to background
	BrightBackgroundBlack = ""

	// BrightBackgroundRed - Applies bold/bright red to background
	BrightBackgroundRed = ""

	// BrightBackgroundGreen - Applies bold/bright green to background
	BrightBackgroundGreen = ""

	// BrightBackgroundYellow - Applies bold/bright yellow to background
	BrightBackgroundYellow = ""

	// BrightBackgroundBlue - Applies bold/bright blue to background
	BrightBackgroundBlue = ""

	// BrightBackgroundMagenta - Applies bold/bright magenta to background
	BrightBackgroundMagenta = ""

	// BrightBackgroundCyan - Applies bold/bright cyan to background
	BrightBackgroundCyan = ""

	// BrightBackgroundWhite - Applies bold/bright white to background
	BrightBackgroundWhite = ""
)

These will be set to the proper color codes if Enable() returns successfully. generated from gen_colors/gen_color.py

Functions

func Add added in v0.0.2

func Add(color string, message string) string

Add wraps a given message in a given color. Init must be called first

func Enable added in v0.0.3

func Enable() error

Enable enables color printing. It doesn't currently seem to work on Windows and I don't really have access to a Windows machine to fix it.

func Enabled added in v0.0.3

func Enabled() bool

If Enabled, then you are good to go!

Types

This section is empty.

Jump to

Keyboard shortcuts

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