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)..., ) }