2014-12-13 08:14:18 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2014-12-13 13:38:52 +01:00
|
|
|
"fmt"
|
2014-12-15 01:12:19 +01:00
|
|
|
"math/rand"
|
2014-12-15 01:30:38 +01:00
|
|
|
"regexp"
|
2014-12-13 11:01:37 +01:00
|
|
|
"strings"
|
2014-12-13 23:42:43 +01:00
|
|
|
"time"
|
2014-12-13 08:14:18 +01:00
|
|
|
)
|
|
|
|
|
2014-12-15 01:12:19 +01:00
|
|
|
const (
|
|
|
|
// Reset resets the color
|
|
|
|
Reset = "\033[0m"
|
2014-12-13 08:14:18 +01:00
|
|
|
|
2014-12-15 01:12:19 +01:00
|
|
|
// Bold makes the following text bold
|
|
|
|
Bold = "\033[1m"
|
2014-12-13 08:14:18 +01:00
|
|
|
|
2014-12-15 01:12:19 +01:00
|
|
|
// Dim dims the following text
|
|
|
|
Dim = "\033[2m"
|
|
|
|
|
|
|
|
// Italic makes the following text italic
|
|
|
|
Italic = "\033[3m"
|
|
|
|
|
|
|
|
// Underline underlines the following text
|
|
|
|
Underline = "\033[4m"
|
|
|
|
|
|
|
|
// Blink blinks the following text
|
|
|
|
Blink = "\033[5m"
|
|
|
|
|
|
|
|
// Invert inverts the following text
|
|
|
|
Invert = "\033[7m"
|
|
|
|
)
|
|
|
|
|
|
|
|
var colors = []string{"31", "32", "33", "34", "35", "36", "37", "91", "92", "93", "94", "95", "96", "97"}
|
|
|
|
|
2014-12-15 01:30:38 +01:00
|
|
|
// deColor is used for removing ANSI Escapes
|
2014-12-15 01:37:58 +01:00
|
|
|
var deColor = regexp.MustCompile("\033\\[[\\d;]+m")
|
2014-12-14 09:11:59 +01:00
|
|
|
|
2014-12-15 01:30:38 +01:00
|
|
|
// DeColorString removes all color from the given string
|
2014-12-14 09:11:59 +01:00
|
|
|
func DeColorString(s string) string {
|
2014-12-14 17:04:29 +01:00
|
|
|
s = deColor.ReplaceAllString(s, "")
|
2014-12-14 09:11:59 +01:00
|
|
|
return s
|
|
|
|
}
|
2014-12-13 08:14:18 +01:00
|
|
|
|
2014-12-15 07:51:22 +01:00
|
|
|
func randomReadableColor() int {
|
|
|
|
for {
|
|
|
|
i := rand.Intn(256)
|
|
|
|
if (16 <= i && i <= 18) || (232 <= i && i <= 237) {
|
|
|
|
// Remove the ones near black, this is kinda sadpanda.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-15 01:12:19 +01:00
|
|
|
// RandomColor256 returns a random (of 256) color
|
2014-12-13 13:38:52 +01:00
|
|
|
func RandomColor256() string {
|
2014-12-15 07:51:22 +01:00
|
|
|
return fmt.Sprintf("38;05;%d", randomReadableColor())
|
2014-12-13 13:38:52 +01:00
|
|
|
}
|
|
|
|
|
2014-12-15 01:12:19 +01:00
|
|
|
// RandomColor returns a random color
|
2014-12-13 08:14:18 +01:00
|
|
|
func RandomColor() string {
|
|
|
|
return colors[rand.Intn(len(colors))]
|
|
|
|
}
|
|
|
|
|
2014-12-15 01:12:19 +01:00
|
|
|
// ColorString returns a message in the given color
|
2014-12-13 11:01:37 +01:00
|
|
|
func ColorString(color string, msg string) string {
|
2014-12-15 01:12:19 +01:00
|
|
|
return Bold + "\033[" + color + "m" + msg + Reset
|
2014-12-13 11:01:37 +01:00
|
|
|
}
|
|
|
|
|
2014-12-15 01:12:19 +01:00
|
|
|
// RandomColorInit initializes the random seed
|
2014-12-13 23:42:43 +01:00
|
|
|
func RandomColorInit() {
|
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
}
|
|
|
|
|
2014-12-15 01:12:19 +01:00
|
|
|
// ContinuousFormat is a horrible hack to "continue" the previous string color
|
|
|
|
// and format after a RESET has been encountered.
|
|
|
|
//
|
2014-12-13 11:01:37 +01:00
|
|
|
// This is not HTML where you can just do a </style> to resume your previous formatting!
|
|
|
|
func ContinuousFormat(format string, str string) string {
|
2014-12-15 01:12:19 +01:00
|
|
|
return systemMessageFormat + strings.Replace(str, Reset, format, -1) + Reset
|
2014-12-13 08:14:18 +01:00
|
|
|
}
|