diff options
Diffstat (limited to 'ui/widget/color.go')
-rw-r--r-- | ui/widget/color.go | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/ui/widget/color.go b/ui/widget/color.go index 874b93d..57f943f 100644 --- a/ui/widget/color.go +++ b/ui/widget/color.go @@ -26,6 +26,7 @@ import ( var colorNames []string +// init initializes the colorNames array. func init() { colorNames = make([]string, len(tcell.ColorNames)) i := 0 @@ -33,9 +34,22 @@ func init() { colorNames[i] = name i++ } + // In order to have consistent coloring between restarts, we need to sort the array. sort.Sort(sort.StringSlice(colorNames)) } +// GetHashColorName gets a color name for the given string based on its FNV-1 hash. +// +// The array of possible color names are the alphabetically ordered color +// names specified in tcell.ColorNames. +// +// The algorithm to get the color is as follows: +// colorNames[ FNV1(string) % len(colorNames) ] +// +// With the exception of the three special cases: +// --> = green +// <-- = red +// --- = yellow func GetHashColorName(s string) string { switch s { case "-->": @@ -51,10 +65,15 @@ func GetHashColorName(s string) string { } } +// GetHashColor gets the tcell Color value for the given string. +// +// GetHashColor calls GetHashColorName() and gets the Color value from the tcell.ColorNames map. func GetHashColor(s string) tcell.Color { return tcell.ColorNames[GetHashColorName(s)] } +// AddHashColor adds tview color tags to the given string. +// The color added is the color returned by GetHashColorName(). func AddHashColor(s string) string { return fmt.Sprintf("[%s]%s[white]", GetHashColorName(s), s) } |