color

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2019 License: MIT Imports: 6 Imported by: 0

README

Color GoDoc

Color is based on the github.com/fatih/color package. Unfortunately the original color package is archived and is no longer supported. Like the original, this package lets you use colorized outputs in terms of ANSI Escape Codes in Golang but offers a number of improvements from the original. Posix and Windows platforms are supported.

Color seeks to remain mostly backward compatible with fatih/color but has a number of changes to support concurrency, improved performance and a more idiomatic style.

Changes And Improvements

The methods of the new Color struct do not mutate the sender. This results in better concurrency support and improved performance.

You don't need to remember to wrap io.Writer arguments in colorable.NewColorable in order to support Windows functionality.

Package public global variables are removed. color.NoColor was removed. Colored output can be disabled using the Console.DisableColors method instead. color.Output and color.Error were removed, use Stdout() and Stderr() instead.

Instances of Console can be passed to methods in third party packages that take io.Writer as an argument. If the third party package emits ANSI color information the passed in writer will be interpreted correctly on Windows. In addition, color information can be stripped by calling Console.DisableColors(true).

Performance is improved significantly, as much as 400%. Note that some functions that you'd expect to take an array of interface{} take an array of strings instead because underlying calls to fmt.SprintXX functions are slow.

fatih/color has race conditions. This package was developed with test.Parallel and -race enabled for tests. Thus far no race conditions are known and so this package is suitable for use in a multi goroutine environment.

Examples

Standard colors
// Print with default helper functions
color.Cyan("Prints text in cyan.")

// A newline will be appended automatically
color.Blue("Prints %s in blue.", "text")
Mix and reuse colors
// Create a new color object
color.Stdout().Println(color.New(color.FgCyan, color.Underline), "Prints cyan text with an underline.")
Use your own output (io.Writer)
// Use your own io.Writer output
wtr := color.NewConsole(os.Stderr)
wtr.Println(color.New(color.FgBlue), "Hello! I'm blue.")
Custom print functions (PrintFunc)
// Create a custom print function for convenience
red := color.StdErr().PrintfFunc(color.New(color.FgRed))
red("Warning")
red("Error: %s", err)

// Mix up multiple attributes
notice := color.Stdout().PrintlnFunc(color.New(color.Bold, color.FgGreen))
notice("Don't forget this...")
Insert into noncolor strings (SprintFunc)
// Create SprintXxx functions to mix strings with other non-colorized strings:
yellow := color.New(color.FgYellow).SprintFunc()
red := color.New(color.FgRed).SprintFunc()
fmt.Printf("This is a %s and this is %s.\n", yellow("warning"), red("error"))

info := color.New(color.FgWhite, color.BgGreen).SprintFunc()
fmt.Printf("This %s rocks!\n", info("package"))

// Use helper functions
fmt.Println("This", color.RedString("warning"), "should be not neglected.")
fmt.Printf("%v %v\n", color.GreenString("Info:"), "an important message.")
Disable/Enable color

There might be a case where you want to explicitly disable/enable color output.

Color has support to disable/enable colors on a per Console basis.
For example suppose you have a CLI app and a --no-color bool flag. You can easily disable the color output with:


var flagNoColor = flag.Bool("no-color", false, "Disable color output")
color.Stdout().DisableColors(*flagNoColor)

Credits

License

The MIT License (MIT) - see LICENSE.md for more details

Documentation

Overview

Package color produces colored output in terms of ANSI Escape Codes. Posix and Windows platforms are supported.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Black

func Black(format string, a ...interface{})

Black helper to produce black text to stdout.

func BlackE

func BlackE(format string, a ...interface{})

BlackE helper to produce black text to stderr.

func BlackString

func BlackString(format string, a ...interface{}) string

BlackString returns a string decorated with black attributes.

func Blue

func Blue(format string, a ...interface{})

Blue helper to produce blue text to stdout.

func BlueE

func BlueE(format string, a ...interface{})

BlueE helper to produce blue text to stderr.

func BlueString

func BlueString(format string, a ...interface{}) string

BlueString returns a string decorated with blue attributes.

func Cyan

func Cyan(format string, a ...interface{})

Cyan helper to produce cyan text to stdout.

func CyanE

func CyanE(format string, a ...interface{})

CyanE helper to produce cyan text to stderr.

func CyanString

func CyanString(format string, a ...interface{}) string

CyanString returns a string decorated with cyan attributes.

func Green

func Green(format string, a ...interface{})

Green helper to produce green text to stdout.

func GreenE

func GreenE(format string, a ...interface{})

GreenE helper to produce green text to stderr.

func GreenString

func GreenString(format string, a ...interface{}) string

GreenString returns a string decorated with green attributes.

func HiBlack

func HiBlack(format string, a ...interface{})

HiBlack helper to produce black text to stdout.

func HiBlackE

func HiBlackE(format string, a ...interface{})

HiBlackE helper to produce black text to stderr.

func HiBlackString

func HiBlackString(format string, a ...interface{}) string

HiBlackString returns a high contrast black string.

func HiBlue

func HiBlue(format string, a ...interface{})

HiBlue helper writes high contrast blue text to stdout.

func HiBlueE

func HiBlueE(format string, a ...interface{})

HiBlueE helper writes high contrast blue text to stderr.

func HiBlueString

func HiBlueString(format string, a ...interface{}) string

HiBlueString returns a high contrast blue string.

func HiCyan

func HiCyan(format string, a ...interface{})

HiCyan writes high contrast cyan colored text to stdout.

func HiCyanE

func HiCyanE(format string, a ...interface{})

HiCyanE writes high contrast contrast cyan colored text to stderr.

func HiCyanString

func HiCyanString(format string, a ...interface{}) string

HiCyanString returns a high contrast cyan string.

func HiGreen

func HiGreen(format string, a ...interface{})

HiGreen helper writes high contrast green text to stdout.

func HiGreenE

func HiGreenE(format string, a ...interface{})

HiGreenE helper writes high contrast green text to stderr.

func HiGreenString

func HiGreenString(format string, a ...interface{}) string

HiGreenString returns a high contrast green string.

func HiMagenta

func HiMagenta(format string, a ...interface{})

HiMagenta writes high contrast magenta text to stdout.

func HiMagentaE

func HiMagentaE(format string, a ...interface{})

HiMagentaE writes high contrast magenta text to stderr.

func HiMagentaString

func HiMagentaString(format string, a ...interface{}) string

HiMagentaString returns a high contrast magenta string.

func HiRed

func HiRed(format string, a ...interface{})

HiRed helper to write high contrast red text to stdout.

func HiRedE

func HiRedE(format string, a ...interface{})

HiRedE helper to write high contrast red text to stderr.

func HiRedString

func HiRedString(format string, a ...interface{}) string

HiRedString returns a high contrast contrast black string.

func HiWhite

func HiWhite(format string, a ...interface{})

HiWhite writes high contrast white colored text to stdout.

func HiWhiteE

func HiWhiteE(format string, a ...interface{})

HiWhiteE writes high contrast white colored text to stderr.

func HiWhiteString

func HiWhiteString(format string, a ...interface{}) string

HiWhiteString returns a high contrast white string.

func HiYellow

func HiYellow(format string, a ...interface{})

HiYellow helper writes high contrast yellow text to stdout.

func HiYellowE

func HiYellowE(format string, a ...interface{})

HiYellowE helper writes high contrast yellow text to stderr.

func HiYellowString

func HiYellowString(format string, a ...interface{}) string

HiYellowString returns a high contrast yellow string.

func Magenta

func Magenta(format string, a ...interface{})

Magenta helper to produce magenta text to stdout.

func MagentaE

func MagentaE(format string, a ...interface{})

MagentaE produces magenta text to stderr.

func MagentaString

func MagentaString(format string, a ...interface{}) string

MagentaString returns a string decorated with magenta attributes.

func Red

func Red(format string, a ...interface{})

Red helper to produce red text to stdout.

Example
// Print some red text.
Red("Hello red!")
Output:

func RedE

func RedE(format string, a ...interface{})

RedE helper to produce red text to stderr.

func RedString

func RedString(format string, a ...interface{}) string

RedString returns a string decorated with red attributes.

func White

func White(format string, a ...interface{})

White helper to produce white text to stdout.

func WhiteE

func WhiteE(format string, a ...interface{})

WhiteE helper to produce white text to stderr.

func WhiteString

func WhiteString(format string, a ...interface{}) string

WhiteString returns a string decorated with white attributes.

func Yellow

func Yellow(format string, a ...interface{})

Yellow helper to produce yellow text to stdout.

func YellowE

func YellowE(format string, a ...interface{})

YellowE helper to produce yellow text to stderr.

func YellowString

func YellowString(format string, a ...interface{}) string

YellowString returns a string decorated with yellow attributes.

Types

type Attribute

type Attribute uint64

Attribute defines a single SGR Code.

const (
	Reset Attribute = 1 << iota
	Bold
	Faint
	Italic
	Underline
	BlinkSlow
	BlinkRapid
	ReverseVideo
	Concealed
	CrossedOut
	FgBlack
	FgRed
	FgGreen
	FgYellow
	FgBlue
	FgMagenta
	FgCyan
	FgWhite
	FgHiBlack
	FgHiRed
	FgHiGreen
	FgHiYellow
	FgHiBlue
	FgHiMagenta
	FgHiCyan
	FgHiWhite
	BgBlack
	BgRed
	BgGreen
	BgYellow
	BgBlue
	BgMagenta
	BgCyan
	BgWhite
	BgHiBlack
	BgHiRed
	BgHiGreen
	BgHiYellow
	BgHiBlue
	BgHiMagenta
	BgHiCyan
	BgHiWhite
)

func (Attribute) Name added in v0.0.2

func (a Attribute) Name() string

Name returns a human readable name for an Attribute.

func (Attribute) String

func (a Attribute) String() string

String returns color code as a string.

type Color

type Color struct {
	// contains filtered or unexported fields
}

Color contains methods to create colored strings of text.

func New

func New(attrs ...Attribute) *Color

New creates a Color. It takes a list of Attributes to define the appearance of output.

func (Color) Sprint

func (v Color) Sprint(a ...interface{}) string

Sprint returns text decorated with the display Attributes passed to Color constructor function.

func (Color) SprintFunc

func (v Color) SprintFunc() func(a ...interface{}) string

SprintFunc returns function that wraps Sprint.

Example
// Create functions that add color information
emphasized := New(FgRed, Bold, Underline).SprintFunc()
fmt.Println("Wow, this is", emphasized("exciting!"))
Output:

func (Color) Sprintf

func (v Color) Sprintf(format string, a ...interface{}) string

Sprint formats according to the format specifier and returns text decorated with the display Attributes passed to Color constructor function.

func (Color) SprintfFunc

func (v Color) SprintfFunc() func(format string, a ...interface{}) string

SprintfFunc returns function that wraps Sprintf.

func (Color) Sprintln

func (v Color) Sprintln(a ...interface{}) string

Sprint returns text decorated with the display Attributes and terminated by a line feed.

func (Color) SprintlnFunc

func (v Color) SprintlnFunc() func(a ...interface{}) string

SprintlnFunc returns function that wraps Sprintln.

type Console

type Console struct {
	sync.Mutex
	// contains filtered or unexported fields
}

Console manages state for output, typically stdout or stderr.

func NewConsole

func NewConsole(out *os.File) *Console

NewConsole creates a wrapper around out which will output platform independent colored text.

func Stderr

func Stderr() *Console

Stderr returns an io.Writer that writes colored text to standard error.

func Stdout

func Stdout() *Console

Stdout returns an io.Writer that writes colored text to standard out.

func (*Console) DisableColors

func (c *Console) DisableColors(strip bool)

DisableColors if strip is true ANSI color information will be removed. Otherwise it will be included.

func (*Console) Print added in v0.0.2

func (c *Console) Print(col *Color, args ...string) (int, error)

Print writes colored text to the console. The number of bytes written is returned.

func (*Console) PrintFunc added in v0.0.2

func (c *Console) PrintFunc(col *Color) func(a ...string)

PrintFunc returns a wrapper function for Print.

func (*Console) Printf added in v0.0.2

func (c *Console) Printf(col *Color, format string, args ...interface{}) (int, error)

Printf formats according to a format specifier and writes colored text to the console.

func (*Console) PrintfFunc added in v0.0.2

func (c *Console) PrintfFunc(col *Color) func(format string, args ...interface{})

PrintfFunc returns a wrapper function for Printf.

func (*Console) Println added in v0.0.2

func (c *Console) Println(col *Color, args ...string) (int, error)

Println writes colored text to console, appending input with a line feed. The number of bytes written is returned.

Example
// Output underlined white text to stdout.
clr := New(FgWhite, Underline)
Stdout().Println(clr, "I'm underlined and white!")
Output:

func (*Console) PrintlnFunc added in v0.0.2

func (c *Console) PrintlnFunc(col *Color) func(a ...string)

PrintlnFunc returns a wrapper function for Println.

func (*Console) Set added in v0.0.2

func (c *Console) Set(color *Color)

Set will cause the color passed in as an argument to be written until Unset is called.

func (*Console) Unset added in v0.0.2

func (c *Console) Unset()

Unset will restore console output to default. It will undo colored console output defined from a call to Set.

func (*Console) Write

func (c *Console) Write(b []byte) (int, error)

Write so we can treat a console as a Writer

Directories

Path Synopsis
test
cmd

Jump to

Keyboard shortcuts

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