From 50d1ac83b67854fde4737783c212a1cc2c91b307 Mon Sep 17 00:00:00 2001 From: Timon Ringwald Date: Sun, 22 May 2022 22:39:41 +0200 Subject: [PATCH] color attribute support --- color.go | 75 ++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 62 insertions(+), 13 deletions(-) diff --git a/color.go b/color.go index da41997..a1e6b63 100644 --- a/color.go +++ b/color.go @@ -1,36 +1,85 @@ package main -import "github.com/fatih/color" +import ( + "fmt" + "strings" + + "github.com/fatih/color" +) var colorCache = map[string]*color.Color{} -func makeColor(name string) (c *color.Color) { +func makeColor(colorSpec string) (c *color.Color) { // caching - if c, ok := colorCache[name]; ok { + if c, ok := colorCache[colorSpec]; ok { return c } - defer func() { colorCache[name] = c }() + defer func() { colorCache[colorSpec] = c }() + + splits := strings.Split(colorSpec, ",") + name := strings.TrimSpace(splits[0]) + + c = color.New() switch name { case "black": - return color.New(color.FgBlack) + c.Add(color.FgBlack) case "red": - return color.New(color.FgRed) + c.Add(color.FgRed) case "green": - return color.New(color.FgGreen) + c.Add(color.FgGreen) case "yellow": - return color.New(color.FgYellow) + c.Add(color.FgYellow) case "blue": - return color.New(color.FgBlue) + c.Add(color.FgBlue) case "magenta": - return color.New(color.FgMagenta) + c.Add(color.FgMagenta) case "cyan": - return color.New(color.FgCyan) + c.Add(color.FgCyan) case "white": - return color.New(color.FgWhite) + c.Add(color.FgWhite) case "": - return color.New() default: panic("unknown color name. valid color names: black, red, green, yellow, blue, magenta, cyan, white") } + + if len(splits) >= 2 { + attr := splits[1] + + switch attr { + case "bold": + fmt.Println("bold") + c.Add(color.Bold) + case "faint": + fmt.Println("faint") + c.Add(color.Faint) + case "italic": + fmt.Println("italic") + c.Add(color.Italic) + case "underline": + fmt.Println("underline") + c.Add(color.Underline) + case "blink_slow": + fmt.Println("blink_slow") + c.Add(color.BlinkSlow) + case "blink_rapid": + fmt.Println("blink_rapid") + c.Add(color.BlinkRapid) + case "reverse_video": + fmt.Println("reverse_video") + c.Add(color.ReverseVideo) + case "concealed": + fmt.Println("concealed") + c.Add(color.Concealed) + case "crossed_out": + fmt.Println("crossed_out") + c.Add(color.CrossedOut) + case "": + fmt.Println("") + default: + panic("unknown attribute. valid attributes: bold, faint, italic, underline, blink_slow, blink_rapid, reverse_video, concealed, crossed_out") + } + } + + return c }