aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTulir Asokan <tulir@maunium.net>2018-04-18 17:33:59 +0300
committerTulir Asokan <tulir@maunium.net>2018-04-18 17:33:59 +0300
commit3b2d5fa0349bf01217f6954813c669680cdd16d4 (patch)
tree11e9c8a6e22bd37f5203a7f9debf8d609bbc1e7a
parent670d369c92bc3c450071c1a10ec5d034a53258ea (diff)
Fix nick coloring
-rw-r--r--ui/messages/parser/htmlparser.go10
-rw-r--r--ui/messages/parser/parser.go33
-rw-r--r--ui/room-view.go4
-rw-r--r--ui/widget/color.go9
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)
}