mirror of
https://github.com/go-task/task.git
synced 2025-11-23 22:24:45 +02:00
feat: support 8-bit color (#1576)
This commit is contained in:
@@ -25,43 +25,62 @@ type (
|
||||
)
|
||||
|
||||
func Default() PrintFunc {
|
||||
return color.New(envColor("TASK_COLOR_RESET", color.Reset)).FprintfFunc()
|
||||
return color.New(envColor("TASK_COLOR_RESET", color.Reset)...).FprintfFunc()
|
||||
}
|
||||
|
||||
func Blue() PrintFunc {
|
||||
return color.New(envColor("TASK_COLOR_BLUE", color.FgBlue)).FprintfFunc()
|
||||
return color.New(envColor("TASK_COLOR_BLUE", color.FgBlue)...).FprintfFunc()
|
||||
}
|
||||
|
||||
func Green() PrintFunc {
|
||||
return color.New(envColor("TASK_COLOR_GREEN", color.FgGreen)).FprintfFunc()
|
||||
return color.New(envColor("TASK_COLOR_GREEN", color.FgGreen)...).FprintfFunc()
|
||||
}
|
||||
|
||||
func Cyan() PrintFunc {
|
||||
return color.New(envColor("TASK_COLOR_CYAN", color.FgCyan)).FprintfFunc()
|
||||
return color.New(envColor("TASK_COLOR_CYAN", color.FgCyan)...).FprintfFunc()
|
||||
}
|
||||
|
||||
func Yellow() PrintFunc {
|
||||
return color.New(envColor("TASK_COLOR_YELLOW", color.FgYellow)).FprintfFunc()
|
||||
return color.New(envColor("TASK_COLOR_YELLOW", color.FgYellow)...).FprintfFunc()
|
||||
}
|
||||
|
||||
func Magenta() PrintFunc {
|
||||
return color.New(envColor("TASK_COLOR_MAGENTA", color.FgMagenta)).FprintfFunc()
|
||||
return color.New(envColor("TASK_COLOR_MAGENTA", color.FgMagenta)...).FprintfFunc()
|
||||
}
|
||||
|
||||
func Red() PrintFunc {
|
||||
return color.New(envColor("TASK_COLOR_RED", color.FgRed)).FprintfFunc()
|
||||
return color.New(envColor("TASK_COLOR_RED", color.FgRed)...).FprintfFunc()
|
||||
}
|
||||
|
||||
func envColor(env string, defaultColor color.Attribute) color.Attribute {
|
||||
func envColor(env string, defaultColor color.Attribute) []color.Attribute {
|
||||
if os.Getenv("FORCE_COLOR") != "" {
|
||||
color.NoColor = false
|
||||
}
|
||||
|
||||
override, err := strconv.Atoi(os.Getenv(env))
|
||||
if err == nil {
|
||||
return color.Attribute(override)
|
||||
// Fetch the environment variable
|
||||
override := os.Getenv(env)
|
||||
|
||||
// First, try splitting the string by commas (RGB shortcut syntax) and if it
|
||||
// matches, then prepend the 256-color foreground escape sequence.
|
||||
// Otherwise, split by semicolons (ANSI color codes) and use them as is.
|
||||
attributeStrs := strings.Split(override, ",")
|
||||
if len(attributeStrs) == 3 {
|
||||
attributeStrs = append([]string{"38", "2"}, attributeStrs...)
|
||||
} else {
|
||||
attributeStrs = strings.Split(override, ";")
|
||||
}
|
||||
return defaultColor
|
||||
|
||||
// Loop over the attributes and convert them to integers
|
||||
attributes := make([]color.Attribute, len(attributeStrs))
|
||||
for i, attributeStr := range attributeStrs {
|
||||
attribute, err := strconv.Atoi(attributeStr)
|
||||
if err != nil {
|
||||
return []color.Attribute{defaultColor}
|
||||
}
|
||||
attributes[i] = color.Attribute(attribute)
|
||||
}
|
||||
|
||||
return attributes
|
||||
}
|
||||
|
||||
// Logger is just a wrapper that prints stuff to STDOUT or STDERR,
|
||||
|
||||
Reference in New Issue
Block a user