README
¶
🦜 colors
One more Go library for using colors in the terminal console. The most important features are:
- ANSI colors support (using Escape Sequences)
- Multi-thread safe
- Support
FORCE_COLOR
,NO_COLOR
andTERM
variables out of the box - Super-lightweight and extremely fast
- Color codes are not pre-allocated, but cached (in memory) and re-used upon further usage
- Easy to integrate with the existing code-base
Usage examples
package main
import (
"fmt"
"gh.tarampamp.am/colors"
)
func main() {
fmt.Println((colors.FgGreen | colors.Bold).Wrap("green color + bold text"))
var bg = colors.BgRed
fmt.Printf("%s red background %s\n", bg.Start(), bg.Reset())
colors.Enabled(false) // disable colors
colors.Enabled(true) // enable colors
}
For more examples see examples directory.
Documentation
¶
Index ¶
- func Enabled(newState ...bool) bool
- type TextStyle
- func (ts *TextStyle) Add(styles ...TextStyle)
- func (ts TextStyle) ColorCodes() (start, reset string)
- func (ts TextStyle) Has(z TextStyle) bool
- func (ts *TextStyle) Remove(styles ...TextStyle)
- func (ts TextStyle) Reset() (reset string)
- func (ts TextStyle) Start() (start string)
- func (ts TextStyle) String() string
- func (ts TextStyle) Wrap(s string) string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type TextStyle ¶
type TextStyle uint32
TextStyle is a set of text styles.
Developer note:
uint32 = 0b11111111111111111111111111111111 ^^^^^^^^^ - foreground color ^ - bright foreground color bit ^^^^^^^^^ - background color ^ - bright background color bit ^^^^^^^^ - text style ^ - reset style bit ^^^ - reserved bits
Pretty doc: <https://gist.github.com/fnky/458719343aabd01cfb17a3a4f7296797>
const ( FgBlack TextStyle = 1 << iota // Black text color FgRed // Red text color FgGreen // Green text color FgYellow // Yellow text color FgBlue // Blue text color FgMagenta // Magenta text color FgCyan // Cyan text color FgWhite // White text color FgDefault // Default text color FgBright // Bright text color, usage example: (FgRed | FgBright).Wrap("hello world") BgBlack // Black background color BgRed // Red background color BgGreen // Green background color BgYellow // Yellow background color BgBlue // Blue background color BgMagenta // Magenta background color BgCyan // Cyan background color BgWhite // White background color BgDefault // Default background color BgBright // Bright background color, usage example: (BgRed | BgBright).Wrap("hello world") Bold // Bold text Faint // Faint text Italic // Italic text Underline // Underline text Blinking // Blinking text Reverse // Reverse text Invisible // Invisible text Strike // Strike text Reset // Reset text style )
func (TextStyle) ColorCodes ¶
ColorCodes returns color codes for the text style. Important note: the result of this function working does not depend on the colors enabling state.
func (TextStyle) Reset ¶
Reset returns current text style resetting code. An empty string will return when colors are disabled or when called on FgDefault, BgDefault, or Reset.
func (TextStyle) Start ¶
Start returns current text style starting code. An empty string will return when colors are disabled.