1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
|
// This file is available under the Apache 2.0 License
// This file was copied from: https://github.com/briandowns/spinner
//
// Please see the LICENSE file for the copy of the Apache 2.0 License.
//
// Modifications:
//
// - made validColors set map more idiomatic with an empty struct value
// - added a function for creating color functions from color list
package yacspin
import (
"fmt"
"github.com/fatih/color"
)
// ValidColors holds the list of the strings that are mapped to
// github.com/fatih/color color attributes. Any of these colors / attributes can
// be used with the *Spinner type, and it should be reflected in the output.
var ValidColors = map[string]struct{}{
// default colors for backwards compatibility
"black": {},
"red": {},
"green": {},
"yellow": {},
"blue": {},
"magenta": {},
"cyan": {},
"white": {},
// attributes
"reset": {},
"bold": {},
"faint": {},
"italic": {},
"underline": {},
"blinkslow": {},
"blinkrapid": {},
"reversevideo": {},
"concealed": {},
"crossedout": {},
// foreground text
"fgBlack": {},
"fgRed": {},
"fgGreen": {},
"fgYellow": {},
"fgBlue": {},
"fgMagenta": {},
"fgCyan": {},
"fgWhite": {},
// foreground Hi-Intensity text
"fgHiBlack": {},
"fgHiRed": {},
"fgHiGreen": {},
"fgHiYellow": {},
"fgHiBlue": {},
"fgHiMagenta": {},
"fgHiCyan": {},
"fgHiWhite": {},
// background text
"bgBlack": {},
"bgRed": {},
"bgGreen": {},
"bgYellow": {},
"bgBlue": {},
"bgMagenta": {},
"bgCyan": {},
"bgWhite": {},
// background Hi-Intensity text
"bgHiBlack": {},
"bgHiRed": {},
"bgHiGreen": {},
"bgHiYellow": {},
"bgHiBlue": {},
"bgHiMagenta": {},
"bgHiCyan": {},
"bgHiWhite": {},
}
// returns a valid color's foreground text color attribute
var colorAttributeMap = map[string]color.Attribute{
// default colors for backwards compatibility
"black": color.FgBlack,
"red": color.FgRed,
"green": color.FgGreen,
"yellow": color.FgYellow,
"blue": color.FgBlue,
"magenta": color.FgMagenta,
"cyan": color.FgCyan,
"white": color.FgWhite,
// attributes
"reset": color.Reset,
"bold": color.Bold,
"faint": color.Faint,
"italic": color.Italic,
"underline": color.Underline,
"blinkslow": color.BlinkSlow,
"blinkrapid": color.BlinkRapid,
"reversevideo": color.ReverseVideo,
"concealed": color.Concealed,
"crossedout": color.CrossedOut,
// foreground text colors
"fgBlack": color.FgBlack,
"fgRed": color.FgRed,
"fgGreen": color.FgGreen,
"fgYellow": color.FgYellow,
"fgBlue": color.FgBlue,
"fgMagenta": color.FgMagenta,
"fgCyan": color.FgCyan,
"fgWhite": color.FgWhite,
// foreground Hi-Intensity text colors
"fgHiBlack": color.FgHiBlack,
"fgHiRed": color.FgHiRed,
"fgHiGreen": color.FgHiGreen,
"fgHiYellow": color.FgHiYellow,
"fgHiBlue": color.FgHiBlue,
"fgHiMagenta": color.FgHiMagenta,
"fgHiCyan": color.FgHiCyan,
"fgHiWhite": color.FgHiWhite,
// background text colors
"bgBlack": color.BgBlack,
"bgRed": color.BgRed,
"bgGreen": color.BgGreen,
"bgYellow": color.BgYellow,
"bgBlue": color.BgBlue,
"bgMagenta": color.BgMagenta,
"bgCyan": color.BgCyan,
"bgWhite": color.BgWhite,
// background Hi-Intensity text colors
"bgHiBlack": color.BgHiBlack,
"bgHiRed": color.BgHiRed,
"bgHiGreen": color.BgHiGreen,
"bgHiYellow": color.BgHiYellow,
"bgHiBlue": color.BgHiBlue,
"bgHiMagenta": color.BgHiMagenta,
"bgHiCyan": color.BgHiCyan,
"bgHiWhite": color.BgHiWhite,
}
// validColor will make sure the given color is actually allowed
func validColor(c string) bool {
_, ok := ValidColors[c]
return ok
}
func colorFunc(colors ...string) (func(format string, a ...interface{}) string, error) {
if len(colors) == 0 {
return fmt.Sprintf, nil
}
attrib := make([]color.Attribute, len(colors))
for i, color := range colors {
if !validColor(color) {
return nil, fmt.Errorf("%s is not a valid color", color)
}
attrib[i] = colorAttributeMap[color]
}
return color.New(attrib...).SprintfFunc(), nil
}
|