Prevent unnecessarily caching usercolors if randomcolors are disabled or there are not more than one color
MODIFY
discordutil/users.go
discordutil/users.go
@@ -18,6 +18,7 @@ var (
userColorCache = make(map[string]string)
lastRandomNumber = -1
+ randColorLength = len(config.GetTheme().RandomUserColors)
)
// GetUserColor gets the users color for this session. If no color can be found
@@ -27,6 +28,13 @@ func GetUserColor(user *discordgo.User) string {
return tviewutil.ColorToHex(config.GetTheme().BotColor)
}
+ //Avoid unnecessarily retrieving and caching colors
+ if !config.Current.UseRandomUserColors || randColorLength == 0 {
+ return tviewutil.ColorToHex(config.GetTheme().DefaultUserColor)
+ } else if randColorLength == 1 {
+ return tviewutil.ColorToHex(config.GetTheme().RandomUserColors[0])
+ }
+
color, ok := userColorCache[user.ID]
if ok {
return color
@@ -38,13 +46,6 @@ func GetUserColor(user *discordgo.User) string {
}
func getRandomColorString() string {
- randColorLength := len(config.GetTheme().RandomUserColors)
- if randColorLength == 0 {
- return tviewutil.ColorToHex(config.GetTheme().DefaultUserColor)
- } else if randColorLength == 1 {
- return tviewutil.ColorToHex(config.GetTheme().RandomUserColors[0])
- }
-
randIndex := rand.Intn(randColorLength)
if randIndex == lastRandomNumber {
return getRandomColorString()

MODIFY
discordutil/users_test.go
discordutil/users_test.go
@@ -6,11 +6,13 @@ import (
"github.com/Bios-Marcel/cordless/ui/tviewutil"
- "github.com/Bios-Marcel/cordless/config"
"github.com/Bios-Marcel/discordgo"
+
+ "github.com/Bios-Marcel/cordless/config"
)
func TestGetUserColor(t *testing.T) {
+ config.Current.UseRandomUserColors = true
tests := []struct {
name string
user *discordgo.User

MODIFY
ui/chatview.go
ui/chatview.go
@@ -437,13 +437,9 @@ func (chatView *ChatView) formatMessageAuthor(message *discordgo.Message) string
messageAuthor = discordutil.GetUserName(message.Author)
}
- if config.Current.UseRandomUserColors {
return "[" + discordutil.GetUserColor(message.Author) + "]" + messageAuthor
}
- return "[" + tviewutil.ColorToHex(config.GetTheme().DefaultUserColor) + "]" + messageAuthor
-}
-
func (chatView *ChatView) formatMessageText(message *discordgo.Message) string {
if message.Type == discordgo.MessageTypeDefault {
return chatView.formatDefaultMessageText(message)

Prevent unnecessarily caching usercolors if randomcolors are disabled or there are not more than one color