diff options
author | Tulir Asokan <tulir@maunium.net> | 2018-03-22 23:03:36 +0200 |
---|---|---|
committer | Tulir Asokan <tulir@maunium.net> | 2018-03-22 23:03:36 +0200 |
commit | b93dea2e1c18752b89b09790b390e54c33dda1bf (patch) | |
tree | c004fdf58d89b3a832db5df4887f716bf364d3f9 /ui/widget/color.go | |
parent | 225dbdba4e1fed5814c0a3d1f34a8aedf87fc445 (diff) |
Refactoring and documentation
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) } |