Fix nick coloring
This commit is contained in:
parent
670d369c92
commit
3b2d5fa034
@ -37,6 +37,7 @@ var matrixToURL = regexp.MustCompile("^(?:https?://)?(?:www\\.)?matrix\\.to/#/([
|
|||||||
type MatrixHTMLProcessor struct {
|
type MatrixHTMLProcessor struct {
|
||||||
text tstring.TString
|
text tstring.TString
|
||||||
|
|
||||||
|
senderID string
|
||||||
sender string
|
sender string
|
||||||
msgtype string
|
msgtype string
|
||||||
|
|
||||||
@ -57,7 +58,7 @@ func (parser *MatrixHTMLProcessor) newline() {
|
|||||||
|
|
||||||
func (parser *MatrixHTMLProcessor) Preprocess() {
|
func (parser *MatrixHTMLProcessor) Preprocess() {
|
||||||
if parser.msgtype == "m.emote" {
|
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]
|
pillTarget := match[1]
|
||||||
if pillTarget[0] == '@' {
|
if pillTarget[0] == '@' {
|
||||||
if member := parser.room.GetMember(pillTarget); member != nil {
|
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 {
|
} else {
|
||||||
parser.text = parser.text.Append(pillTarget)
|
parser.text = parser.text.Append(pillTarget)
|
||||||
}
|
}
|
||||||
@ -185,6 +186,7 @@ func ParseHTMLMessage(room *rooms.Room, evt *gomatrix.Event, senderDisplayname s
|
|||||||
room: room,
|
room: room,
|
||||||
text: tstring.NewBlankTString(),
|
text: tstring.NewBlankTString(),
|
||||||
msgtype: msgtype,
|
msgtype: msgtype,
|
||||||
|
senderID: evt.Sender,
|
||||||
sender: senderDisplayname,
|
sender: senderDisplayname,
|
||||||
indent: "",
|
indent: "",
|
||||||
listType: "",
|
listType: "",
|
||||||
|
@ -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) {
|
func getMembershipEventContent(room *rooms.Room, evt *gomatrix.Event) (sender string, text tstring.TString) {
|
||||||
member := room.GetMember(evt.Sender)
|
member := room.GetMember(evt.Sender)
|
||||||
|
senderDisplayname := evt.Sender
|
||||||
if member != nil {
|
if member != nil {
|
||||||
evt.Sender = member.DisplayName
|
senderDisplayname = member.DisplayName
|
||||||
}
|
}
|
||||||
|
|
||||||
membership, _ := evt.Content["membership"].(string)
|
membership, _ := evt.Content["membership"].(string)
|
||||||
displayname, _ := evt.Content["displayname"].(string)
|
displayname, _ := evt.Content["displayname"].(string)
|
||||||
if len(displayname) == 0 {
|
if len(displayname) == 0 {
|
||||||
displayname = *evt.StateKey
|
displayname = *evt.StateKey
|
||||||
}
|
}
|
||||||
|
|
||||||
prevMembership := "leave"
|
prevMembership := "leave"
|
||||||
prevDisplayname := ""
|
prevDisplayname := *evt.StateKey
|
||||||
if evt.Unsigned.PrevContent != nil {
|
if evt.Unsigned.PrevContent != nil {
|
||||||
prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string)
|
prevMembership, _ = evt.Unsigned.PrevContent["membership"].(string)
|
||||||
prevDisplayname, _ = evt.Unsigned.PrevContent["displayname"].(string)
|
prevDisplayname, _ = evt.Unsigned.PrevContent["displayname"].(string)
|
||||||
@ -98,36 +101,40 @@ func getMembershipEventContent(room *rooms.Room, evt *gomatrix.Event) (sender st
|
|||||||
switch membership {
|
switch membership {
|
||||||
case "invite":
|
case "invite":
|
||||||
sender = "---"
|
sender = "---"
|
||||||
text = tstring.NewColorTString(fmt.Sprintf("%s invited %s.", evt.Sender, displayname), tcell.ColorGreen)
|
text = tstring.NewColorTString(fmt.Sprintf("%s invited %s.", senderDisplayname, displayname), tcell.ColorGreen)
|
||||||
text.Colorize(0, len(evt.Sender), widget.GetHashColor(evt.Sender))
|
text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender))
|
||||||
text.Colorize(len(evt.Sender)+len(" invited "), len(displayname), widget.GetHashColor(displayname))
|
text.Colorize(len(senderDisplayname)+len(" invited "), len(displayname), widget.GetHashColor(*evt.StateKey))
|
||||||
case "join":
|
case "join":
|
||||||
sender = "-->"
|
sender = "-->"
|
||||||
text = tstring.NewColorTString(fmt.Sprintf("%s joined the room.", displayname), tcell.ColorGreen)
|
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":
|
case "leave":
|
||||||
sender = "<--"
|
sender = "<--"
|
||||||
if evt.Sender != *evt.StateKey {
|
if evt.Sender != *evt.StateKey {
|
||||||
reason, _ := evt.Content["reason"].(string)
|
reason, _ := evt.Content["reason"].(string)
|
||||||
text = tstring.NewColorTString(fmt.Sprintf("%s kicked %s: %s", evt.Sender, displayname, reason), tcell.ColorRed)
|
text = tstring.NewColorTString(fmt.Sprintf("%s kicked %s: %s", senderDisplayname, displayname, reason), tcell.ColorRed)
|
||||||
text.Colorize(0, len(evt.Sender), widget.GetHashColor(evt.Sender))
|
text.Colorize(0, len(senderDisplayname), widget.GetHashColor(evt.Sender))
|
||||||
text.Colorize(len(evt.Sender)+len(" kicked "), len(displayname), widget.GetHashColor(displayname))
|
text.Colorize(len(senderDisplayname)+len(" kicked "), len(displayname), widget.GetHashColor(*evt.StateKey))
|
||||||
} else {
|
} else {
|
||||||
text = tstring.NewColorTString(fmt.Sprintf("%s left the room.", displayname), tcell.ColorRed)
|
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 {
|
} else if displayname != prevDisplayname {
|
||||||
sender = "---"
|
sender = "---"
|
||||||
text = tstring.NewColorTString(fmt.Sprintf("%s changed their display name to %s.", prevDisplayname, displayname), tcell.ColorYellow)
|
text = tstring.NewColorTString(fmt.Sprintf("%s changed their display name to %s.", prevDisplayname, displayname), tcell.ColorGreen)
|
||||||
text.Colorize(0, len(prevDisplayname), widget.GetHashColor(prevDisplayname))
|
text.Colorize(0, len(prevDisplayname), widget.GetHashColor(*evt.StateKey))
|
||||||
text.Colorize(len(prevDisplayname)+len(" changed their display name to "), len(displayname), widget.GetHashColor(displayname))
|
text.Colorize(len(prevDisplayname)+len(" changed their display name to "), len(displayname), widget.GetHashColor(*evt.StateKey))
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func ParseMembershipEvent(room *rooms.Room, evt *gomatrix.Event) messages.UIMessage {
|
func ParseMembershipEvent(room *rooms.Room, evt *gomatrix.Event) messages.UIMessage {
|
||||||
displayname, text := getMembershipEventContent(room, evt)
|
displayname, text := getMembershipEventContent(room, evt)
|
||||||
|
if len(text) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
ts := unixToTime(evt.Timestamp)
|
ts := unixToTime(evt.Timestamp)
|
||||||
return messages.NewExpandedTextMessage(evt.ID, evt.Sender, displayname, "m.room.membership", text, ts)
|
return messages.NewExpandedTextMessage(evt.ID, evt.Sender, displayname, "m.room.membership", text, ts)
|
||||||
}
|
}
|
||||||
|
@ -240,10 +240,10 @@ func (view *RoomView) UpdateUserList() {
|
|||||||
var invited strings.Builder
|
var invited strings.Builder
|
||||||
for _, user := range view.Room.GetMembers() {
|
for _, user := range view.Room.GetMembers() {
|
||||||
if user.Membership == "join" {
|
if user.Membership == "join" {
|
||||||
joined.WriteString(widget.AddHashColor(user.DisplayName))
|
joined.WriteString(widget.AddColor(user.DisplayName, widget.GetHashColorName(user.UserID)))
|
||||||
joined.WriteRune('\n')
|
joined.WriteRune('\n')
|
||||||
} else if user.Membership == "invite" {
|
} else if user.Membership == "invite" {
|
||||||
invited.WriteString(widget.AddHashColor(user.DisplayName))
|
invited.WriteString(widget.AddColor(user.DisplayName, widget.GetHashColorName(user.UserID)))
|
||||||
invited.WriteRune('\n')
|
invited.WriteRune('\n')
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"hash/fnv"
|
"hash/fnv"
|
||||||
"sort"
|
"sort"
|
||||||
|
|
||||||
|
"maunium.net/go/gomuks/debug"
|
||||||
"maunium.net/go/tcell"
|
"maunium.net/go/tcell"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -51,6 +52,7 @@ func init() {
|
|||||||
// <-- = red
|
// <-- = red
|
||||||
// --- = yellow
|
// --- = yellow
|
||||||
func GetHashColorName(s string) string {
|
func GetHashColorName(s string) string {
|
||||||
|
debug.Print("Getting color for", s)
|
||||||
switch s {
|
switch s {
|
||||||
case "-->":
|
case "-->":
|
||||||
return "green"
|
return "green"
|
||||||
@ -72,8 +74,7 @@ func GetHashColor(s string) tcell.Color {
|
|||||||
return tcell.ColorNames[GetHashColorName(s)]
|
return tcell.ColorNames[GetHashColorName(s)]
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddHashColor adds tview color tags to the given string.
|
// AddColor adds tview color tags to the given string.
|
||||||
// The color added is the color returned by GetHashColorName().
|
func AddColor(s, color string) string {
|
||||||
func AddHashColor(s string) string {
|
return fmt.Sprintf("[%s]%s[white]", color, s)
|
||||||
return fmt.Sprintf("[%s]%s[white]", GetHashColorName(s), s)
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user