colors.go
package main import ( "fmt" "strconv" "strings") // ColorStub is the text string stub which the shell// can interpret as an ANSI colorconst ColorStub = "\033[" // Colorer is the convenience function to color thingsvar Colorer = Colors{} // Palette stores the color code to int mapvar Palette = map[string]int{ "default": 0, "bold": 1, "dim": 2, "italics": 3, "underline": 4, "black": 30, "white": 97, "gray": 90, "grey": 90, "red": 31, "yellow": 33, "green": 32, "cyan": 36, "blue": 34, "violet": 35, "bgwhite": 47, "bgblack": 40, "bgred": 41, "bgyellow": 43, "bggreen": 42, "bgcyan": 46, "bgblue": 44, "bgviolet": 45, "lgray": 37, "lgrey": 37, "lred": 91, "lyellow": 93, "lgreen": 92, "lcyan": 96, "lblue": 94, "lviolet": 95,} // Color applies the color code :color to the string :valuefunc Color(color string, value string) string { format := ColorStub + strconv.Itoa(Palette[color]) + "m" unformat := ColorStub + "0m" finalValue := value if strings.Contains(value, unformat) { finalValue = strings.Replace(value, unformat, unformat+format, -1) } return fmt.Sprintf("%s%s%s%s", ColorStub, format, finalValue, unformat)} // Colors defines a struct for the Colorer`Colors` has 23 methods (exceeds 20 allowed). Consider refactoring.type Colors struct{} // Default sets the string :value to the default colorfunc (*Colors) Default(value string) string { return Color("default", value)} // Bold sets the string :value to boldfunc (*Colors) Bold(value string) string { return Color("bold", value)} // Dim dims the string :valuefunc (*Colors) Dim(value string) string { return Color("dim", value)} // Italics italicises the string :valuefunc (*Colors) Italics(value string) string { return Color("italics", value)} // Underline underlines the string :valuefunc (*Colors) Underline(value string) string { return Color("underline", value)} // Black sets the string :value to blackfunc (*Colors) Black(value string) string { return Color("black", value)} // Gray sets the string :value to grayfunc (*Colors) Gray(value string) string { return Color("gray", value)} // Grey sets the string :value to greyfunc (*Colors) Grey(value string) string { return Color("grey", value)} // Red sets the string :value to redfunc (*Colors) Red(value string) string { return Color("red", value)} // LightRed sets the string :value to light redfunc (*Colors) LightRed(value string) string { return Color("lred", value)} // Green sets the string :value to greenfunc (*Colors) Green(value string) string { return Color("green", value)} // LightGreen sets the string :value to light greenfunc (*Colors) LightGreen(value string) string { return Color("lgreen", value)} // Yellow sets the string :value to yellowfunc (*Colors) Yellow(value string) string { return Color("yellow", value)} // LightYellow sets the string :value to light yellowfunc (*Colors) LightYellow(value string) string { return Color("lyellow", value)} // Blue sets the string :value to bluefunc (*Colors) Blue(value string) string { return Color("blue", value)} // LightBlue sets the string :value to light bluefunc (*Colors) LightBlue(value string) string { return Color("lblue", value)} // Violet sets the string :value to violetfunc (*Colors) Violet(value string) string { return Color("violet", value)} // LightViolet sets the string :value to light violetfunc (*Colors) LightViolet(value string) string { return Color("lviolet", value)} // Cyan sets the string :value to cyanfunc (*Colors) Cyan(value string) string { return Color("cyan", value)} // LightCyan sets the string :value to light cyanfunc (*Colors) LightCyan(value string) string { return Color("lcyan", value)} // LightGray sets the string :value to light grayfunc (*Colors) LightGray(value string) string { return Color("lgray", value)} // LightGrey sets the string :value to light greyfunc (*Colors) LightGrey(value string) string { return Color("lgrey", value)} // White sets the string :value to light whitefunc (*Colors) White(value string) string { return Color("white", value)}