From ee8fc613eec708033854be22351185124b235be5 Mon Sep 17 00:00:00 2001 From: erroneousboat Date: Sat, 11 Aug 2018 13:27:19 +0200 Subject: [PATCH] Add individual name colors Fixes #79 --- components/chat.go | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/components/chat.go b/components/chat.go index d1a8a86..9c49d21 100644 --- a/components/chat.go +++ b/components/chat.go @@ -11,6 +11,19 @@ import ( "github.com/erroneousboat/slack-term/config" ) +var ( + COLORS = []string{ + "fg-black", + "fg-red", + "fg-green", + "fg-yellow", + "fg-blue", + "fg-magenta", + "fg-cyan", + "fg-white", + } +) + type Message struct { Time time.Time Name string @@ -23,6 +36,21 @@ type Message struct { FormatTime string } +func (m Message) colorizeName(styleName string) string { + if strings.Contains(styleName, "colorize") { + var sum int + for _, c := range m.Name { + sum = sum + int(c) + } + + i := sum % len(COLORS) + + return strings.Replace(m.StyleName, "colorize", COLORS[i], -1) + } + + return styleName +} + // Chat is the definition of a Chat component type Chat struct { List *termui.List @@ -66,7 +94,10 @@ func (c *Chat) Buffer() termui.Buffer { // Name cells = append(cells, termui.DefaultTxBuilder.Build( - fmt.Sprintf("[<%s>](%s) ", msg.Name, msg.StyleName), + fmt.Sprintf("[<%s>](%s) ", + msg.Name, + msg.colorizeName(msg.StyleName), + ), termui.ColorDefault, termui.ColorDefault)..., ) }