From b93dea2e1c18752b89b09790b390e54c33dda1bf Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Thu, 22 Mar 2018 23:03:36 +0200 Subject: Refactoring and documentation --- ui/widget/color.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) (limited to 'ui/widget/color.go') 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) } -- cgit v1.2.3