gocolor

package module
v0.0.0-...-8a458d0 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2022 License: MIT Imports: 9 Imported by: 0

README

Go Color

I love the way fatih/color work and the power of BurntSushi/termcolor.

Kitty terminal

Installation

go get github.com/Nguyen-Hoang-Nam/go-color

Usage

First you need to create new instance with New function then you can use this instance to run function like fmt package. (Currently this library only support Println function)

warning := gocolor.New(gocolor.AnsiEscape(gocolor.FgRed))
warning.Println("Some warning")

One more thing, New function is actually variadic function so you can add as many parameters as you like.

warning := gocolor.New(gocolor.AnsiEscape(gocolor.FgRed), gocolor.AnsiEscape(gocolor.FgYellow))
warning.Println("Some warning")
Add

Sometime, you may want to add or change color then this function is for you

warning := gocolor.New(gocolor.AnsiEscape(gocolor.FgRed)).Add(gocolor.AnsiEscape(gocolor.BgYellow))
warning.Println("Some warning")
RGB

RGB color is the reason why I create this package

warning := gocolor.New(gocolor.FgRGB(255, 0, 0))
warning.Println("Some warning")

In case, your terminal is not support RGB color then I will try to fallback to 256 colors or ansi color base on your flag.

I also support HSV, too. (Thanks to go-colorful)

warning := gocolor.New(gocolor.FgHSV(120, 1, 0.5))
warning.Println("Some warning")
Flags

This topic is quite complicated, there are 5 flags you can set:

  • TrueColor (Only work with true color terminal, otherwise weird character will print in screen)
  • Color256 (Only work with terminal support 256 colors)
  • Ansi (Work in most terminal event Window but look really ugly)
  • NoColor (Really, why use this package with this flag :v)
  • Auto (Default options, try to guest with flag should be use by checking $COLORTERM, $TERM, $NO_COLOR)
truecolor := gocolor.New(gocolor.FgRBG(255, 0, 255)).setFlag(gocolor.TrueColor)

There is one caution, most terminal emulator supports users to customize Ansi color (16 colors). This library simply can not get these customizable colors so I choose default 16 colors of XTerm as fallback colors.

Color256

Yes, this function allows you to choose between 256 colors.

color256 := gocolor.New(gocolor.FgColor256(50))
Ansi

This is quite madness, so you may want to read more in wiki

There are 8 colors, you can combine with 4 prefix (Fg-, Bg-, FgBright-, BgBright-)

  • Black
  • Red
  • Green
  • Yellow
  • Blue
  • Magenta
  • Cyan
  • White
ansiColor := gocolor.New(gocolor.AnsiEscape(gocolor.FgRed))

I create some constants so that you don't need to remember their codes.

  • Reset
  • Bold
  • Dim
  • Italic
  • Underline
  • Reverse
ansiColor := gocolor.New(gocolor.AnsiEscape(gocolor.FgRed), gocolor.AnsiEscape(gocolor.Underline))
Distance

This is also a tough topic, when I try to fallback RGB aka true color to support 256 color or Ansi color terminal. I might need some way to find the color which is the nearest to original color.

  • DistanceRgb (default)
  • DistanceLab
  • DistanceLuv
  • DistanceCIE94
  • DistanceCIEDE2000
distanceLab := gocolor.New(gocolor.FgRBG(255, 0, 255)).SetDistance(gocolor.DistanceLab)

Each distances may give you a completely difference fallback color which is unsurprising

Complementary

Sometime you may want to make background or foreground to the complementary color to make a high contrast message.

complementary := gocolor.New(gocolor.FgRBG(255, 0, 255)).AddComplementary()
Fallback

If you want to change default fallback colors. You can use SetFallback or SetFallbackFromPath function.

fallback := gocolor.New().SetFallbackFromPath("./configuration.json").Add(gocolor.FgColor256(24))

TODO

  • Pipeline
  • Add image to README
  • Support change default Ansi color
  • Add github CI
  • Improve test cases

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

License

MIT

Documentation

Index

Constants

View Source
const (
	TrueColor = iota
	Color256
	Ansi
	NoColor
	Auto
)
View Source
const (
	Reset     = 0
	Bold      = 1
	Dim       = 2
	Italic    = 3
	Underline = 4
	Reverse   = 7

	FgBlack   = 30
	FgRed     = 31
	FgGreen   = 32
	FgYellow  = 33
	FgBlue    = 34
	FgMagenta = 35
	FgCyan    = 36
	FgWhite   = 37

	BgBlack   = 40
	BgRed     = 41
	BgGreen   = 42
	BgYellow  = 43
	BgBlue    = 44
	BgMagenta = 45
	BgCyan    = 46
	BgWhite   = 47

	FgBrightBlack   = 90
	FgBrightRed     = 91
	FgBrightGreen   = 92
	FgBrightYellow  = 93
	FgBrightBlue    = 94
	FgBrightMagenta = 95
	FgBrightCyan    = 96
	FgBrightWhite   = 97

	BgBrightBlack   = 100
	BgBrightRed     = 101
	BgBrightGreen   = 102
	BgBrightYellow  = 103
	BgBrightBlue    = 104
	BgBrightMagenta = 105
	BgBrightCyan    = 106
	BgBrightWhite   = 107
)
View Source
const (
	TERM       = "TERM"
	NO_COLOR   = "NO_COLOR"
	COLOR_TERM = "COLORTERM"
)
View Source
const (
	Kitty = iota
	Alacritty
	Default
)
View Source
const (
	DistanceRgb = iota
	DistanceLab
	DistanceLuv
	DistanceCIE94
	DistanceCIEDE2000
)

Variables

This section is empty.

Functions

func AnsiEscape

func AnsiEscape(escapeCode uint8) colorAttribute

func BgColor256

func BgColor256(color256 uint8) colorAttribute

func BgHSV

func BgHSV(hue float64, saturation float64, value float64) colorAttribute

func BgRGB

func BgRGB(red uint8, green uint8, blue uint8) colorAttribute

func FgColor256

func FgColor256(color256 uint8) colorAttribute

func FgHSV

func FgHSV(hue float64, saturation float64, value float64) colorAttribute

func FgRGB

func FgRGB(red uint8, green uint8, blue uint8) colorAttribute

Types

type ColorAttributes

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

func New

func New(attributes ...colorAttribute) *ColorAttributes

func (*ColorAttributes) Add

func (c *ColorAttributes) Add(attributes ...colorAttribute) *ColorAttributes

func (*ColorAttributes) AddComplementary

func (c *ColorAttributes) AddComplementary() *ColorAttributes

func (*ColorAttributes) Println

func (c *ColorAttributes) Println(text string)

func (*ColorAttributes) SetDistance

func (c *ColorAttributes) SetDistance(distance int) *ColorAttributes

func (*ColorAttributes) SetFallback

func (c *ColorAttributes) SetFallback(colors map[int]colorful.Color) *ColorAttributes

func (*ColorAttributes) SetFallbackFromPath

func (c *ColorAttributes) SetFallbackFromPath(filePath string) *ColorAttributes

func (*ColorAttributes) SetFlag

func (c *ColorAttributes) SetFlag(flag int) *ColorAttributes

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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