69 lines
1.2 KiB
Go
69 lines
1.2 KiB
Go
package color
|
|
|
|
// ANSI color codes
|
|
const (
|
|
Reset = "\033[0m"
|
|
Red = "\033[31m"
|
|
Green = "\033[32m"
|
|
Yellow = "\033[33m"
|
|
Blue = "\033[34m"
|
|
Purple = "\033[35m"
|
|
Cyan = "\033[36m"
|
|
White = "\033[37m"
|
|
Gray = "\033[90m"
|
|
)
|
|
|
|
var useColors = true
|
|
|
|
// SetColors enables or disables colors globally
|
|
func SetColors(enabled bool) {
|
|
useColors = enabled
|
|
}
|
|
|
|
// ColorsEnabled returns current global color setting
|
|
func ColorsEnabled() bool {
|
|
return useColors
|
|
}
|
|
|
|
// Apply adds color to text using global color setting
|
|
func Apply(text, color string) string {
|
|
if useColors {
|
|
return color + text + Reset
|
|
}
|
|
return text
|
|
}
|
|
|
|
// ApplyIf adds color to text if useColors is true (for backward compatibility)
|
|
func ApplyIf(text, color string, enabled bool) string {
|
|
if enabled {
|
|
return color + text + Reset
|
|
}
|
|
return text
|
|
}
|
|
|
|
// Set adds color to text (always applies color, ignores global setting)
|
|
func Set(text, color string) string {
|
|
return color + text + Reset
|
|
}
|
|
|
|
// Strip removes ANSI color codes from a string
|
|
func Strip(s string) string {
|
|
result := ""
|
|
inEscape := false
|
|
|
|
for _, c := range s {
|
|
if inEscape {
|
|
if c == 'm' {
|
|
inEscape = false
|
|
}
|
|
continue
|
|
}
|
|
if c == '\033' {
|
|
inEscape = true
|
|
continue
|
|
}
|
|
result += string(c)
|
|
}
|
|
return result
|
|
}
|