From 3b2d5fa0349bf01217f6954813c669680cdd16d4 Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Wed, 18 Apr 2018 17:33:59 +0300 Subject: [PATCH] Fix nick coloring --- ui/messages/parser/htmlparser.go | 10 ++++++---- ui/messages/parser/parser.go | 33 +++++++++++++++++++------------- ui/room-view.go | 4 ++-- ui/widget/color.go | 9 +++++---- 4 files changed, 33 insertions(+), 23 deletions(-) diff --git a/ui/messages/parser/htmlparser.go b/ui/messages/parser/htmlparser.go index 5e3b289..8577ee7 100644 --- a/ui/messages/parser/htmlparser.go +++ b/ui/messages/parser/htmlparser.go @@ -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: "", diff --git a/ui/messages/parser/parser.go b/ui/messages/parser/parser.go index d12383a..fbb2bf5 100644 --- a/ui/messages/parser/parser.go +++ b/ui/messages/parser/parser.go @@ -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) } diff --git a/ui/room-view.go b/ui/room-view.go index d38db94..d1e5a16 100644 --- a/ui/room-view.go +++ b/ui/room-view.go @@ -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') } } diff --git a/ui/widget/color.go b/ui/widget/color.go index c4f1abf..c46377a 100644 --- a/ui/widget/color.go +++ b/ui/widget/color.go @@ -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) }