Skip to content

lorossi/colorize

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

48 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Colorize

Colorize is a simple and handy Go package that lets you use colors and styling in your console!

Installation

go get github.com/lorossi/colorize

Documentation

Read the docs here!

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.

Examples

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.

List of constants

Text color constants

FgBlack
FgRed
FgGreen
FgYellow
FgBlue
FgMagenta
FgCyan
FgWhite
FgBrightBlack
FgBrightRed
FgBrightGreen
FgBrightYellow
FgBrightBlue
FgBrightMagenta
FgBrightCyan
FgBrightWhite

Backgroud color constants

BgBlack
BgRed
BgGreen
BgYellow
BgBlue
BgMagenta
BgCyan
BgWhite
BgBrightBlack
BgBrightRed
BgBrightGreen
BgBrightYellow
BgBrightBlue
BgBrightMagenta
BgBrightCyan
BgBrightWhite

Text decoration constants

Bold
Faint
Italic
Underline
SlowBlink
RapidBlink
Invert
Hide
Strike
Framed  // Not widely supported
Encircled // Not widely supported

Quick functions

Text color functions

Red()
Green()
Yellow()
Blue()
Magenta()
Cyan()
White()
BrightRed()
BrightGreen()
BrightYellow()
BrightBlue()
BrightMagenta()
BrightCyan()
BrightWhite()

Background color functions

RedBg()
GreenBg()
YellowBg()
BlueBg()
MagentaBg()
CyanBg()
WhiteBg()
BrightRedBg()
BrightGreenBg()
BrightYellowBg()
BrightBlueBg()
BrightMagentaBg()
BrightCyanBg()
BrightWhiteBg()

Text decoration functions

BoldStyle()
FaintStyle()
ItalicStyle()
UnderlineStyle()
SlowBlinkStyle()
RapidBlinkStyle()
InvertStyle()
HideStyle()
StrikeStyle()
FramedStyle() // Not widely supported
EncircledStyle()  // Not widely supported

License

This project is distributed under CC 4.0 License.