Fix nick coloring

This commit is contained in:
Tulir Asokan 2018-04-18 17:33:59 +03:00
parent 670d369c92
commit 3b2d5fa034
4 changed files with 33 additions and 23 deletions

View File

@ -37,8 +37,9 @@ var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([
type MatrixHTMLProcessor struct {
text tstring.TString
sender string
msgtype string
senderID string
sender string
msgtype string
indent string
listType string
@ -57,7 +58,7 @@ func (parser *MatrixHTMLProcessor) newline() {
func (parser *MatrixHTMLProcessor) Preprocess() {
if parser.msgtype == "m.emote" {
parser.text = tstring.NewColorTString(fmt.Sprintf("* %s ", parser.sender), widget.GetHashColor(parser.sender))
parser.text = tstring.NewColorTString(fmt.Sprintf("* %s ", parser.sender), widget.GetHashColor(parser.senderID))
}
}
@ -147,7 +148,7 @@ func (parser *MatrixHTMLProcessor) HandleEndTag(tagName string) {
pillTarget := match[1]
if pillTarget[0] == '@' {
if member := parser.room.GetMember(pillTarget); member != nil {
parser.text = parser.text.AppendColor(member.DisplayName, widget.GetHashColor(member.DisplayName))
parser.text = parser.text.AppendColor(member.DisplayName, widget.GetHashColor(member.UserID))
} else {
parser.text = parser.text.Append(pillTarget)
}
@ -185,6 +186,7 @@ func ParseHTMLMessage(room *rooms.Room, evt *gomatrix.Event, senderDisplayname s
room: room,
text: tstring.NewBlankTString(),
msgtype: msgtype,
senderID: evt.Sender,
sender: senderDisplayname,
indent: "",
listType: "",

View File

@ -79,16 +79,19 @@ func ParseMessage(gmx ifc.Gomuks, room *rooms.Room, evt *gomatrix.Event) message
func getMembershipEventContent(room *rooms.Room, evt *gomatrix.Event) (sender string, text tstring.TString) {
member := room.GetMember(evt.Sender)
senderDisplayname := evt.Sender
if member != nil {
evt.Sender = member.DisplayName
senderDisplayname = member.DisplayName
}
membership, _ := evt.Content["membership"].(string)
displayname, _ := evt.Content["displayname"].(string)
if len(displayname) == 0 {
displayname = *evt.StateKey
}
prevMembership := "leave"
prevDisplayname := ""
prevDisplayname := *evt.StateKey
if evt.Unsigned.PrevContent != nil {
prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string)
prevDisplayname, _ = evt.Unsigned.PrevContent["displayname"].(string)
@ -98,36 +101,40 @@ func getMembershipEventContent(room *rooms.Room, evt *gomatrix.Event) (sender st
switch membership {
case "invite":
sender = "---"
text = tstring.NewColorTString(fmt.Sprintf("%s invited %s.", evt.Sender, displayname), tcell.ColorGreen)
text.Colorize(0, len(evt.Sender), widget.GetHashColor(evt.Sender))
text.Colorize(len(evt.Sender)+len(" invited "), len(displayname), widget.GetHashColor(displayname))
text = tstring.NewColorTString(fmt.Sprintf("%s invited %s.", senderDisplayname, displayname), tcell.ColorGreen)
text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender))
text.Colorize(len(senderDisplayname)+len(" invited "), len(displayname), widget.GetHashColor(*evt.StateKey))
case "join":
sender = "-->"
text = tstring.NewColorTString(fmt.Sprintf("%s joined the room.", displayname), tcell.ColorGreen)
text.Colorize(0, len(displayname), widget.GetHashColor(displayname))
text.Colorize(0, len(displayname), widget.GetHashColor(*evt.StateKey))
case "leave":
sender = "<--"
if evt.Sender != *evt.StateKey {
reason, _ := evt.Content["reason"].(string)
text = tstring.NewColorTString(fmt.Sprintf("%s kicked %s: %s", evt.Sender, displayname, reason), tcell.ColorRed)
text.Colorize(0, len(evt.Sender), widget.GetHashColor(evt.Sender))
text.Colorize(len(evt.Sender)+len(" kicked "), len(displayname), widget.GetHashColor(displayname))
text = tstring.NewColorTString(fmt.Sprintf("%s kicked %s: %s", senderDisplayname, displayname, reason), tcell.ColorRed)
text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender))
text.Colorize(len(senderDisplayname)+len(" kicked "), len(displayname), widget.GetHashColor(*evt.StateKey))
} else {
text = tstring.NewColorTString(fmt.Sprintf("%s left the room.", displayname), tcell.ColorRed)
text.Colorize(0, len(displayname), widget.GetHashColor(displayname))
text.Colorize(0, len(displayname), widget.GetHashColor(*evt.StateKey))
}
}
} else if displayname != prevDisplayname {
sender = "---"
text = tstring.NewColorTString(fmt.Sprintf("%s changed their display name to %s.", prevDisplayname, displayname), tcell.ColorYellow)
text.Colorize(0, len(prevDisplayname), widget.GetHashColor(prevDisplayname))
text.Colorize(len(prevDisplayname)+len(" changed their display name to "), len(displayname), widget.GetHashColor(displayname))
text = tstring.NewColorTString(fmt.Sprintf("%s changed their display name to %s.", prevDisplayname, displayname), tcell.ColorGreen)
text.Colorize(0, len(prevDisplayname), widget.GetHashColor(*evt.StateKey))
text.Colorize(len(prevDisplayname)+len(" changed their display name to "), len(displayname), widget.GetHashColor(*evt.StateKey))
}
return
}
func ParseMembershipEvent(room *rooms.Room, evt *gomatrix.Event) messages.UIMessage {
displayname, text := getMembershipEventContent(room, evt)
if len(text) == 0 {
return nil
}
ts := unixToTime(evt.Timestamp)
return messages.NewExpandedTextMessage(evt.ID, evt.Sender, displayname, "m.room.membership", text, ts)
}

View File

@ -240,10 +240,10 @@ func (view *RoomView) UpdateUserList() {
var invited strings.Builder
for _, user := range view.Room.GetMembers() {
if user.Membership == "join" {
joined.WriteString(widget.AddHashColor(user.DisplayName))
joined.WriteString(widget.AddColor(user.DisplayName, widget.GetHashColorName(user.UserID)))
joined.WriteRune('\n')
} else if user.Membership == "invite" {
invited.WriteString(widget.AddHashColor(user.DisplayName))
invited.WriteString(widget.AddColor(user.DisplayName, widget.GetHashColorName(user.UserID)))
invited.WriteRune('\n')
}
}

View File

@ -21,6 +21,7 @@ import (
"hash/fnv"
"sort"
"maunium.net/go/gomuks/debug"
"maunium.net/go/tcell"
)
@ -51,6 +52,7 @@ func init() {
// <-- = red
// --- = yellow
func GetHashColorName(s string) string {
debug.Print("Getting color for", s)
switch s {
case "-->":
return "green"
@ -72,8 +74,7 @@ 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)
// AddColor adds tview color tags to the given string.
func AddColor(s, color string) string {
return fmt.Sprintf("[%s]%s[white]", color, s)
}