format/color.go

37 lines
797 B
Go
Raw Normal View History

2022-05-22 21:06:06 +02:00
package main
import "github.com/fatih/color"
var colorCache = map[string]*color.Color{}
func makeColor(name string) (c *color.Color) {
// caching
if c, ok := colorCache[name]; ok {
return c
}
defer func() { colorCache[name] = c }()
switch name {
case "black":
return color.New(color.FgBlack)
case "red":
return color.New(color.FgRed)
case "green":
return color.New(color.FgGreen)
case "yellow":
return color.New(color.FgYellow)
case "blue":
return color.New(color.FgBlue)
case "magenta":
return color.New(color.FgMagenta)
case "cyan":
return color.New(color.FgCyan)
case "white":
return color.New(color.FgWhite)
case "":
return color.New()
default:
panic("unknown color name. valid color names: black, red, green, yellow, blue, magenta, cyan, white")
}
}