An open API service indexing awesome lists of open source software.

https://github.com/lorossi/colorize

Simple Go package to have colored and formatted text inside your terminal
https://github.com/lorossi/colorize

colors go-package golang-package released terminal

Last synced: 5 months ago
JSON representation

Simple Go package to have colored and formatted text inside your terminal

Awesome Lists containing this project

README

          

# Colorize









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

## Installation

```bash
go get github.com/lorossi/colorize

```

## Documentation

[Read the docs here!](/DOCS.md)

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](https://www.github.com/lorossi/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:

```go
colorize.SetStyle(colorize.Bold, colorize.FgBrightBlue, colorize.BgBrightYellow)
Fmt.Println("WOW")
colorize.ResetStyle()
```
Set an rgb value for the text:

```go
colorize.SetFgRGB(255, 0, 0)
Fmt.Println("Colored!")
colorize.ResetStyle()
```

Set an rgb value for the background:

```go
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):

```go
colorize.SetFgTruecolor(255, 255, 0)
colorize.SetBgTruecolor(0, 0, 255)
Fmt.Println("Everything is so colorful!")
colorize.ResetStyle()
```

Set color by HSL values:

```go
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:

```go
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:

```go
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.](/examples/main.go)**

## List of constants

### Text color constants

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

### Backgroud color constants

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

### Text decoration constants

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

## Quick functions

### Text color functions

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

### Background color functions

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

### Text decoration functions

```go
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.