Colorize is a simple and handy Go package that lets you use colors and styling in your console!
go get github.com/lorossi/colorize
This package uses no dependancies exluding the built-in functions.
There are a lot of packages similar (and even better!) than this one. But since I wanted to use colored terminal for my project Journal, I decided to make my own package, as a learning experience. I don't regret it at all. It's been fun so far.
Set the style manually:
colorize.SetStyle(colorize.Bold, colorize.FgBrightBlue, colorize.BgBrightYellow)
Fmt.Println("WOW")
colorize.ResetStyle()
Set an rgb value for the text:
colorize.SetFgRGB(255, 0, 0)
Fmt.Println("Colored!")
colorize.ResetStyle()
Set an rgb value for the background:
colorize.SetBgRGB(0, 255, 0)
Fmt.Println("Now is green!")
colorize.ResetStyle()
Set truecolors text and background colors! This gives the users more colors than its rgb counterpart but it's less supported (Win10 powershell and linux terminal support this):
colorize.SetFgTruecolor(255, 255, 0)
colorize.SetBgTruecolor(0, 0, 255)
Fmt.Println("Everything is so colorful!")
colorize.ResetStyle()
Set color by HSL values:
colorize.SetFgTruecolorHSL(92, 255, 127)
colorize.SetBgTruecolorHSL(112, 255, 127)
fmt.Println("RED on GREEN!")
colorize.ResetStyle()
Never foget to reset the style via the ResetStyle()
function!
Set a text color, background color or style with the quick functions:
fmt.Println(colorize.Green("Green text!", 123, "also green numbers!"))
fmt.Println(colorize.BrightMagentaBg("So magenta and so bright!"))
fmt.Println(colorize.Bold("This is so bold!"))
If you need to style only a string with default constants (no RGB, HSL or Truetype) you can also use the oneliner function:
fmt.Println(StyleText("This is waaay faster!", colorize.FgBrightWhite, colorize.BgBlue, colorize.Bold, colorize.Underline))
s := StyleText("This is the opposite!", colorize.FgBlue, colorize.BgBrightWhite, colorize.Bold, colorize.Underline)
fmt.Println(s)
Note that there's no need to reset the style later on.
See a few more examples here.
FgBlack
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
FgBrightBlack
FgBrightRed
FgBrightGreen
FgBrightYellow
FgBrightBlue
FgBrightMagenta
FgBrightCyan
FgBrightWhite
BgBlack
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
BgBrightBlack
BgBrightRed
BgBrightGreen
BgBrightYellow
BgBrightBlue
BgBrightMagenta
BgBrightCyan
BgBrightWhite
Bold
Faint
Italic
Underline
SlowBlink
RapidBlink
Invert
Hide
Strike
Framed // Not widely supported
Encircled // Not widely supported
Red()
Green()
Yellow()
Blue()
Magenta()
Cyan()
White()
BrightRed()
BrightGreen()
BrightYellow()
BrightBlue()
BrightMagenta()
BrightCyan()
BrightWhite()
RedBg()
GreenBg()
YellowBg()
BlueBg()
MagentaBg()
CyanBg()
WhiteBg()
BrightRedBg()
BrightGreenBg()
BrightYellowBg()
BrightBlueBg()
BrightMagentaBg()
BrightCyanBg()
BrightWhiteBg()
BoldStyle()
FaintStyle()
ItalicStyle()
UnderlineStyle()
SlowBlinkStyle()
RapidBlinkStyle()
InvertStyle()
HideStyle()
StrikeStyle()
FramedStyle() // Not widely supported
EncircledStyle() // Not widely supported
This project is distributed under CC 4.0 License.