Add name highlighting

The server adds name highlighting to the message to be the same color as the user's tag.
resolves #48
This commit is contained in:
joeyak 2019-03-21 09:04:57 -04:00
parent 355f09bd48
commit b2d62ad42b
1 changed files with 19 additions and 0 deletions

View File

@ -3,6 +3,7 @@ package main
import (
"fmt"
"html"
"regexp"
"strings"
"unicode"
@ -86,6 +87,11 @@ func (cl *Client) NewMsg(data common.ClientData) {
}
func (cl *Client) SendChatData(data common.ChatData) error {
// Colorize name on chat messages
if data.Type == common.DTChat {
data = replaceColorizedName(data, cl)
}
cd, err := data.ToJSON()
if err != nil {
return fmt.Errorf("could not create ChatDataJSON of type %d: %v", data.Type, err)
@ -173,3 +179,16 @@ func removeDumbSpaces(msg string) string {
}
return newMsg
}
func replaceColorizedName(chatData common.ChatData, client *Client) common.ChatData {
data := chatData.Data.(common.DataMessage)
data.Message = regexp.MustCompile(fmt.Sprintf(`(%s|@%s)`, client.name, client.name)).
ReplaceAllString(
data.Message,
fmt.Sprintf(`<span style="color: %s">$1</span>`, client.color),
)
chatData.Data = data
return chatData
}