color

package module
v2.0.6 Latest Latest
Warning

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

Go to latest
Published: Mar 6, 2020 License: MIT Imports: 9 Imported by: 4

README

Color

CircleCI godoc.org codecov.io Releases GA

Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang). It has support for Windows too! The API can be used in several ways, pick one that suits you.

Color

Install

go get -u github.com/zchee/color/v2

Note that the vendor folder is here for stability. Remove the folder if you already have the dependencies in your GOPATH.

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")

// These are using the default foreground colors
color.Red("We have red")
color.Magenta("And many others ..")

Mix and reuse colors
// Create a new color object
c := color.New(color.FgCyan).Add(color.Underline)
c.Println("Prints cyan text with an underline.")

// Or just add them to New()
d := color.New(color.FgCyan, color.Bold)
d.Printf("This prints bold cyan %s\n", "too!.")

// Mix up foreground and background colors, create new mixes!
red := color.New(color.FgRed)

boldRed := red.Add(color.Bold)
boldRed.Println("This will print text in bold red.")

whiteBackground := red.Add(color.BgWhite)
whiteBackground.Println("Red text with white background.")
Use your own output (io.Writer)
// Use your own io.Writer output
color.New(color.FgBlue).Fprintln(myWriter, "blue color!")

blue := color.New(color.FgBlue)
blue.Fprint(writer, "This will print text in blue.")
Custom print functions (PrintFunc)
// Create a custom print function for convenience
red := color.New(color.FgRed).PrintfFunc()
red("Warning")
red("Error: %s", err)

// Mix up multiple attributes
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
notice("Don't forget this...")
Custom fprint functions (FprintFunc)
blue := color.New(FgBlue).FprintfFunc()
blue(myWriter, "important notice: %s", stars)

// Mix up with multiple attributes
success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
success(myWriter, "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.")

// Windows supported too! Just don't forget to change the output to color.Output
fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))
Plug into existing code
// Use handy standard colors
color.Set(color.FgYellow)

fmt.Println("Existing text will now be in yellow")
fmt.Printf("This one %s\n", "too")

color.Unset() // Don't forget to unset

// You can mix up parameters
color.Set(color.FgMagenta, color.Bold)
defer color.Unset() // Use it in your function

fmt.Println("All text will now be bold magenta.")
Disable/Enable color

There might be a case where you want to explicitly disable/enable color output. the go-isatty package will automatically disable color output for non-tty output streams (for example if the output were piped directly to less)

Color has support to disable/enable colors both globally and for single color definitions. 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")

if *flagNoColor {
	color.NoColor = true // disables colorized output
}

It also has support for single color definitions (local). You can disable/enable color output on the fly:

c := color.New(color.FgCyan)
c.Println("Prints cyan text")

c.DisableColor()
c.Println("This is printed without any color")

c.EnableColor()
c.Println("This prints again cyan...")

Benchmark

Run benchmark
cd ./benchmarks
go mod vendor -v
go test -v -mod=vendor -tags=benchmark -cpu 1,4,12 -count 10 -run='^$' -bench=. -benchtime=.1s . -fatih | tee old.txt
go test -v -mod=vendor -tags=benchmark -cpu 1,4,12 -count 10 -run='^$' -bench=. -benchtime=.1s . | tee new.txt
benchstat old.txt new.txt
Benchmark result

On my Macbook Pro.

$ system_profiler SPHardwareDataType
# (omitted)
Model Name: MacBook Pro
Model Identifier: MacBookPro15,1
Processor Name: Intel Core i9
Processor Speed: 2.9 GHz
Number of Processors: 1
Total Number of Cores: 6
L2 Cache (per Core): 256 KB
L3 Cache: 12 MB
Memory: 32 GB

$ list_cpu_features
arch            : x86
brand           : Intel(R) Core(TM) i9-8950HK CPU @ 2.90GHz
family          :   6 (0x06)
model           : 158 (0x9E)
stepping        :  10 (0x0A)
uarch           : INTEL_KBL
flags           : aes,avx,avx2,bmi1,bmi2,cx16,erms,f16c,fma3,movbe,popcnt,rdrnd,sgx,sse4_1,sse4_2,ssse3

$ lscpu
Architecture:            x86_64
Byte Order:              Little Endian
Total CPU(s):            12
Thread(s) per core:      2
Core(s) per socket:      6
Socket(s):               1
Vendor:                  GenuineIntel
CPU family:              6
Model:                   158
Model name:              MacBookPro15,1
Stepping:                10
L1d cache:               32K
L1i cache:               32K
L2 cache:                256K
L3 cache:                12M
Flags:                   fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 cflsh ds acpi mmx fxsr sse sse2 ss htt tm pbe sse3 pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline aes xsave osxsave avx f16c rdrnd syscall nx pdpe1gb rdtscp lm lahf_lm lzcnt
name                  old time/op    new time/op    delta
NewPrint                18.3µs ± 9%    20.0µs ± 2%   +8.97%  (p=0.000 n=10+9)
NewPrint-4              5.25µs ± 5%    5.34µs ±10%     ~     (p=0.605 n=9+9)
NewPrint-12             3.35µs ±23%    3.62µs ±34%     ~     (p=1.000 n=9+10)
ColorPrint              2.41µs ± 5%    2.13µs ± 3%  -11.76%  (p=0.000 n=10+8)
ColorPrint-4             724ns ±20%     603ns ± 4%  -16.70%  (p=0.000 n=10+8)
ColorPrint-12            526ns ±11%     437ns ± 1%  -16.80%  (p=0.000 n=9+8)
ColorString             3.32µs ± 3%    3.01µs ±19%   -9.28%  (p=0.005 n=9+10)
ColorString-4           1.07µs ±11%    0.99µs ± 4%   -8.11%  (p=0.000 n=9+8)
ColorString-12          1.14µs ±21%    1.01µs ± 5%  -11.35%  (p=0.002 n=10+10)
GetCacheColorFg         49.2ns ±15%    18.2ns ± 5%  -62.99%  (p=0.000 n=10+8)
GetCacheColorFg-4       64.7ns ±12%     6.5ns ±39%  -90.03%  (p=0.000 n=10+10)
GetCacheColorFg-12      99.1ns ± 7%     3.5ns ± 1%  -96.43%  (p=0.000 n=9+10)
GetCacheColorFgHi       48.1ns ±11%    18.1ns ± 7%  -62.45%  (p=0.000 n=9+8)
GetCacheColorFgHi-4     69.9ns ±10%     5.3ns ± 8%  -92.40%  (p=0.000 n=9+8)
GetCacheColorFgHi-12     105ns ± 4%       4ns ± 2%  -96.58%  (p=0.000 n=9+10)
GetCacheColorBg         50.9ns ± 9%    18.5ns ± 6%  -63.58%  (p=0.000 n=10+8)
GetCacheColorBg-4       73.5ns ±10%     6.1ns ±44%  -91.67%  (p=0.000 n=10+10)
GetCacheColorBg-12       106ns ± 6%       4ns ± 1%  -96.61%  (p=0.000 n=8+8)
GetCacheColorBgHi       51.5ns ±11%    19.3ns ±16%  -62.51%  (p=0.000 n=10+9)
GetCacheColorBgHi-4     71.7ns ± 7%     6.5ns ±50%  -90.90%  (p=0.000 n=9+10)
GetCacheColorBgHi-12     106ns ± 9%       4ns ± 4%  -96.56%  (p=0.000 n=10+9)
ColorPrintFg            2.60µs ± 5%    2.31µs ±11%  -11.05%  (p=0.000 n=10+9)
ColorPrintFg-4           711ns ±10%     628ns ± 6%  -11.72%  (p=0.000 n=9+8)
ColorPrintFg-12          529ns ±12%     483ns ±36%   -8.77%  (p=0.029 n=9+9)
ColorPrintFgHi          2.80µs ±11%    2.50µs ±21%  -10.81%  (p=0.009 n=10+10)
ColorPrintFgHi-4         715ns ±18%     633ns ± 6%  -11.46%  (p=0.000 n=9+8)
ColorPrintFgHi-12        566ns ±24%     496ns ±41%  -12.37%  (p=0.021 n=10+9)
ColorPrintBgHi          2.91µs ±12%    2.59µs ±22%  -10.78%  (p=0.011 n=10+10)
ColorPrintBgHi-4         726ns ± 7%     636ns ± 5%  -12.30%  (p=0.000 n=9+9)
ColorPrintBgHi-12        605ns ±24%     556ns ±38%     ~     (p=0.271 n=10+10)
ColorStringFg           3.80µs ±16%    3.41µs ±12%  -10.39%  (p=0.017 n=10+9)
ColorStringFg-4         1.19µs ±16%    1.06µs ± 5%  -10.48%  (p=0.000 n=10+10)
ColorStringFg-12        1.22µs ±14%    1.18µs ± 3%     ~     (p=0.943 n=10+7)
ColorStringFgHi         3.80µs ±11%    3.44µs ±15%   -9.49%  (p=0.011 n=10+10)
ColorStringFgHi-4       1.14µs ±12%    1.05µs ± 3%   -8.25%  (p=0.001 n=10+8)
ColorStringFgHi-12      1.24µs ±13%    1.16µs ± 5%   -5.98%  (p=0.008 n=10+10)
ColorStringBgHi         3.79µs ±23%    3.32µs ±11%  -12.41%  (p=0.006 n=10+9)
ColorStringBgHi-4       1.15µs ±13%    1.06µs ± 8%   -7.99%  (p=0.028 n=10+8)
ColorStringBgHi-12      1.29µs ±17%    1.08µs ± 8%  -16.07%  (p=0.001 n=10+9)

name                  old alloc/op   new alloc/op   delta
NewPrint                 87.0B ± 0%     66.0B ± 0%  -24.14%  (p=0.000 n=10+9)
NewPrint-4               87.4B ± 1%     66.4B ± 2%  -24.03%  (p=0.000 n=10+10)
NewPrint-12              90.9B ± 1%     69.8B ± 3%  -23.21%  (p=0.000 n=10+10)
ColorPrint               85.0B ± 0%     64.0B ± 0%  -24.71%  (p=0.000 n=10+10)
ColorPrint-4             85.0B ± 0%     64.0B ± 0%  -24.71%  (p=0.000 n=10+10)
ColorPrint-12            86.0B ± 0%     64.0B ± 0%  -25.58%  (p=0.000 n=10+10)
ColorString             4.72kB ± 0%    4.69kB ± 0%   -0.78%  (p=0.000 n=9+10)
ColorString-4           4.74kB ± 0%    4.70kB ± 0%   -0.79%  (p=0.000 n=10+10)
ColorString-12          4.77kB ± 0%    4.73kB ± 0%   -0.82%  (p=0.000 n=10+10)
GetCacheColorFg          0.00B          0.00B          ~     (all equal)
GetCacheColorFg-4        0.00B          0.00B          ~     (all equal)
GetCacheColorFg-12       0.00B          0.00B          ~     (all equal)
GetCacheColorFgHi        0.00B          0.00B          ~     (all equal)
GetCacheColorFgHi-4      0.00B          0.00B          ~     (all equal)
GetCacheColorFgHi-12     0.00B          0.00B          ~     (all equal)
GetCacheColorBg          0.00B          0.00B          ~     (all equal)
GetCacheColorBg-4        0.00B          0.00B          ~     (all equal)
GetCacheColorBg-12       0.00B          0.00B          ~     (all equal)
GetCacheColorBgHi        0.00B          0.00B          ~     (all equal)
GetCacheColorBgHi-4      0.00B          0.00B          ~     (all equal)
GetCacheColorBgHi-12     0.00B          0.00B          ~     (all equal)
ColorPrintFg             85.0B ± 0%     64.0B ± 0%  -24.71%  (p=0.000 n=10+10)
ColorPrintFg-4           85.0B ± 0%     64.0B ± 0%  -24.71%  (p=0.000 n=10+10)
ColorPrintFg-12          86.0B ± 0%     64.0B ± 0%  -25.58%  (p=0.000 n=10+10)
ColorPrintFgHi           85.0B ± 0%     64.0B ± 0%  -24.71%  (p=0.000 n=10+10)
ColorPrintFgHi-4         85.0B ± 0%     64.0B ± 0%  -24.71%  (p=0.000 n=10+10)
ColorPrintFgHi-12        86.0B ± 0%     64.0B ± 0%  -25.58%  (p=0.000 n=10+10)
ColorPrintBgHi           90.0B ± 0%     64.0B ± 0%  -28.89%  (p=0.000 n=10+10)
ColorPrintBgHi-4         91.0B ± 0%     64.0B ± 0%  -29.67%  (p=0.000 n=10+10)
ColorPrintBgHi-12        91.0B ± 0%     64.0B ± 0%  -29.67%  (p=0.000 n=9+9)
ColorStringFg           4.72kB ± 0%    4.68kB ± 0%   -0.78%  (p=0.000 n=9+9)
ColorStringFg-4         4.74kB ± 0%    4.70kB ± 0%   -0.79%  (p=0.000 n=7+10)
ColorStringFg-12        4.76kB ± 0%    4.72kB ± 1%   -0.94%  (p=0.000 n=10+10)
ColorStringFgHi         4.72kB ± 0%    4.69kB ± 0%   -0.78%  (p=0.000 n=10+10)
ColorStringFgHi-4       4.74kB ± 0%    4.70kB ± 0%   -0.78%  (p=0.000 n=10+9)
ColorStringFgHi-12      4.77kB ± 0%    4.72kB ± 0%   -0.99%  (p=0.000 n=9+10)
ColorStringBgHi         4.72kB ± 0%    4.69kB ± 0%   -0.75%  (p=0.000 n=8+10)
ColorStringBgHi-4       4.74kB ± 0%    4.70kB ± 0%   -0.80%  (p=0.000 n=9+8)
ColorStringBgHi-12      4.77kB ± 0%    4.73kB ± 0%   -0.69%  (p=0.000 n=10+9)

name                  old allocs/op  new allocs/op  delta
NewPrint                  5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
NewPrint-4                5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
NewPrint-12               5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
ColorPrint                5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
ColorPrint-4              5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
ColorPrint-12             5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
ColorString               9.00 ± 0%      6.00 ± 0%  -33.33%  (p=0.000 n=10+10)
ColorString-4             9.00 ± 0%      6.00 ± 0%  -33.33%  (p=0.000 n=10+10)
ColorString-12            9.00 ± 0%      6.00 ± 0%  -33.33%  (p=0.000 n=10+10)
GetCacheColorFg           0.00           0.00          ~     (all equal)
GetCacheColorFg-4         0.00           0.00          ~     (all equal)
GetCacheColorFg-12        0.00           0.00          ~     (all equal)
GetCacheColorFgHi         0.00           0.00          ~     (all equal)
GetCacheColorFgHi-4       0.00           0.00          ~     (all equal)
GetCacheColorFgHi-12      0.00           0.00          ~     (all equal)
GetCacheColorBg           0.00           0.00          ~     (all equal)
GetCacheColorBg-4         0.00           0.00          ~     (all equal)
GetCacheColorBg-12        0.00           0.00          ~     (all equal)
GetCacheColorBgHi         0.00           0.00          ~     (all equal)
GetCacheColorBgHi-4       0.00           0.00          ~     (all equal)
GetCacheColorBgHi-12      0.00           0.00          ~     (all equal)
ColorPrintFg              5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
ColorPrintFg-4            5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
ColorPrintFg-12           5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
ColorPrintFgHi            5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
ColorPrintFgHi-4          5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
ColorPrintFgHi-12         5.00 ± 0%      4.00 ± 0%  -20.00%  (p=0.000 n=10+10)
ColorPrintBgHi            6.00 ± 0%      4.00 ± 0%  -33.33%  (p=0.000 n=10+10)
ColorPrintBgHi-4          6.00 ± 0%      4.00 ± 0%  -33.33%  (p=0.000 n=10+10)
ColorPrintBgHi-12         6.00 ± 0%      4.00 ± 0%  -33.33%  (p=0.000 n=10+10)
ColorStringFg             9.00 ± 0%      6.00 ± 0%  -33.33%  (p=0.000 n=10+10)
ColorStringFg-4           9.00 ± 0%      6.00 ± 0%  -33.33%  (p=0.000 n=10+10)
ColorStringFg-12          9.00 ± 0%      6.00 ± 0%  -33.33%  (p=0.000 n=10+10)
ColorStringFgHi           9.00 ± 0%      6.00 ± 0%  -33.33%  (p=0.000 n=10+10)
ColorStringFgHi-4         9.00 ± 0%      6.00 ± 0%  -33.33%  (p=0.000 n=10+10)
ColorStringFgHi-12        9.00 ± 0%      6.00 ± 0%  -33.33%  (p=0.000 n=10+10)
ColorStringBgHi           10.0 ± 0%       6.0 ± 0%  -40.00%  (p=0.000 n=10+10)
ColorStringBgHi-4         10.0 ± 0%       6.0 ± 0%  -40.00%  (p=0.000 n=10+10)
ColorStringBgHi-12        10.0 ± 0%       6.0 ± 0%  -40.00%  (p=0.000 n=10+10)

Todo

  • Save/Return previous values
  • Evaluate fmt.Formatter interface

Credits

License

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

Documentation

Overview

Package color is an ANSI color package to output colorized or SGR defined output to the standard output. The API can be used in several way, pick one that suits you.

Use simple and default helper functions with predefined foreground colors:

color.Cyan("Prints text in cyan.")

// a newline will be appended automatically
color.Blue("Prints %s in blue.", "text")

// More default foreground colors..
color.Red("We have red")
color.Yellow("Yellow color too!")
color.Magenta("And many others ..")

// Hi-intensity colors
color.HiGreen("Bright green color.")
color.HiBlack("Bright black means gray..")
color.HiWhite("Shiny white color!")

However there are times where custom color mixes are required. Below are some examples to create custom color objects and use the print functions of each separate color object.

// Create a new color object
c := color.New(color.FgCyan).Add(color.Underline)
c.Println("Prints cyan text with an underline.")

// Or just add them to New()
d := color.New(color.FgCyan, color.Bold)
d.Printf("This prints bold cyan %s\n", "too!.")

// Mix up foreground and background colors, create new mixes!
red := color.New(color.FgRed)

boldRed := red.Add(color.Bold)
boldRed.Println("This will print text in bold red.")

whiteBackground := red.Add(color.BgWhite)
whiteBackground.Println("Red text with White background.")

// Use your own io.Writer output
color.New(color.FgBlue).Fprintln(myWriter, "blue color!")

blue := color.New(color.FgBlue)
blue.Fprint(myWriter, "This will print text in blue.")

You can create PrintXxx functions to simplify even more:

// Create a custom print function for convenient
red := color.New(color.FgRed).PrintfFunc()
red("warning")
red("error: %s", err)

// Mix up multiple attributes
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
notice("don't forget this...")

You can also FprintXxx functions to pass your own io.Writer:

blue := color.New(FgBlue).FprintfFunc()
blue(myWriter, "important notice: %s", stars)

// Mix up with multiple attributes
success := color.New(color.Bold, color.FgGreen).FprintlnFunc()
success(myWriter, don't forget this...")

Or create SprintXxx functions to mix strings with other non-colorized strings:

yellow := New(FgYellow).SprintFunc()
red := New(FgRed).SprintFunc()

fmt.Printf("this is a %s and this is %s.\n", yellow("warning"), red("error"))

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

Windows support is enabled by default. All Print functions work as intended. However only for color.SprintXXX functions, user should use fmt.FprintXXX and set the output to color.Output:

fmt.Fprintf(color.Output, "Windows support: %s", color.GreenString("PASS"))

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

Using with existing code is possible. Just use the Set() method to set the standard output to the given parameters. That way a rewrite of an existing code is not required.

// Use handy standard colors.
color.Set(color.FgYellow)

fmt.Println("Existing text will be now in Yellow")
fmt.Printf("This one %s\n", "too")

color.Unset() // don't forget to unset

// You can mix up parameters
color.Set(color.FgMagenta, color.Bold)
defer color.Unset() // use it in your function

fmt.Println("All text will be now bold magenta.")

There might be a case where you want to disable color output (for example to pipe the standard output of your app to somewhere else). `Color` has support to disable colors both globally and for single color definition. 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")

if *flagNoColor {
	color.NoColor = true // disables colorized output
}

It also has support for single color definitions (local). You can disable/enable color output on the fly:

c := color.New(color.FgCyan)
c.Println("Prints cyan text")

c.DisableColor()
c.Println("This is printed without any color")

c.EnableColor()
c.Println("This prints again cyan...")

Index

Constants

This section is empty.

Variables

View Source
var (
	// NoColor defines if the output is colorized or not. It's dynamically set to
	// false or true based on the stdout's file descriptor referring to a terminal
	// or not. This is a global option and affects all colors. For more control
	// over each color block use the methods DisableColor() individually.
	NoColor = os.Getenv("TERM") == "dumb" ||
		(!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd()))

	// Output defines the standard output of the print functions. By default
	// os.Stdout is used.
	Output = colorable.NewColorableStdout()

	// Error defines a color supporting writer for os.Stderr.
	Error = colorable.NewColorableStderr()
)

Functions

func Black

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

Black is a convenient helper function to print with black foreground. A newline is appended to format by default.

func BlackString

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

BlackString is a convenient helper function to return a string with black foreground.

func Blue

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

Blue is a convenient helper function to print with blue foreground. A newline is appended to format by default.

func BlueString

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

BlueString is a convenient helper function to return a string with blue foreground.

func Cyan

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

Cyan is a convenient helper function to print with cyan foreground. A newline is appended to format by default.

func CyanString

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

CyanString is a convenient helper function to return a string with cyan foreground.

func Green

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

Green is a convenient helper function to print with green foreground. A newline is appended to format by default.

func GreenString

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

GreenString is a convenient helper function to return a string with green foreground.

func HiBlack

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

HiBlack is a convenient helper function to print with hi-intensity black foreground. A newline is appended to format by default.

func HiBlackString

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

HiBlackString is a convenient helper function to return a string with hi-intensity black foreground.

func HiBlue

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

HiBlue is a convenient helper function to print with hi-intensity blue foreground. A newline is appended to format by default.

func HiBlueString

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

HiBlueString is a convenient helper function to return a string with hi-intensity blue foreground.

func HiCyan

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

HiCyan is a convenient helper function to print with hi-intensity cyan foreground. A newline is appended to format by default.

func HiCyanString

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

HiCyanString is a convenient helper function to return a string with hi-intensity cyan foreground.

func HiGreen

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

HiGreen is a convenient helper function to print with hi-intensity green foreground. A newline is appended to format by default.

func HiGreenString

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

HiGreenString is a convenient helper function to return a string with hi-intensity green foreground.

func HiMagenta

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

HiMagenta is a convenient helper function to print with hi-intensity magenta foreground. A newline is appended to format by default.

func HiMagentaString

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

HiMagentaString is a convenient helper function to return a string with hi-intensity magenta foreground.

func HiRed

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

HiRed is a convenient helper function to print with hi-intensity red foreground. A newline is appended to format by default.

func HiRedString

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

HiRedString is a convenient helper function to return a string with hi-intensity red foreground.

func HiWhite

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

HiWhite is a convenient helper function to print with hi-intensity white foreground. A newline is appended to format by default.

func HiWhiteString

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

HiWhiteString is a convenient helper function to return a string with hi-intensity white foreground.

func HiYellow

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

HiYellow is a convenient helper function to print with hi-intensity yellow foreground. A newline is appended to format by default.

func HiYellowString

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

HiYellowString is a convenient helper function to return a string with hi-intensity yellow foreground.

func Magenta

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

Magenta is a convenient helper function to print with magenta foreground. A newline is appended to format by default.

func MagentaString

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

MagentaString is a convenient helper function to return a string with magenta foreground.

func Red

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

Red is a convenient helper function to print with red foreground. A newline is appended to format by default.

func RedString

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

RedString is a convenient helper function to return a string with red foreground.

func Unset

func Unset()

Unset resets all escape attributes and clears the output. Usually should be called after Set().

func White

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

White is a convenient helper function to print with white foreground. A newline is appended to format by default.

func WhiteString

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

WhiteString is a convenient helper function to return a string with white foreground.

func Yellow

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

Yellow is a convenient helper function to print with yellow foreground. A newline is appended to format by default.

func YellowString

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

YellowString is a convenient helper function to return a string with yellow foreground.

Types

type Attribute

type Attribute int

Attribute defines a single SGR Code.

const (
	Reset Attribute = iota
	Bold
	Faint
	Italic
	Underline
	BlinkSlow
	BlinkRapid
	ReverseVideo
	Concealed
	CrossedOut
)

Base attributes.

const (
	FgBlack Attribute = iota + 30
	FgRed
	FgGreen
	FgYellow
	FgBlue
	FgMagenta
	FgCyan
	FgWhite
)

Foreground text colors.

const (
	FgHiBlack Attribute = iota + 90
	FgHiRed
	FgHiGreen
	FgHiYellow
	FgHiBlue
	FgHiMagenta
	FgHiCyan
	FgHiWhite
)

Foreground Hi-Intensity text colors.

const (
	BgBlack Attribute = iota + 40
	BgRed
	BgGreen
	BgYellow
	BgBlue
	BgMagenta
	BgCyan
	BgWhite
)

Background text colors.

const (
	BgHiBlack Attribute = iota + 100
	BgHiRed
	BgHiGreen
	BgHiYellow
	BgHiBlue
	BgHiMagenta
	BgHiCyan
	BgHiWhite
)

Background Hi-Intensity text colors.

func (Attribute) Name

func (i Attribute) Name() string

Name name of Attribute.

func (Attribute) String

func (a Attribute) String() string

String implements fmt.Stringer.

type AttributeHash

type AttributeHash uint16

AttributeHash is the key of colorCache with hashed Attributes.

type Color

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

Color defines a custom color object which is defined by SGR parameters.

func New

func New(value ...Attribute) *Color

New returns a newly created color object.

func Set

func Set(p ...Attribute) (c *Color)

Set sets the given parameters immediately. It will change the color of output with the given SGR parameters until color.Unset() is called.

func (*Color) Add

func (c *Color) Add(value ...Attribute) *Color

Add is used to chain SGR parameters. Use as many as parameters to combine and create custom color objects. Example: Add(color.FgRed, color.Underline).

func (*Color) DisableColor

func (c *Color) DisableColor()

DisableColor disables the color output. Useful to not change any existing code and still being able to output. Can be used for flags like "--no-color". To enable back use EnableColor() method.

func (*Color) EnableColor

func (c *Color) EnableColor()

EnableColor enables the color output. Use it in conjunction with DisableColor(). Otherwise this method has no side effects.

func (*Color) Equals

func (c *Color) Equals(c2 *Color) bool

Equals returns a boolean value indicating whether two colors are equal.

func (*Color) Fprint

func (c *Color) Fprint(w io.Writer, a ...interface{}) (n int, err error)

Fprint formats using the default formats for its operands and writes to w. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered. On Windows, users should wrap w with colorable.NewColorable() if w is of type *os.File.

func (*Color) FprintFunc

func (c *Color) FprintFunc() FprintFunc

FprintFunc returns a new function that prints the passed arguments as colorized with color.Fprint().

func (*Color) Fprintf

func (c *Color) Fprintf(w io.Writer, format string, a ...interface{}) (n int, err error)

Fprintf formats according to a format specifier and writes to w. It returns the number of bytes written and any write error encountered. On Windows, users should wrap w with colorable.NewColorable() if w is of type *os.File.

func (*Color) FprintfFunc

func (c *Color) FprintfFunc() FprintfFunc

FprintfFunc returns a new function that prints the passed arguments as colorized with color.Fprintf().

func (*Color) Fprintln

func (c *Color) Fprintln(w io.Writer, a ...interface{}) (n int, err error)

Fprintln formats using the default formats for its operands and writes to w. Spaces are always added between operands and a newline is appended. On Windows, users should wrap w with colorable.NewColorable() if w is of type *os.File.

func (*Color) FprintlnFunc

func (c *Color) FprintlnFunc() FprintlnFunc

FprintlnFunc returns a new function that prints the passed arguments as colorized with color.Fprintln().

func (*Color) Prepend

func (c *Color) Prepend(value Attribute)

Prepend prepends value Attribute to c.

func (*Color) Print

func (c *Color) Print(a ...interface{}) (n int, err error)

Print formats using the default formats for its operands and writes to standard output. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered. This is the standard fmt.Print() method wrapped with the given color.

func (*Color) PrintFunc

func (c *Color) PrintFunc() PrintFunc

PrintFunc returns a new function that prints the passed arguments as colorized with color.Print().

func (*Color) Printf

func (c *Color) Printf(format string, a ...interface{}) (n int, err error)

Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered. This is the standard fmt.Printf() method wrapped with the given color.

func (*Color) PrintfFunc

func (c *Color) PrintfFunc() PrintfFunc

PrintfFunc returns a new function that prints the passed arguments as colorized with color.Printf().

func (*Color) Println

func (c *Color) Println(a ...interface{}) (n int, err error)

Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered. This is the standard fmt.Print() method wrapped with the given color.

func (*Color) PrintlnFunc

func (c *Color) PrintlnFunc() PrintlnFunc

PrintlnFunc returns a new function that prints the passed arguments as colorized with color.Println().

func (*Color) Reset

func (c *Color) Reset()

Reset resets the c.params slice.

func (*Color) Set

func (c *Color) Set() *Color

Set sets the SGR sequence.

func (*Color) Sprint

func (c *Color) Sprint(a ...interface{}) string

Sprint is just like Print, but returns a string instead of printing it.

func (*Color) SprintFunc

func (c *Color) SprintFunc() SprintFunc

SprintFunc returns a new function that returns colorized strings for the given arguments with fmt.Sprint(). Useful to put into or mix into other string. Windows users should use this in conjunction with color.Output, example:

put := New(FgYellow).SprintFunc()
fmt.Fprintf(color.Output, "This is a %s", put("warning"))

func (*Color) Sprintf

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

Sprintf is just like Printf, but returns a string instead of printing it.

func (*Color) SprintfFunc

func (c *Color) SprintfFunc() SprintfFunc

SprintfFunc returns a new function that returns colorized strings for the given arguments with fmt.Sprintf(). Useful to put into or mix into other string. Windows users should use this in conjunction with color.Output.

func (*Color) Sprintln

func (c *Color) Sprintln(a ...interface{}) string

Sprintln is just like Println, but returns a string instead of printing it.

func (*Color) SprintlnFunc

func (c *Color) SprintlnFunc() SprintlnFunc

SprintlnFunc returns a new function that returns colorized strings for the given arguments with fmt.Sprintln(). Useful to put into or mix into other string. Windows users should use this in conjunction with color.Output.

type FprintFunc

type FprintFunc func(w io.Writer, a ...interface{})

FprintFunc represents a FprintFunc method return type.

type FprintfFunc

type FprintfFunc func(w io.Writer, format string, a ...interface{})

FprintfFunc represents a FprintfFunc method return type.

type FprintlnFunc

type FprintlnFunc func(w io.Writer, a ...interface{})

FprintlnFunc represents a FprintlnFunc method return type.

type PrintFunc

type PrintFunc func(a ...interface{})

PrintFunc represents a PrintFunc method return type.

type PrintfFunc

type PrintfFunc func(format string, a ...interface{})

PrintfFunc represents a PrintfFunc method return type.

type PrintlnFunc

type PrintlnFunc func(a ...interface{})

PrintlnFunc represents a PrintlnFunc method return type.

type SprintFunc

type SprintFunc func(a ...interface{}) string

SprintFunc represents a SprintFunc method return type.

type SprintfFunc

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

SprintfFunc represents a SprintfFunc method return type.

type SprintlnFunc

type SprintlnFunc func(a ...interface{}) string

SprintlnFunc represents a SprintlnFunc method return type.

Jump to

Keyboard shortcuts

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