1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- package text
- import "fmt"
- const (
- redCode = "\x1b[31m"
- greenCode = "\x1b[32m"
- yellowCode = "\x1b[33m"
- magentaCode = "\x1b[35m"
- CyanCode = "\x1b[36m"
- boldCode = "\x1b[1m"
- ResetCode = "\x1b[0m"
- )
- // UseColor determines if package will emit colors
- var UseColor = true
- func stylize(startCode, in string) string {
- if UseColor {
- return startCode + in + ResetCode
- }
- return in
- }
- func red(in string) string {
- return stylize(redCode, in)
- }
- func green(in string) string {
- return stylize(greenCode, in)
- }
- func yellow(in string) string {
- return stylize(yellowCode, in)
- }
- func cyan(in string) string {
- return stylize(CyanCode, in)
- }
- func Magenta(in string) string {
- return stylize(magentaCode, in)
- }
- func Bold(in string) string {
- return stylize(boldCode, in)
- }
- // ColorHash Colors text using a hashing algorithm. The same text will always produce the
- // same color while different text will produce a different color.
- func ColorHash(name string) (output string) {
- if !UseColor {
- return name
- }
- var hash uint = 5381
- for i := 0; i < len(name); i++ {
- hash = uint(name[i]) + ((hash << 5) + (hash))
- }
- return fmt.Sprintf("\x1b[%dm%s\x1b[0m", hash%6+31, name)
- }
|